Results 1 to 10 of 10

Thread: QAbstractListModel

  1. #1
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QAbstractListModel

    I tried many times, and now i have this declaration:
    class ListModel : public QAbstractListModel
    Qt Code:
    1. {
    2. Q_OBJECT
    3. public:
    4. ListModel(QVector<QStringList> &my2darray, QObject *parent = 0)
    5. : QAbstractListModel(parent), vector(my2darray){}
    6.  
    7. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    8. QVariant data(const QModelIndex &index, int role) const;
    9.  
    10. private:
    11. QVector<QStringList> &vector;
    12. };
    To copy to clipboard, switch view to plain text mode 

    I would like to know, what can i return as QVariant? Only strings? i need to return a custom_widget i created (it consists of 4 labels). I tried this code:
    QVariant ListModel::data(const QModelIndex &index, int role) const
    Qt Code:
    1. {
    2. if (!index.isValid())
    3. return QVariant();
    4.  
    5. if (index.row() >= 20)
    6. return QVariant();
    7.  
    8. if (role == Qt::DisplayRole)
    9. {
    10. int a = index.row();
    11. return Custom_Widget(vector[a][0], vector[a][1], vector[a][2], "");
    12. }
    13. else
    14. return QVariant();
    15. }
    To copy to clipboard, switch view to plain text mode 

    But it didn't worked. How should I alter my code to get it working?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QAbstractListModel

    You can return evertything as a QVariant. See the documentation of QVariant, Q_DECLARE_METATYPE and qRegisterMetaType.

  3. #3
    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: QAbstractListModel

    If your intention is that you are going to show this custom widget in a view in place of the model index for which you return the widget then it will not work. One thing is that your syntax is not correct (you'd have to return a pointer to the widget which would result in an instant memory leak) and second is that the views will simply not understand what you want from them. What is the final result that you want to achieve?
    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.


  4. #4
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAbstractListModel

    OK, so I want to create a List of Widgets. Widgets are custom (I already wrote a class where they can be created). I need to implement this using the MVC principle.
    I try to fill that list with my custom widgets, that are provided by Custom_Widget class (where i can create new custom widgets), but i have no idea how to do it.
    Could someone please tell me, do i need to subclass the QItemDelegate to show my custom widgets in the list, if yes, then how do i get the delegate to show my custom widgets?
    I want to know, if there even is a need to subclass QAbstractListModel.

    I found few examples, where i learned, that it is possible to use QListWidget/QTableWidget and delegates, according to MVC principle (i think one example is Star Rating in demos). If i need to subclass QItemDelegate, do i need to set the Editor, even though i don't need to edit the data that will be shown in the list?

    If you could tell me, i would also want to know in theory what should i do to achieve my task? Example: u need main.cpp, some_delegate.cpp, and custom_widget.cpp. In those files u'll have to implement these things... and so on.

  5. #5
    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: QAbstractListModel

    A "list" is a data model. It can live well wthout a view. Returning widgets from the model rarely makes sense. If you want to show a list of widgets then use QScrollArea instead of the model-view framework.
    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. #6
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAbstractListModel

    The problem is My superior told me to do this one using MVC..

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QAbstractListModel

    The problem is My superior told me to do this one using MVC..
    Told you to do that using Qt Model-View framework, or told you to implement MVC pattern (using Qt) ?

  8. #8
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAbstractListModel

    I think they are the same

  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QAbstractListModel

    I think they are the same
    Not really Design pattern is something abstract, it's an idea - you can have the same design pattern implemented in different programming languages.
    Have you heard about "Gang Of Four" (Gamma, Helm, Johnson, Vlissides) ? I really recommend this book: Design Patterns: Elements of Reusable Object-Oriented Software
    Here is nice explanation of MVC pattern: MVC link

  10. The following user says thank you to stampede for this useful post:

    Archa4 (9th February 2011)

  11. #10
    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: QAbstractListModel

    Quote Originally Posted by Archa4 View Post
    I think they are the same
    Qt ItemViews is not an implementation of MVC. It is based on MVC but lacks the controller component. Thus it is commonly called "model-view framework". I don't know what your superior told you but either you are approaching the problem from a wrong end (basing your ui on widgets instead of something else) or your superior is not aware of the fact that what you are doing can't be done efficiently in Qt's model-view framework. You should have a serious conversation with your superior about it.
    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.


  12. The following user says thank you to wysota for this useful post:

    Archa4 (9th February 2011)

Similar Threads

  1. Problem with QAbstractListModel
    By chandan in forum Newbie
    Replies: 0
    Last Post: 12th April 2010, 01:24
  2. QAbstractListModel problem
    By UltimatePace in forum Qt Programming
    Replies: 5
    Last Post: 27th September 2009, 11:51
  3. Problem with QAbstractListModel
    By eekhoorn12 in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2009, 15:26
  4. QAbstractListModel searching.
    By ComaWhite in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 19:41
  5. QAbstractListModel and icon problem
    By mchrk in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2009, 12:21

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.