treecombobox.h
#ifndef TREECOMBOBOX_H
#define TREECOMBOBOX_H
#include <QComboBox>
#include "QAbstractItemView"
#include "QTreeView"
class TreeComboBox : public QComboBox
{
Q_OBJECT
public:
explicit TreeComboBox(QWidget *parent = 0);
~TreeComboBox();
protected:
QTreeView* internalView;
signals:
public slots:
};
#endif // TREECOMBOBOX_H
treecombobox.cpp
#include "treecombobox.h"
TreeComboBox::TreeComboBox(QWidget *parent) :
QComboBox(parent){
this->internalView = new QTreeView( parent );
this->setView( this->internalView );
QAbstractItemModel* model = this->internalView->model();
model->insertRows( 0, 2 );
model->setData( model->index(0,0), "First" );
model->setData( model->index(1,0), "Second" );
this->view()->setCurrentIndex( model->index(1,0) );
}
TreeComboBox::~TreeComboBox(){
if( this->internalView ){
delete this->internalView;
this->internalView = 0;
}
}
2番目のアイテムを表示したいのですが、qtで最初のアイテムを表示します。this-> view()-> currentIndex()は正しいモデルインデックスを提供しますが、ウィジェットは正しいコンテンツを表示しません。
私が欲しいのは、ツリービューのポップアップボックスを備えたコンボボックスです。ポップアップボックスは正常に機能します。唯一の問題は、プログラムで自動アイテムを選択しようとするとうまくいかないことです。
誰かが私にやり方を教えてもらえますか?