How to add check box inside qtree widget?
-
Hi all,
In my project i'm using a tree widget.
Which i created using designer...
Now i wanted to add checkbox at each child..
How can i do that?
My code to create the tree widget as shown below
@
#include "dialog.h"
#include "ui_dialog.h"
#include <QtCore>
#include <QtGui>Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{ui->setupUi(this); ui->treeWidget->setColumnCount(2); ui->treeWidget->setHeaderLabels(QStringList()<< "one"<<"two"); AddRoot("1 first","tree"); AddRoot("2 second","person"); AddRoot("3 third","man"); AddRoot("4 fourth","and last");
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::AddRoot(QString name,QString Description)
{
QTreeWidgetItem *itm =new QTreeWidgetItem(ui->treeWidget);
itm->setText(0,name);
itm->setText(1,Description);AddChild(itm,"one","1111"); AddChild(itm,"two","2222");
}
void Dialog::AddChild(QTreeWidgetItem *parent,QString name, QString Description)
{
QTreeWidgetItem *itm =new QTreeWidgetItem();
itm->setText(0,name);
itm->setText(1,Description);
parent->addChild(itm);
}void Dialog::on_pushButton_clicked()
{
ui->treeWidget->currentItem()->setBackgroundColor(0,Qt::red);ui->treeWidget->currentItem()->setBackgroundColor(1,Qt::blue);
}
@here i need check box to select - "one" ,"111"
and "two","2222222"...
please tell me how can i do that? -
Something like this would do the trick:
@void Dialog::AddChild(QTreeWidgetItem *parent,QString name, QString Description)
{
QTreeWidgetItem *itm =new QTreeWidgetItem();
itm->setText(0,name);
itm->setText(1,Description);
itm->setFlags(itm->flags() | Qt::ItemIsUserCheckable);
itm->setCheckState(Qt::Checked);
parent->addChild(itm);
}
@It first sets the item to be checkable, and then sets the current checked state to checked.