Results 1 to 6 of 6

Thread: Trouble with Qtreeview and QCheckboxes

  1. #1
    Join Date
    Feb 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Trouble with Qtreeview and QCheckboxes

    I'm trying to get checkboxes into my treeview and can't quite get it to work. I've looked around and can't find anything to solve my problem. I've attached the code that I have so far, but doesn't work:

    Qt Code:
    1. QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
    2. cities->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    3. cities->setText(0, tr("Places"));
    4.  
    5. (new QTreeWidgetItem(cities))->setText(0, tr("USA Cities"));
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance for your help

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trouble with Qtreeview and QCheckboxes

    Your item probably needs to have a non-null QVariant() value returned for its Qt::CheckStateRole. try setting Qt::Checked or Qt::Unchecked for Qt::CheckStateRole.

  3. #3
    Join Date
    Feb 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with Qtreeview and QCheckboxes

    Thank you for your input, it definitely helped. The code that I used to get it to work was:

    Qt Code:
    1. cities->setCheckState(0, Qt::Checked);
    To copy to clipboard, switch view to plain text mode 

    This was for the parent, I now have to do the same for the children.

    One such child is:

    Qt Code:
    1. (new QTreeWidgetItem(cities))->setText(0, tr("USA Cities"));
    To copy to clipboard, switch view to plain text mode 

    I'm not sure how to make this into a checkbox (I've checked the "Help" logs in QT for a idea but came up short when it came to children). Do you have any suggestions?

    Thanks again

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trouble with Qtreeview and QCheckboxes

    You create the item just like the cities node and then addChild() or insertChild() them to the parent item.

  5. #5
    Join Date
    Feb 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with Qtreeview and QCheckboxes

    Thanks man, you've been a HUGE help. The code that I have is:

    Qt Code:
    1. QTreeWidgetItem *citiesChild = new QTreeWidgetItem();
    2. cities->insertChild(0, citiesChild); //error here
    3. citiesChild->setText(0, tr("USA Cities"));
    4. citiesChild->setCheckState(0, Qt::Checked);
    To copy to clipboard, switch view to plain text mode 

    Works like a charm. Thanks again

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trouble with Qtreeview and QCheckboxes

    What is the "error here" if it "Works like a charm"?

Similar Threads

  1. Marquee selection for Qcheckboxes
    By hamid ghous in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2011, 13:12
  2. Trouble with CRC-32 function
    By Sölve in forum Newbie
    Replies: 4
    Last Post: 24th October 2010, 23:53
  3. Trouble with QLabel
    By dany_MB in forum Newbie
    Replies: 3
    Last Post: 14th August 2009, 08:21
  4. SQL trouble
    By xmeister in forum Newbie
    Replies: 2
    Last Post: 25th March 2009, 11:53
  5. Spreadsheet of QCheckBoxes...help!
    By JonathanForQT4 in forum Newbie
    Replies: 7
    Last Post: 11th July 2007, 10:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.