Skip to content

Instantly share code, notes, and snippets.

@manashmandal
Created September 14, 2016 20:49
Show Gist options
  • Save manashmandal/2854e2b08690718258bf3a99f14830e6 to your computer and use it in GitHub Desktop.
Save manashmandal/2854e2b08690718258bf3a99f14830e6 to your computer and use it in GitHub Desktop.
QTreeWidget with Buttons and other widgets
#include "treewidgetbutton.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TreeWidgetButton w;
w.show();
return a.exec();
}
#include "treewidgetbutton.h"
#include "ui_treewidgetbutton.h"
#include <QPushButton>
#include <QTreeWidgetItem>
#include <QDebug>
#include <QList>
#include <QLabel>
TreeWidgetButton::TreeWidgetButton(QWidget *parent) :
QDialog(parent),
ui(new Ui::TreeWidgetButton)
{
ui->setupUi(this);
// Push buttons to be added
QPushButton *topLevelButton = new QPushButton("Top Level Button");
QPushButton *childButton1 = new QPushButton("Child Button 1");
QPushButton *childButton = new QPushButton("Child Button 0");
QPushButton *nestedChildButton = new QPushButton("Nested Child");
// // Connecting the button signals with corresponding slots
connect(childButton1, SIGNAL(clicked(bool)), this, SLOT(onchildButton1Clicked(bool)));
connect(topLevelButton, SIGNAL(clicked(bool)), this, SLOT(onTopLevelButtonClicked(bool)));
QTreeWidgetItem *topLevelItem = new QTreeWidgetItem();
QTreeWidgetItem *childItem1 = new QTreeWidgetItem();
QTreeWidgetItem *childItem = new QTreeWidgetItem();
QTreeWidgetItem *nestedChildItem = new QTreeWidgetItem();
ui->treeWidget->addTopLevelItem(topLevelItem);
// Adding children to the topLevelItem
topLevelItem->addChild(childItem);
topLevelItem->addChild(childItem1);
// Adding a child node to a child [Nested Child]
childItem->addChild(nestedChildItem);
ui->treeWidget->setItemWidget(topLevelItem, 0, topLevelButton);
ui->treeWidget->setItemWidget(childItem, 0, childButton);
ui->treeWidget->setItemWidget(childItem1, 0, childButton1);
ui->treeWidget->setItemWidget(nestedChildItem, 0, nestedChildButton);
}
void TreeWidgetButton::onTopLevelButtonClicked(bool clicked)
{
ui->label->setText("Top Level Button was Clicked");
}
void TreeWidgetButton::onchildButton1Clicked(bool clicked)
{
ui->label->setText("Child Button 1 was Clicked");
}
TreeWidgetButton::~TreeWidgetButton()
{
delete ui;
}
#ifndef TREEWIDGETBUTTON_H
#define TREEWIDGETBUTTON_H
#include <QDialog>
namespace Ui {
class TreeWidgetButton;
}
class TreeWidgetButton : public QDialog
{
Q_OBJECT
public:
explicit TreeWidgetButton(QWidget *parent = 0);
~TreeWidgetButton();
protected slots:
void onTopLevelButtonClicked(bool clicked);
void onchildButton1Clicked(bool clicked);
private:
Ui::TreeWidgetButton *ui;
};
#endif // TREEWIDGETBUTTON_H
#-------------------------------------------------
#
# Project created by QtCreator 2016-09-13T10:02:21
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TreeWidgetButton
TEMPLATE = app
SOURCES += main.cpp\
treewidgetbutton.cpp
HEADERS += treewidgetbutton.h
FORMS += treewidgetbutton.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TreeWidgetButton</class>
<widget class="QDialog" name="TreeWidgetButton">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>TreeWidgetButton</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QTreeWidget" name="treeWidget">
<column>
<property name="text">
<string>Button Column</string>
</property>
</column>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Which Button Was Clicked</string>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment