PDA

View Full Version : Making a combobox in a tree view behave like a persistent editor



zoeker
17th March 2008, 05:55
Hi,

I have a QTreeView in which one of the columns is handled by a delegate producing QComboBox editors in its createEditor function. In paint I'm drawing a snapshot of a combobox into the item area. For quite some time now I've been trying to achieve the look and feel of a real combobox being there, to no avail. You get the look easily, overriding the paint virtual function as described above. Getting the feel however means that the combobox popup should open when I click on such an item and that just doesn't happen - at least one click is needed to enter into edit mode, and a second one to open the popup.

The only solution I've found so far is to open persistent editors for each item in that particular column as soon as the model is set to the view, and also when rows are inserted or removed. However I dislike the idea of using persistent editors, even though the tree is quite small.

Do you know of a way to do this without using persistent editors and without breaking anything else?

jpn
17th March 2008, 20:29
I hope someone can come up with a better solution but here's one hackish and dirty way to work it around.

Fist, expose QComboBox::showPopup() as slot:


class ComboBox : public QComboBox
{
Q_OBJECT
public:
ComboBox(QWidget* parent = 0) : QComboBox(parent) { }

public slots:
void showPopup() { QComboBox::showPopup(); }
};


Then, use a timer in createEditor() to make a delayed call to above slot:


QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
{
ComboBox* combo = new ComboBox(parent);
...
QTimer::singleShot(100, combo, SLOT(showPopup()));
return combo;
}

zoeker
18th March 2008, 15:38
Thanks, jpn, that is one of the most well working solutions so far (all being hackish in some way). Unfortunately, some randomness is observed when single-clicking (quick single click, no delay on the press part) on the combo—sometimes it opens and stays opened, sometimes it shows for a brief moment and then disappears. The latter seems to be reproducible when first focusing the view and then clicking on the combos.

As a sidenote, my initial intention was to use QTreeView instead of QTableView although the data is of tabular nature because of the better default looks of QTreeView. However it turned out that I cannot tab navigate to other columns except the first one in a QTreeView. So I switched to QTableView.

In both views natural (not sure what exactly 'natural' means in the context of UI design but it surely goes agains my intuition) tab key navigation breaks, because when hitting tab and you get to a cell with a combo, the combo popup shows, getting the focus and then the user is forced to either hit Esc to get out or select some option.

And finally I got another problem—once focus gets inside a table view, there's no getting out of there using only the keyboard, it just cycles endlessly in the table view.

Considering all said, do you think it makes sense to research the option of writing my own table widget?