在QTreeview中使用QCombobox委托项

时间:2018-02-20 18:53:21

标签: c++ qt model-view-controller qtreeview qcombobox

我一直试图利用Qt的MVC类型编程来解决我试图解决的特定问题。基本上我想要一个QTree,其中的单元格中填充了各种QComboBox。

我已经创建了我的委托类,并且Combobox似乎已正确填充,另外我在更改组合框索引时使用自定义插槽进行调用但是我无法将发送方对象强制转换为组合框或找到另一种方法,我可以确定在组合框中选择的索引。

如果有人能帮到我,我会非常感激,我只是开始与Qt的MVC合作,我觉得我很接近,但我不能理解这一点进行。

主要有一些主窗口类executeLogicWindow.cpp

executeLogicSetupWindow::executeLogicSetupWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::executeLogicSetupWindow)
{
ui->setupUi(this);

int maxRow = 3,maxCol = 2;
treeModel = new QStandardItemModel(maxRow,maxCol,this);
treeItemDelegate = new controlObjectItemDelegate(this);

//tableView.setItemDelegateForColumn(1, delegate); // Column 0 can take any value, column 1 can only take values up to 8.
ui->dynamicObjectTreeView->setModel(treeModel);
ui->dynamicObjectTreeView->setItemDelegate(treeItemDelegate);



for(int row = 0;row< maxRow;row++)
{
    for(int col = 0;col< maxCol;col++)
    {
        QModelIndex index = treeModel->index(row,col,QModelIndex());
        //int value = (row+1) * (col+1);
        //treeModel->setData(index,QVariant(value),Qt::EditRole);
        treeModel->setData(index,"Click to Edit",Qt::EditRole);
    }
}


connect(treeModel,SIGNAL(itemChanged(QStandardItem*)),this,SLOT(OnTreeItemCBChanged(QStandardItem *)));


ui->logicSetupTableWidget->setColumnCount(8);
ui->logicSetupTableWidget->setRowCount(1);
ui->logicSetupTableWidget->setHorizontalHeaderLabels(QString("Input Condition;Val A;Comparison;Val B;Reaction;Output Action").split(";"));

methodIndex = 0;

addComboRow(ui->logicSetupTableWidget->rowCount()-1);

}

executeLogicSetupWindow::~executeLogicSetupWindow()
{
delete ui;
}


void executeLogicSetupWindow::OnTreeItemCBChanged(QStandardItem *testItem)
{

QComboBox* combo = qobject_cast<QComboBox*>(sender());

//QComboBox* combo = qobject_cast<QComboBox*>(testItem);

QModelIndex testIndex = testItem->index();
qDebug() << testItem->index();
int row = testItem->index().row();
int col = testItem->index().column();
QVariant value = testItem->data();

//qDebug(testItem);
if (combo)
{
    qDebug("It worked");
}
else
{
    qDebug("Guess its fucked");
}
}

继承委托类controlobjectdelegate.cpp

#include "controlobjectitemdelegate.h"
#include "QAbstractItemDelegate"
#include "QAbstractItemModel"


controlObjectItemDelegate::controlObjectItemDelegate(QObject *parent) :
QItemDelegate(parent)
{
}

QWidget* controlObjectItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *cellComboBox = new QComboBox(parent);
cellComboBox->addItems(QString("Test 1;Test 2;Test 3").split(";"));
return cellComboBox;
}

void controlObjectItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int value = index.model()->data(index,Qt::EditRole).toInt();
QComboBox *cellComboBox = static_cast<QComboBox*>(editor);
cellComboBox->setCurrentIndex(value);
}

void controlObjectItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *cellComboBox = static_cast<QComboBox*>(editor);
QStringList value;
for(int i=0;i<cellComboBox->count();++i)
{
    value.append(cellComboBox->itemText(i));
}


model->setData(index, cellComboBox->currentText(), Qt::EditRole);
}

void controlObjectItemDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}

1 个答案:

答案 0 :(得分:0)

首先,在使用setEditorData时,必须更正代理的代码,使用index.model()->data(index, Qt::EditRole)这包含一个无法转换为int的字符串,因此您始终将索引设置为0,因此,当您要选择其他选项时,第一个项目显示为已选中,您必须将该代码更改为以下内容:

void controlObjectItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString value = index.model()->data(index,Qt::EditRole).toString();
    QComboBox *cellComboBox = static_cast<QComboBox*>(editor);
    cellComboBox->setCurrentText(value);
}

没有方法来获取委托编辑小部件,但是如果我们可以获取它的值,我们可以将该值存储在新角色中:

void controlObjectItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QComboBox *cellComboBox = static_cast<QComboBox*>(editor);
    model->setData(index, cellComboBox->currentText(), Qt::EditRole);
    # save the index of the QComboBox in the role Qt::UserRole + 1
    model->setData(index, cellComboBox->currentIndex(), Qt::UserRole+1);
}

然后在插槽中获得该值:

void executeLogicSetupWindow::OnTreeItemCBChanged(QStandardItem *testItem)
{
    QVariant variant = testItem->data(Qt::UserRole+1);
    if(variant.isValid()){
        qDebug()<<variant.toInt();
    }
}