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()为我提供了正确的模型索引,但小部件不显示正确的内容。
我想要的是一个带有树形视图弹出框的组合框。弹出框工作正常。唯一的问题是当我尝试在程序中自动选择一个项目时出错。
有人能告诉我该怎么办?