Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT Clickable widget (possibly button) inside QTreeWidget?

Tags:

c++

qt

I have a table that is basically a QTreeWidget and I want to put a clickable widget, possibly a button inside it. Each row is a QTreeWidgetItem, but I don't see how I can add a button with QTreeWidgetItem::setData

like image 681
ulak blade Avatar asked Jul 09 '15 11:07

ulak blade


1 Answers

Here is a modification to the example provided in Qt Documentation for QTreeWidget adding a QPushButton to the second item

 ui->treeWidget->setColumnCount(1);
 QList<QTreeWidgetItem *> items;
 for (int i = 0; i < 10; ++i)
    items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
 ui->treeWidget->insertTopLevelItems(0, items);

 ui->treeWidget->setItemWidget(items.value(1),0,new QPushButton("Click Me")); // Solution for your problem 

For two push buttons side by side within an item,you can take this approach

QWidget *dualPushButtons = new QWidget();
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addWidget(new QPushButton("Button1"));
hLayout->addWidget(new QPushButton("Button2"));
dualPushButtons->setLayout(hLayout);

ui->treeWidget->setItemWidget(items.value(1),0,dualPushButtons);

You can adapt this by adding properties to the buttons etc.

like image 86
techneaz Avatar answered Nov 02 '22 18:11

techneaz