QComboBox inside QTreeWidgetItem

点点圈 提交于 2019-12-22 04:31:24

问题


Is there something similar to the (PyQT) QTreeWidgetItem.setCheckState(0, Qt.Checked) but for the combo box?

I can't see anything in the reference, so how can I insert a custom QComboBox as one of the elements within QTreeWidgetItem?


回答1:


Use QTreeWidget::setItemWidget ( QTreeWidgetItem * item, int column, QWidget * widget ) to put the combo box into the cells.

For example, let's make all rows of the second column of a 2-column QTreeWidget to all be combo boxes:

QTreeWidgetItemIterator it(ui->treeWidget);
while (*it) {
    QComboBox *comboBox = new QComboBox(this);
    comboBox->addItems(QStringList() << "item1" << "item2");
    ui->treeWidget->setItemWidget(*it, 1, comboBox);
    ++it;
}

Our example widget now looks like this:




回答2:


I know this is an old question but I think I have a more thorough answer. To get any functionality out of the QComboBox, you'll probably need to subclass it. Here's the solution that I came up with:


#ifndef COMBOBOXITEM_H
#define COMBOBOXITEM_H

#include 

class ComboBoxItem : public QComboBox
{
    Q_OBJECT

private:
    QTreeWidgetItem *item;
    int column;

public:
    ComboBoxItem(QTreeWidgetItem*, int);

public slots:
    void changeItem(int);

};

ComboBoxItem::ComboBoxItem(QTreeWidgetItem *item, int column)
{
    this->item = item;
    this->column = column;
    connect(this, SIGNAL(currentIndexChanged(int)), SLOT(changeItem(int)));
}

void ComboBoxItem::changeItem(int index)
{
    if(index >=0)
    {
        item->setData(this->column, Qt::UserRole, this->itemText(index));
        qDebug() item->data(this->column, Qt::UserRole).toString();
    }
}

#include "moc_ComboBoxItem.cpp"

#endif // COMBOBOXITEM_H

////// Sample implementation..

lst = new QTreeWidget;
// Snip
QTreeWidgetItem *itm = new QTreeWidgetItem;
// Snip
ComboBoxItem *cmb = new ComboBoxItem(itm, 1);
cmb->addItem("One");
cmb->addItem("Two");
cmb->addItem("Three");
cmb->addItem("Four");
lst->setItemWidget(itm, 1, cmb);

I hope that helps someone in need of a QComboBox inside of a QTreeWidgetItem!




回答3:


Here is small fix to the another posters method. I found that is uses Data to update the box How ever I made small change to setText updater for the method.

#ifndef COMBOBOXITEM_H
#define COMBOBOXITEM_H

#include <QtGui>

class ComboBoxItem : public QComboBox
{
    Q_OBJECT

private:
    QTreeWidgetItem *item;
    int column;

public:
    ComboBoxItem(QTreeWidgetItem*, int);

public slots:
    void changeItem(int);

};

ComboBoxItem::ComboBoxItem(QTreeWidgetItem *item, int column)
{
    this->item = item;
    this->column = column;
    connect(this, SIGNAL(currentIndexChanged(int)), SLOT(changeItem(int)));
}

void ComboBoxItem::changeItem(int index)
{
    if(index >=0)
    {
        this->item->setText(this->column, this->currentText());

    }
}

#include "moc_ComboBoxItem.cpp"



#endif // COMBOBOXITEM_H

////// Sample implementation..

lst = new QTreeWidget;
// Snip
QTreeWidgetItem *itm = new QTreeWidgetItem;
// Snip
ComboBoxItem *cmb = new ComboBoxItem(itm, 1);
cmb->addItem("One");
cmb->addItem("Two");
cmb->addItem("Three");
cmb->addItem("Four");
lst->setItemWidget(itm, 1, cmb);



回答4:


This is easiest method:

QComboBox *cb = new QComboBox(this);
QStringList cbTexts;
cbTexts << tr("First") << tr("Second") << tr("Third");
cb->addItems(cbTexts);

QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
ui->treeWidget->addTopLevelItem(item);
ui->treeWidget->setItemWidget(item, [colum here], cb);
for (int col = 0; col < [num colums]; ++col) ui->treeWidget->resizeColumnToContents(col);



回答5:


Use

setItemWidget(QTreeWidgetItem( ), column, QWidget( ) )

.Just add your QComboBox() as a parameter, as it inherits QWidget() so it is compatible.

tree = QTreeWidget()

cmb = QComboBox()
cmb.addItem("Item1", 'value1')
cmb.addItem("Item2", 'value2')
cmb.addItem("Item3", 'value3')

item = QTreeWidgetItem(tree.invisibleRootItem())
column = 0
item.setData(column, Qt.EditRole, 'NameYouWant')
column += 1
tree.setItemWidget(item, column , cmb)


来源:https://stackoverflow.com/questions/1667688/qcombobox-inside-qtreewidgetitem

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