Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add check box inside qtree widget?
QtWS25 Last Chance

How to add check box inside qtree widget?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 19.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aurora
    wrote on 29 Dec 2011, 10:01 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 29 Dec 2011, 10:42 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aurora
        wrote on 29 Dec 2011, 11:45 last edited by
        #3

        Thank u so much Andre...:)

        1 Reply Last reply
        0

        1/3

        29 Dec 2011, 10:01

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved