Skip to content

Instantly share code, notes, and snippets.

@manashmandal
Created September 13, 2016 10:17
Show Gist options
  • Save manashmandal/8dd428a1bf30968b246ad455cd356973 to your computer and use it in GitHub Desktop.
Save manashmandal/8dd428a1bf30968b246ad455cd356973 to your computer and use it in GitHub Desktop.
Widget items on QTreeWidget
#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);
QTreeWidget *myTree = ui->treeWidget;
// 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");
// 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)));
// Creating widgetitems
QTreeWidgetItem *topLevelItem = new QTreeWidgetItem();
QTreeWidgetItem *childItem1 = new QTreeWidgetItem();
QTreeWidgetItem *childItem = new QTreeWidgetItem();
// Top level item having the next level item as the child
topLevelItem->addChild(childItem1);
topLevelItem->addChild(childItem);
// Adding the Top Level Button to the tree
myTree->addTopLevelItem(topLevelItem);
// Adding the buttons
myTree->setItemWidget(topLevelItem, 0, topLevelButton);
myTree->setItemWidget(childItem1, 0, childButton1);
myTree->setItemWidget(childItem, 0, childButton);
}
void TreeWidgetButton::onTopLevelButtonClicked(bool clicked)
{
ui->whichButtonLabel->setText("Top Level Button was Clicked");
}
void TreeWidgetButton::onchildButton1Clicked(bool clicked)
{
ui->whichButtonLabel->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="whichButtonLabel">
<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