树组合框.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
树组合框.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;
}
}
我想显示第二个项目,但 qt 给了我第一个项目。this->view()->currentIndex() 给了我正确的模型索引,但小部件没有显示正确的内容。
我想要的是一个带有树视图弹出框的组合框。弹出框工作正常。唯一的问题是当我尝试在程序中自动选择一个项目时它出错了。
有人可以告诉我该怎么做吗?