Results 1 to 10 of 10

Thread: qtreeview + checkboxes

  1. #1
    Join Date
    Jul 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default qtreeview + checkboxes

    hi there!

    I had a look at some other threads concerning my question but still I'm not sure how to get this working.

    I'm doing an application where I have got a QTreeView in QSplitter. The Tree should fill itself up with data from a QList<QString> and it's depth will be just 1, there should be 2 more columns with checkboxes for the children so:

    column0 column1 column2
    parent
    child checkbox checkbox
    child checkbox checkbox
    child checkbox checkbox
    parent
    child checkbox checkbox
    parent
    child checkbox checkbox
    child checkbox checkbox etc.

    and the checkboxes should be editable and other elements in the QSplitter will change according to what checkboxes are checked.

    So should I implement my own Model and somehow override the flags or is it better to do it just using the QStandardItemModel and then model->setIndexWidget(index,QCheckbox) but then I can't get the children's indexes, it just works for the parents.

    Maybe someone's done something similar and could send me a sample?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qtreeview + checkboxes

    You should definitely use checkable items instead of index widgets. Writing a tree model is not trivial so using QStandardItemModel might be easier to start with.
    J-P Nurmi

  3. #3
    Join Date
    Jul 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qtreeview + checkboxes

    thanks for your reply.

    I tried building my model, but it doesn't work, so I think I'll go back to the QStandardItemModel idea as you suggested.

    I had a look at the QT website and there's an example to create a tree:
    Qt Code:
    1. QStandardItem *parentItem = model.invisibleRootItem();
    2. for (int i = 0; i < 4; ++i) {
    3. QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
    4. parentItem->appendRow(item);
    5. parentItem = item;
    6. }
    To copy to clipboard, switch view to plain text mode 

    and then:

    Qt Code:
    1. QTreeView *treeView = new QTreeView(this);
    2. treeView->setModel(myStandardItemModel);
    3. connect(treeView, SIGNAL(clicked(QModelIndex)),
    4. this, SLOT(clicked(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

    and what is this myStandardItemModel? is it just a defined variable in the header file like:

    Qt Code:
    1. QStandardItemModel *myStandardItemModel;
    To copy to clipboard, switch view to plain text mode 

    pls help me out with this. I'm really lost now.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qtreeview + checkboxes

    why you can't use this method
    Qt Code:
    1. void QStandardItem::setCheckable ( bool checkable )
    To copy to clipboard, switch view to plain text mode 
    ?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qtreeview + checkboxes

    They are two different code example snippets. Variable names don't necessarily match each others. Just like all over Qt docs, basic C++ knowledge is necessary to understand examples. You should try to concentrate on understanding what they actually do instead of copy-pasting them directly to your own code and compiling blindly.
    J-P Nurmi

  6. #6
    Join Date
    Jul 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qtreeview + checkboxes

    Right thanks a lot for the replies

    Yeah I got a bit confused and fed up with my work so far but I'm back on the road again.
    and now I got a piece of code like this:
    Qt Code:
    1. model = new QStandardItemModel(0,2,this);
    2. model->setHeaderData(0, Qt::Horizontal, QObject::tr("BLA1"));
    3. model->setHeaderData(1, Qt::Horizontal, QObject::tr("BLA2"));
    4. QStandardItem *parentItem = new QStandardItem("parent");
    5. model->appendRow(parentItem);
    6.  
    7. QStandardItem *subItem1 = new QStandardItem("item1");
    8. parentItem->appendRow(subItem1);
    To copy to clipboard, switch view to plain text mode 
    and now I want a checkbox in the second row and second column i.e. next to subitem1, same row, next column, so I do:
    Qt Code:
    1. model->setData(model->index(1, 1), Qt::Checked, Qt::CheckStateRole);
    To copy to clipboard, switch view to plain text mode 
    and thiiiis does nooooot work! tell me why tell me why

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qtreeview + checkboxes

    I think you have to make the item checkable first by calling the method mentioned earlier.

  8. #8
    Join Date
    Jul 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qtreeview + checkboxes

    right so if I use the following statement:
    Qt Code:
    1. item->setCheckable(true)
    To copy to clipboard, switch view to plain text mode 
    in a while loop, how can I now control what happens to the items, I mean, how can I apply some kind of action if the user checks a box?
    I tried using the
    Qt Code:
    1. connect()
    To copy to clipboard, switch view to plain text mode 
    method, but it doesn't work for QStandarditems...

    I'm sorry to bother you guys, I'm a real newbie
    thanks for any help.

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qtreeview + checkboxes

    You could connect to QStandardItemModel::itemChanged().
    J-P Nurmi

  10. #10
    Join Date
    Jul 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qtreeview + checkboxes

    hey there!
    Thanks for your help!
    I really appreciate it I finally did it using
    Qt Code:
    1. void QStandardItem::setCheckable ( bool checkable )
    To copy to clipboard, switch view to plain text mode 
    and then
    Qt Code:
    1. Qt::CheckState checkState () const
    To copy to clipboard, switch view to plain text mode 
    to manage what has been checked.
    I still got maaaany problems, but I'm moving ahead!
    thanks again.

Similar Threads

  1. QTreeView repaint
    By graeme in forum Qt Programming
    Replies: 17
    Last Post: 13th March 2012, 13:27
  2. Checkboxes in QAbstractITemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 28th November 2007, 20:23
  3. QTreeView help
    By bepaald in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 21:22
  4. QTreeView: Holding a line on screen
    By gri in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 11:42
  5. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 14:24

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.