Results 1 to 3 of 3

Thread: Making a combobox in a tree view behave like a persistent editor

  1. #1
    Join Date
    Jan 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question Making a combobox in a tree view behave like a persistent editor

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Making a combobox in a tree view behave like a persistent editor

    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:
    Qt Code:
    1. class ComboBox : public QComboBox
    2. {
    3. Q_OBJECT
    4. public:
    5. ComboBox(QWidget* parent = 0) : QComboBox(parent) { }
    6.  
    7. public slots:
    8. void showPopup() { QComboBox::showPopup(); }
    9. };
    To copy to clipboard, switch view to plain text mode 

    Then, use a timer in createEditor() to make a delayed call to above slot:
    Qt Code:
    1. QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
    2. {
    3. ComboBox* combo = new ComboBox(parent);
    4. ...
    5. QTimer::singleShot(100, combo, SLOT(showPopup()));
    6. return combo;
    7. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jan 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Making a combobox in a tree view behave like a persistent editor

    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?

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.