Results 1 to 5 of 5

Thread: QComboBox multi-column popup selection quirks [SOLVED]

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default QComboBox multi-column popup selection quirks [SOLVED]

    Hi All,

    I have a quick question regarding the behaviour of QComboBox with a multi-column QTableView custom view.

    I have the combo box set up like this:
    Qt Code:
    1. TypeModel *model = new TypeModel(this);
    2.  
    3. QComboBox *combo = new QComboBox(central);
    4. QTableView *view = new QTableView(this);
    5. view->setModel(model);
    6. view->resizeColumnsToContents();
    7. view->resizeRowsToContents();
    8. view->verticalHeader()->setVisible(false);
    9. view->setMinimumWidth(view->horizontalHeader()->length());
    10. view->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    11. view->setSelectionMode(QAbstractItemView::SingleSelection);
    12. view->setSelectionBehavior(QAbstractItemView::SelectRows);
    13. view->setAutoScroll(false);
    14.  
    15. combo->setModel(model);
    16. combo->setModelColumn(0);
    17. combo->setView(view);
    To copy to clipboard, switch view to plain text mode 
    TypeModel is a simple table model returning code-meaning pairs. The flags() method looks like:
    Qt Code:
    1. // column 0 is the code, column 1 is the meaning
    2. Qt::ItemFlags TypeModel::flags(const QModelIndex &index) const
    3. {
    4. Qt::ItemFlags theFlags = QAbstractItemModel::flags(index);
    5. if (index.isValid()) {
    6. theFlags &= ~Qt::ItemIsEditable;
    7. if (index.column() != 0)
    8. theFlags &= ~Qt::ItemIsSelectable;
    9. }
    10. return theFlags;
    11. }
    To copy to clipboard, switch view to plain text mode 
    If lines 7 and 8 are as above then the combo pop-up only allows clicking on items in the first column, and the resulting code is displayed in the combo box. If lines 7-8 are omitted then the user can click on either column of the popup but the text of the cell they clicked on, which may be a meaning, is put in the combo box. Setting the model column for the combo box does not change this. In either case the combo box current index is correct.

    Is there a way to have the popup clickable anywhere but have the code placed in the combo box?

    Regards,
    Chris

    Full code attached. I am using Qt 4.6.3 on Linux.
    Attached Files Attached Files
    Last edited by ChrisW67; 24th January 2011 at 02:57.

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: QComboBox multi-column popup quirks

    In MainWindow constructor you can add:
    Qt Code:
    1. connect(combo, SIGNAL(currentIndexChanged(int)), combo, SLOT(setCurrentIndex(int)));
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to saa7_go for this useful post:

    ChrisW67 (24th January 2011)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QComboBox multi-column popup quirks

    A touch incestuous but it works I was looking at writing a slot to manually do something similar but this is the short version. It still strikes me as odd that the combo box happily displays a value that is not from the model column the combo box is set to use.

    Thanks.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QComboBox multi-column popup quirks

    Quote Originally Posted by ChrisW67 View Post
    Setting the model column for the combo box does not change this.
    Hmm... that's a bit strange since every code path I can see that leads to updating the combo's text goes through this method:
    Qt Code:
    1. QString QComboBox::itemText(int index) const
    2. {
    3. Q_D(const QComboBox);
    4. QModelIndex mi = d->model->index(index, d->modelColumn, d->root);
    5. return d->itemText(mi);
    6. }
    To copy to clipboard, switch view to plain text mode 

    ... which would suggest the column model should be respected.


    Added after 12 minutes:


    I'd say it's a bug in QComboBoxPrivate::setCurrentIndex.

    It says:
    Qt Code:
    1. void QComboBoxPrivate::setCurrentIndex(const QModelIndex &mi)
    2. {
    3. Q_Q(QComboBox);
    4. bool indexChanged = (mi != currentIndex);
    5. if (indexChanged)
    6. currentIndex = QPersistentModelIndex(mi);
    7. if (lineEdit) {
    8. QString newText = q->itemText(currentIndex.row());
    9. if (lineEdit->text() != newText)
    10. lineEdit->setText(newText);
    11. updateLineEditGeometry();
    12. }
    13. if (indexChanged) {
    14. q->update();
    15. _q_emitCurrentIndexChanged(currentIndex);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    and should be:
    Qt Code:
    1. void QComboBoxPrivate::setCurrentIndex(QModelIndex mi)
    2. {
    3. Q_Q(QComboBox);
    4. mi = model->index(mi.row(), modelColumn, mi.parent());
    5. bool indexChanged = (mi != currentIndex);
    6. if (indexChanged)
    7. currentIndex = QPersistentModelIndex(mi);
    8. if (lineEdit) {
    9. QString newText = q->itemText(currentIndex.row());
    10. if (lineEdit->text() != newText)
    11. lineEdit->setText(newText);
    12. updateLineEditGeometry();
    13. }
    14. if (indexChanged) {
    15. q->update();
    16. _q_emitCurrentIndexChanged(currentIndex);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 24th January 2011 at 01:30.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QComboBox multi-column popup quirks

    Indeed, and someone has beaten us to it: http://bugreports.qt.nokia.com/browse/QTBUG-10491

Similar Threads

  1. Related to Popup of the QComboBox
    By ashishsaryar in forum Qt Programming
    Replies: 1
    Last Post: 10th July 2009, 16:29
  2. popup QKeyEvent in QComboBox
    By olosie in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2009, 09:24
  3. QComboBox popup
    By aekilic in forum Qt Programming
    Replies: 9
    Last Post: 6th January 2009, 10:24
  4. Can a QListView support multi-column?
    By wesley in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2008, 08:00
  5. multi level menu popup
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2007, 08:06

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.