Results 1 to 3 of 3

Thread: QListView is white

  1. #1
    Join Date
    Dec 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QListView is white

    I have created my own QListModel through deriving from QAbstractListModel.

    MZ_NoteListModel.h
    Qt Code:
    1. #ifndef MZ_NOTELISTMODEL
    2. #define MZ_NOTELISTMODEL
    3.  
    4.  
    5. #include <QAbstractListModel>
    6. #include <QObject>
    7.  
    8. class MZ_NoteListModel : public QAbstractListModel
    9. {
    10. public:
    11. MZ_NoteListModel(QObject *parent = NULL);
    12. QVariant headerData(int section, Qt::Orientation orientation, int role) const ;
    13. int rowCount ( const QModelIndex & parent = QModelIndex() ) const ;
    14. QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const ;
    15. Qt::ItemFlags flags ( const QModelIndex & index ) const ;
    16. bool setData ( const QModelIndex & index, const QMap<int, QVariant> & roles ) ;
    17. QMap<int, QVariant> itemData ( const QModelIndex & index ) const ;
    18.  
    19. private:
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mz_noteListModel.h"
    2. #include <QDebug>
    3.  
    4. MZ_NoteListModel::MZ_NoteListModel(QObject *parent) : QAbstractListModel(parent)
    5. {
    6.  
    7. }
    8.  
    9. QVariant MZ_NoteListModel::headerData(int section, Qt::Orientation orientation, int role) const
    10. {
    11. if (orientation == Qt::Horizontal)
    12. {
    13. if (role == Qt::DisplayRole)
    14. {
    15. return QString("Notes");
    16. }
    17. }
    18. return QVariant();
    19. }
    20.  
    21. int MZ_NoteListModel::rowCount ( const QModelIndex & parent ) const
    22. {
    23. return 3;
    24. }
    25.  
    26.  
    27. QVariant MZ_NoteListModel::data ( const QModelIndex & index, int role ) const
    28. {
    29. if(index.isValid())
    30. {
    31. switch(index.row())
    32. {
    33. case 0:
    34. return QString("0");
    35. break;
    36. case 1:
    37. return QString("1");
    38. break;
    39. case 2:
    40. return QString("2");
    41. break;
    42. default: return QVariant(); break;
    43. }
    44. }
    45. return QVariant();
    46. }
    47.  
    48. bool MZ_NoteListModel::setData ( const QModelIndex & index, const QMap<int, QVariant> & roles )
    49. {
    50. if( index.isValid() )
    51. {
    52. return true;
    53. }
    54. return false;
    55. }
    56.  
    57. Qt::ItemFlags MZ_NoteListModel::flags ( const QModelIndex & index ) const
    58. {
    59. if(index.isValid())
    60. {
    61. return Qt::ItemIsEditable;
    62. }
    63. return 0;
    64. }
    65.  
    66. QMap<int, QVariant> MZ_NoteListModel::itemData ( const QModelIndex & index ) const
    67. {
    68. QMap<int, QVariant> role;
    69. if(index.isValid())
    70. {
    71. role[index.row()] = Qt::DisplayRole;
    72. }
    73. return role;
    74. }
    To copy to clipboard, switch view to plain text mode 

    My Problem is, when I make a QListView and set the model to my MZ_NoteListModel, the View is only white. Nothing, no header, no rows, nothing!!

    Qt Code:
    1. #include <iostream>
    2. #include <QListView>
    3. #include <QApplication>
    4.  
    5. #include "mz_noteListModel.h"
    6.  
    7. int main(int argc, char** argv)
    8. {
    9. QApplication q(argc, argv);
    10.  
    11. MZ_NoteListModel *noteModel = new MZ_NoteListModel();
    12. QListView noteView;
    13. noteView.setModel(noteModel);
    14. noteView.show();
    15.  
    16. return q.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    Please, can anybody help me?

    Thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListView is white

    You are returning some values (all QStrings) in data() and headerData() for every role, but there many different roles like Qt::ForegroundRole, Qt::SizeHintRole or Qt::BackgroundRole where your QStrings does not make sense.
    Your values should be returned only for Qt:isplayRole, for any other role return base class implementation. For example:
    Qt Code:
    1. QVariant MZ_NoteListModel::data ( const QModelIndex & index, int role ) const
    2. {
    3. // check if it is some other role then Qt::DisplayRole
    4. if (role != Qt::DisplayRole)
    5. return QAbstractListModel::data(index, role);
    6. if(index.isValid())
    7. {
    8. switch(index.row())
    9. {
    10. case 0:
    11. return QString("0");
    12. break;
    13. case 1:
    14. return QString("1");
    15. break;
    16. case 2:
    17. return QString("2");
    18. break;
    19. default: return QVariant(); break;
    20. }
    21. }
    22. return QVariant();
    23. }
    To copy to clipboard, switch view to plain text mode 

    And your itemData() method is wrong. QMap<int, QVariant> is a map which assigns role with a value, not item row with role (which doesn't make sense as this method is called for particular index which has particular row).
    I would suggest you not implementing itemData() and setItemData(), just implement data() and setData(). And you have mixed setData with setItemData, because your setData method has setItemData method's arguments, so in fact, you are not overriding any of this methods but creating your own third one. And remember that basic edit role (equivalent to Qt:isplayRole) is Qt::EditRole, for other roles call base class implementation.
    Surely you should get through Model Subclassing Reference chapter in Qt Assistant where everything is explained and check the examples.
    Last edited by faldzip; 28th January 2010 at 16:59.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Dec 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QListView is white

    Oh ok. I didn't know that there are diffrent roles, i must handle diffrently. But now it works. Thanks very much.

Similar Threads

  1. Qt GUI application appears as white in Nokia 5800
    By biswajithit in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 1st February 2010, 10:34
  2. White background in window, getting desperate!
    By dictoon in forum Qt Programming
    Replies: 19
    Last Post: 31st December 2009, 09:35
  3. Printing plot black & white
    By maybe78 in forum Qwt
    Replies: 4
    Last Post: 9th October 2009, 08:39
  4. White Lines in graphicsview
    By nicolas1 in forum Qt Programming
    Replies: 6
    Last Post: 20th November 2008, 06:05
  5. Screen get white on minimize?
    By vishal.chauhan in forum Qt Programming
    Replies: 2
    Last Post: 11th August 2008, 22:22

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.