Results 1 to 3 of 3

Thread: QTreeView Checkable Item

  1. #1
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QTreeView Checkable Item

    Another question about qtreeview...
    I created a qtreeview and inserted a checkable item as following:

    Qt Code:
    1. MyPlot * plot = new MyPlot;
    2. QStandardItem * item = new QStandardItem(plot->title().text());
    3. item->setData( QVariant::fromValue(plot), PlotRole);
    4. item->setSelectable(false);
    5. item->setEditable(false);
    6. item->setCheckable(true);
    7. item->setCheckState(Qt::Checked);
    8. model->appendRow(item);
    9. myTreeView->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    ...and I've connected the model dataChanged signal to the following slot:

    Qt Code:
    1. void MyWidget::onDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight )
    2. {
    3. QVariant variant = topLeft.data(Spada::PlotRole);
    4. if (!variant.canConvert<MyPlot *>())
    5. return;
    6.  
    7. MyPlot * plot = variant.value<MyPlot *>();
    8.  
    9. variant = topLeft.data(Qt::ChechStateRole);
    10. if (variant.canConvert(QVariant::Int))
    11. {
    12. Qt::CheckState checked = (Qt::CheckState) variant.convert(QVariant::Int);
    13. if (checked == Qt::Unchecked )
    14. plot->hide();
    15. else
    16. plot->show();
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    Now, when I check/uncheck the item checkbox the slot is called but the checked variable is always Qt::PartiallyChecked...why?
    Is there an error in the posted code?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTreeView Checkable Item

    Qt Code:
    1. Qt::CheckState checked = (Qt::CheckState) variant.convert(QVariant::Int);
    To copy to clipboard, switch view to plain text mode 
    Don't know whether this is your problem or not, but variant.convert() returns a bool. In this case it will always be true (checked in the preceding "if" statement). Try using variant.toInt()

  3. #3
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTreeView Checkable Item

    Quote Originally Posted by norobro View Post
    Qt Code:
    1. Qt::CheckState checked = (Qt::CheckState) variant.convert(QVariant::Int);
    To copy to clipboard, switch view to plain text mode 
    Don't know whether this is your problem or not, but variant.convert() returns a bool. In this case it will always be true (checked in the preceding "if" statement). Try using variant.toInt()
    ops...a little careless mistake, I don't realize that the returned type is bool...thanks

Similar Threads

  1. Making QTreeView Items Checkable
    By JimDaniel in forum Qt Programming
    Replies: 4
    Last Post: 30th January 2020, 16:43
  2. Question on checkable QStandardItem in QTreeView
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2009, 00:30
  3. checkable item
    By gyre in forum Newbie
    Replies: 1
    Last Post: 23rd November 2007, 07:44
  4. QDirModel+QTreeView and checkable items
    By L.Marvell in forum Qt Programming
    Replies: 7
    Last Post: 11th May 2007, 18:54
  5. QTableWidget item checkable and combo?
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 07:12

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.