1
我想說明的第二個項目treecombobox.hQT QComboBox的setView到QTreeView則無法做到setCurrentIndex
#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;
}
}
,但QT給我的第一個項目。 this-> view() - > currentIndex()爲我提供了正確的模型索引,但小部件不顯示正確的內容。
我想要的是一個帶有樹形視圖彈出框的組合框。彈出框工作正常。唯一的問題是當我嘗試在程序中自動選擇一個項目時出錯。
有人能告訴我該怎麼辦?