PDA

View Full Version : how to add check box inside qtree widget?



aurora
29th December 2011, 09:59
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?

kunashir
29th December 2011, 10:06
Hello.
In QTableWidget I used
setFlags ( Qt::ItemFlags flags )
and add flags Qt::ItemIsUserCheckable.
Try in your case.
The QTreeWidget has the same features.

aurora
29th December 2011, 11:44
ok thank u....:)

Karti Sharma
24th May 2012, 09:38
@kunashir

Can you mention the code excerpt which you used for ItemFlags usage?
Thanks in advance!!

ChrisW67
25th May 2012, 01:16
You already have the name of the function, QTreeWidgetItem::setFlags(), its documentation, and the Qt::ItemFlag documentation it links to. You even have the the name of the specific flag, Qt::ItemIsUserCheckable. Is it really such a difficult thing to work out?


QTreeWidgetItem *item = new QTreeWidgetItem();
// OR QTableWidgetItem *item = new QTableWidgetItem();
// OR QStandardItem *item = new QStandardItem();
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable);