9

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Piotr Byzia
  • 3,363
  • 7
  • 42
  • 62

5 Answers5

26

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:

enter image description here

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
15

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!

  • Thanks, this is much affective way of answering, i wonder who chose best answer!! – Orochi Nov 26 '11 at 20:45
  • Well, the question was posted back in 2009 and I answered it in 2011 so I'm sure that's why. –  Nov 27 '11 at 00:27
  • :) But thanks anyway....really helpful as i was thinking of using delegates for the views but this solution is much easier – Orochi Nov 27 '11 at 17:01
  • Instead of item->setData(..) try this: item->setText(column, itemText(index)); It will trigger QTreeWidget's signals for changed values. – G Huxley Jan 10 '14 at 00:20
1

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)
Garjy
  • 467
  • 5
  • 12
0

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);
manuel
  • 533
  • 6
  • 16
Jeroi
  • 11
  • 4
0

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);
Jeroi
  • 11
  • 4