Results 1 to 7 of 7

Thread: Readonly Model implementation requirements for QComboBox

  1. #1
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Readonly Model implementation requirements for QComboBox

    Hi @all,

    I'm implementing a model by sub classing QAbstractTableModel.
    The model is read-only.
    I did implement the methods rowCount(), columnCount() and data().

    Using this model with a QComboBoxcauses one problem: The listview is not popping up.

    I was already walking through the QComboBox implementation in order to see what might cause this - but I have absolutely NO idea ....

    Anyone an idea???

    THX alot

  2. #2
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Readonly Model implementation requirements for QComboBox

    Hi, could you check if i.e. QTableView shows some contents? If QTableView or ListView is also empty, post code of your inherited model.

    Oh... one more thing - make sure you have setModel for comboBox...

  3. #3
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Readonly Model implementation requirements for QComboBox

    The content is loaded to the combo box, because I see the content combobox and
    by pressing the up and down arrows I can navigate through the items.

    The only trouble is the listview is not popping up.

  4. #4
    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: Readonly Model implementation requirements for QComboBox

    Did you subclass QComboBox? Did you reimplement any of its event handlers or so? I made a simple test and it works fine for me:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class TableModel : public QAbstractTableModel
    4. {
    5. public:
    6. TableModel(int rows, int cols, QObject* parent = 0)
    7. : QAbstractTableModel(parent), rows(rows), cols(cols) { }
    8.  
    9. int rowCount(const QModelIndex& parent = QModelIndex()) const { return rows; }
    10. int columnCount(const QModelIndex& parent = QModelIndex()) const { return cols; }
    11.  
    12. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
    13. {
    14. if (index.isValid() && role == Qt::DisplayRole)
    15. return QString("%1,%2").arg(index.row()).arg(index.column());
    16. return QVariant();
    17. }
    18.  
    19. private:
    20. int rows;
    21. int cols;
    22. };
    23.  
    24. int main(int argc, char *argv[])
    25. {
    26. QApplication app(argc, argv);
    27. QComboBox combo;
    28. combo.setModel(new TableModel(5, 2, &combo));
    29. combo.show();
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    DeepDiver (8th November 2007)

  6. #5
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Readonly Model implementation requirements for QComboBox

    Thanks for the small demo code.

    I had a bug in the data() implementation where I did not pay enough attention to the role.

    THX

  7. #6
    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: Readonly Model implementation requirements for QComboBox

    Quote Originally Posted by DeepDiver View Post
    Thanks for the small demo code.
    You're welcome
    I had a bug in the data() implementation where I did not pay enough attention to the role.
    I'm curious. What kind of bug? The side effect (even if data is shown in table, combo popup is not shown) is confusing..
    J-P Nurmi

  8. #7
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Readonly Model implementation requirements for QComboBox

    I was always returning the cell data.
    Something like:

    Qt Code:
    1. if (!index.isValid())
    2. return QVariant();
    3. if (role == Qt::DecorationRole)
    4. return QIcon(/* */);
    5. switch(index.column())
    6. {
    7. case 0: return "col0";
    8. case 1: return "col1";
    9. case 2: return "col2";
    10. ...
    11. }
    12. return QVariant();
    To copy to clipboard, switch view to plain text mode 

    What does that mean?

    I return a string for the role Qt::SizeHintRole - which causes each item to have a size of 0.
    That's why the list view has a total height of 0 as well.



    Again THX a lot!

  9. The following user says thank you to DeepDiver for this useful post:

    jpn (8th November 2007)

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.