Results 1 to 7 of 7

Thread: Radio buttons in a tree view

  1. #1
    Join Date
    Oct 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Radio buttons in a tree view

    I want to provide radio buttons inside a tree view, using them in place of check boxes. This post describes these steps:
    • Telling the view to use a radio button icon instead of a check box one.
    • Providing mutually exclusive semantics underneath a tree model.


    I have a subclass of QAbstractItemModel (let's call it MyModel) which works over an internal representation. To add radio buttons to it I've:
    • Reimplemented QItemDelegate::drawCheck to use a radio button primitive element.
    • Used an internal representation which only allows one internal object to have an active state.
    • Defined MyModel::data for CheckStateRole that is Qt::Checked or Qt::Unchecked dependent on whether the internal object for the button is active.
    • Defined MyModel::setData for CheckStateRole which modifies which internal object is active.
    • Defined MyModel::flags


    There are two observable problems. The first is that selecting the button requires two clicks, one to select the cell, and the next to select the button itself. This is similar to the behavior for editing an editable cell: you have to click on the cell to select, and then click again to edit, but it's not an acceptable behavior for radio buttons. What I want is the single-click behavior I can get from a simple demo involving QTreeView and QStandardItem::setCheckable. I thought it might something to do with my combination of flags, but it doesn't appear to. I also presume I can change it by managing mouse events manually but I'd prefer not to.

    The second problem is more serious. Selecting one radio button works fine. Selecting a second button shows that button as selected, but leaves the first button selected as well. Selecting the second button another time clears the first button. That seems like an obvious model problem, except that I've verified the internal state by printing it every time setData is called and only one object is active. What's more I've verified that when data is called its not only got only one object active, but that the appropriate Qt.Checked or Qt.Unchecked is being returned. It's not the model. The view seems to have a refresh problem. The obvious issue is emitting dataChanged(QModelIndex, QModelIndex) but that happens after every successful setData.

    This is PyQt 4.4.2 over Qt 4.4.1. (While it's not impossible that this is a PyQt problem, my experience is that PyQt is a pretty faithful binding. When I have problems in PyQt with Qt's behavior which aren't fatal or clearly Python specific, rewriting the code in C++ as a test always produces the same behavior, so I'm strongly inclined to believe it's my misunderstanding Qt over it being a bindings issue.)

    Relevant code samples below.

  2. #2
    Join Date
    Oct 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: Radio buttons in a tree view

    Relevant code snippets. These aren't the complete methods, just those parts involving the check state (that's probably obvious):

    The tree model:

    Qt Code:
    1. class TreeModel (QAbstractItemModel):
    2.  
    3. def flags(self, index):
    4. flags = Qt.ItemIsEnabled
    5. item = self.get_item(index)
    6. # ...
    7. if index.column() is TreeModel.COLUMN_X:
    8. if item.is_active():
    9. flags |= Qt.ItemIsSelectable | Qt.ItemIsUserCheckable
    10. return flags
    11.  
    12. def data(self, index, role):
    13. data = QVariant()
    14. item = self.get_item(index)
    15. # ...
    16. if role == Qt.CheckStateRole:
    17. if index.column() is TreeModel.COLUMN_X:
    18. if item.is_active():
    19. data = QVariant(Qt.Checked)
    20. else:
    21. data = QVariant(Qt.Unchecked)
    22. return data
    23.  
    24. def setData(self, index, value, role):
    25. status = False
    26. item = self.get_item(index)
    27. # ...
    28. if role == Qt.CheckStateRole:
    29. if index.column() is TreeModel.COLUMN_X:
    30. if not item.is_active():
    31. item.set_active(True)
    32. else:
    33. item.set_active(False)
    34. status = True
    35. self.emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)', index, index)
    36. return status
    To copy to clipboard, switch view to plain text mode 

    The item delegate. Compare to the implementation of QItemDelegate.drawCheck. It differs only in two places:
    • The last line uses QStyle.PE_IndicatorRadioButton instead of QStyle.PE_IndicatorViewItemCheck
    • In the last two lines Instead of asking the private implementation for the owning widget (which can't be done), assume it's the tree view.



    Qt Code:
    1. class TreeRenderer(QItemDelegate):
    2.  
    3. def drawCheck( self, painter, option, rect, state ):
    4. '''Draw a radio button instead of a check button for mutually exclusive fields.'''
    5. if not rect.isValid(): return
    6. option.rect = rect
    7. option.state &= ~QStyle.State_HasFocus
    8. option.state |= {
    9. Qt.Unchecked: QStyle.State_Off,
    10. Qt.PartiallyChecked: QStyle.State_NoChange,
    11. Qt.Checked: QStyle.State_On
    12. }[ state ]
    13. style = self.view.style() if self.view else QApplication.style()
    14. style.drawPrimitive( QStyle.PE_IndicatorRadioButton, option, painter, self.view )
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Radio buttons in a tree view

    Why not implement QItemDelegate::createEditor() .etc to create a radioButton and open it persistent in treeView? in my opinion, Qt::CheckRole behaves like QCheckBox, and if the checked button need to be exclusive, other buttons have to be setChecked(false), every time one button was clicked. i had did similar work before, which needed several exclusive buttonGroups, and QButtonGroup works well.

  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: Radio buttons in a tree view

    Why not implement QItemDelegate::createEditor() .etc to create a radioButton and open it persistent in treeView?
    imho, it is bad idea, because performance of your application will decrease, because a lot of descendants of QRadioButton are opend. I think the best way is implement your own model/delegate which will allow to use radio-items approach.

  5. #5
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Radio buttons in a tree view

    Quote Originally Posted by spirit View Post
    imho, it is bad idea, because performance of your application will decrease, because a lot of descendants of QRadioButton are opend. I think the best way is implement your own model/delegate which will allow to use radio-items approach.
    thank you. so, as "Delegate Class" part in Qt's doc says, "Delegates are expected to be able to render their contents themselves by implementing the paint() and sizeHint() functions.", it is not expected to keep a real editor widget persistently? if i need a radio button in some cells all the time, what should i do? should i implement CustomDelegate:aint(), and paint pixmaps of radio_on and radio_off in the cells? I used to do this like this:

    CustomDelegate::createEditor()
    {
    return new QRadioButton;
    }

    and calling QItemView:penPersistentEditor()for each index, or else the editor will be destroyed the editing finishes. i knew it's not a good way i really need a radiobutton in my view.

  6. #6
    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: Radio buttons in a tree view

    1) (quick method) you can use add method setExclusive to a view or to a delegate and then reimplement this method QItemDelegate::drawCheck. according to exclusivity you can draw checkbox (just call drawCheck of paren, i.e. QItemDelegate::drawCheck) or radiobox.
    2) subclass a needed model, then subclass a needed item, e.g. QStandartItem, add to it mehtod setExclusive and extend methods setChecked for supporting exclusivity () and then reimplement a method QItemDelegate::drawCheck.
    I think that's all. I hope, I helped you.
    Last edited by spirit; 3rd November 2008 at 07:39.

  7. The following user says thank you to spirit for this useful post:

    nifei (3rd November 2008)

  8. #7
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Radio buttons in a tree view

    Quote Originally Posted by spirit View Post
    1) (quick method) you can use add method setExclusive to a view or to a delegate and then reimplement this method QItemDelegate::drawCheck. according to exclusivity you can draw checkbox (just call drawCheck of paren, i.e. QItemDelegate::drawCheck) or radiobox.
    2) subclass a needed model, then subclass a needed item, e.g. QStandartItem, add to it mehtod setExclusive and extend methods setChecked for supporting exclusivity () and then reimplement a method QItemDelegate::drawCheck.
    I think that's all. I hope, I helped you.
    thanks a lot. i'll try.

Similar Threads

  1. Programatically setting radio buttons
    By Doug Broadwell in forum Newbie
    Replies: 2
    Last Post: 25th May 2007, 20:55
  2. Ignore mouse events out of tree view
    By krishna.bv in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2006, 11:24
  3. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  4. array of radio buttons
    By amulya in forum Qt Programming
    Replies: 4
    Last Post: 5th October 2006, 12:59
  5. How to get larger radio buttons on XP?
    By Ben.Hines in forum Qt Programming
    Replies: 9
    Last Post: 24th April 2006, 19:00

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.