Results 1 to 7 of 7

Thread: Radio buttons in a tree view

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

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

    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.

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

    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.

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

    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.

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

    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.

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

    nifei (3rd November 2008)

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

    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
  •  
Qt is a trademark of The Qt Company.