notwithstanding
22nd October 2008, 23:03
I want to provide radio buttons inside a tree view, using them in place of check boxes. This post (http://www.qtcentre.org/forum/p-qtreewidgetitem-radio-button-post54032/postcount4.html) 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.
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.