Inserting a child Item into a row of QTreeWidgetItems

孤人 提交于 2019-12-11 06:05:59

问题


I have setup my QTreeWidget such that each cell is filled with comboboxes, however I would like to create a text edit widget next to the selected combobox (or overwrite the existing combobox), depending on which combobox item the user has selected.

I figured I could do this by adding the comboboxes parent as a property when its initially setup and then upon interaction with the combobox simply use setItemWidget to put a text edit Item in column after the selected combobox (using the same child). But it seems as though the parent is not being passed correctly or some other issue is causing the text edit widget to appear in the row below where it should be.

I've attached a photo and some code for clarification.

This is where I setup the comboboxes for the QTreeWidget (Specifically where rowType == 0)

void customMethodConstructorWindow::addChildRow(QTreeWidget *widgetParent,QTreeWidgetItem *itemParent,int rowType)
{
//widgetParent->setColumnCount(methodBlocks.at(rowType).size());
if(rowType == 0)
{
    QTreeWidgetItem *childItem = new QTreeWidgetItem(itemParent);

    QVariant itemParentVariant;
    itemParentVariant.setValue(itemParent);
    for(uint cycleSetup = 0;cycleSetup < methodBlocks.at(rowType).size();cycleSetup++)
    {

        QComboBox *itemComboBox = new QComboBox;
        itemComboBox->setProperty("rowType", rowType);
        itemComboBox->setProperty("row", 0);
        itemComboBox->setProperty("column",cycleSetup);
        itemComboBox->setProperty("itemParent",itemParentVariant);
        itemComboBox->addItems(methodBlocks.at(0).at(cycleSetup));
        QObject::connect(itemComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
        widgetParent->setItemWidget(childItem,cycleSetup,itemComboBox);
        itemParent->addChild(childItem);
    }

}
else
{
    QTreeWidgetItem *childItem = new QTreeWidgetItem(itemParent);

    QComboBox *item0ComboBox = new QComboBox;
    item0ComboBox->setProperty("rowType", rowType);
    item0ComboBox->setProperty("row", 1);
    item0ComboBox->setProperty("column", 0);
    item0ComboBox->addItems(methodBlocks.at(1).at(0));
    QObject::connect(item0ComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
    widgetParent->setItemWidget(childItem,0,item0ComboBox);
    itemParent->addChild(childItem);

    QComboBox *item1ComboBox = new QComboBox;
    item1ComboBox->setProperty("rowType", rowType);
    item1ComboBox->setProperty("row", 1);
    item1ComboBox->setProperty("column", 1);
    item1ComboBox->addItems(methodBlocks.at(1).at(rowType));
    QObject::connect(item1ComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
    widgetParent->setItemWidget(childItem,1,item1ComboBox);
    itemParent->addChild(childItem);
}
}

And this is where I try to create a text edit widget over one of the comboboxes

void customMethodConstructorWindow::OnComboIndexChanged(const QString& text)
{
QComboBox* combo = qobject_cast<QComboBox*>(sender());
if (combo)
{

    int nRow = combo->property("row").toInt();
    int nCol = combo->property("column").toInt();
    switch (nRow) {
    case 0:
    {
        switch (nCol)
        {
        case 0:
        {
            //combo->setVisible(false);
            //testPoint = combo->pos();


        }
            break;
        case 1:
        {
            if(combo->currentIndex() != 0)
            {
                QTreeWidgetItem *childItem = new QTreeWidgetItem(combo->property("itemParent").value<QTreeWidgetItem*>());
                QTextEdit *textItemEdit = new QTextEdit;
                //QObject::connect(item1ComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
                ui->methodSetupTreeWidget->setItemWidget(childItem,2,textItemEdit);
            }

        }
            break;
        case 2:
                //customObjectAttributes.comparisonType.push_back(combo->currentIndex());
            break;
        case 3:
        {
                //customObjectAttributes.valueB.push_back(combo->currentIndex());
        }
            break;
        case 4:

            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            //ui->logicSetupTableWidget->setRowCount(ui->logicSetupTableWidget->rowCount()+1);
            if(combo->currentIndex() != 3)
            {
                //addComboRow(ui->logicSetupTableWidget->rowCount()-1);
            }
              //customObjectAttributes.outputType.push_back(combo->currentIndex());
            break;
        }
    }
        break;
    case 1:
    {
        switch (combo->property("column").toInt())
        {
        case 0:

            break;
        case 1:
        {
                addChildRow(ui->methodSetupTreeWidget,ui->methodSetupTreeWidget->itemAt(QPoint(2,1)),0);
                addChildRow(ui->methodSetupTreeWidget,ui->methodSetupTreeWidget->itemAt(QPoint(3,1)),1);

        }
            break;
        case 2:
        {

        }
            break;
        case 3:
        {

        }
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
        {

            if(combo->currentIndex() != 3)
            {
                //addComboRow(ui->logicSetupTableWidget->rowCount()-1);
            }

        }
            break;
        }
    }
        break;
    }


}

}

ScreenShot of current situation

My issue here is that the text edit shows up below the less than (<) combobox rather than ontop (or replacing it).


回答1:


I was able to find a solution using QStandardItem:setData() and QStackedWidget to get the functionality I was after.

This was done in the process of trying to solve a related issue here.



来源:https://stackoverflow.com/questions/48932540/inserting-a-child-item-into-a-row-of-qtreewidgetitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!