Results 1 to 4 of 4

Thread: QList<Object>into QListView or QTree

  1. #1
    Join Date
    Jul 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default QList<Object>into QListView or QTree

    Hello everyone.

    Since I'm a newbie to QT, I would like to hear if someone can guide or help me to explain the issue I have.
    What I need is
    I have a class of user, which I creating an object with such as:
    myuser = new user(id, name, firstname, lastname);

    and I'm appending it into a

    QList<users> *userList;
    userList.append(myuser);


    The question is

    I'm creating few dummy users and I want to show them in QListView.

    Since I'm new to the QT, I read that I need to create a model to show the objects(users) in to the view.
    I couldn't find anywhere some good examples that I can follow.
    So what I have to do to show the users in QListView or QTable/Tree view?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList<Object>into QListView or QTree

    You might have an easier start with a QListWidget, QTableWidget or QTreeWidget.
    These allow you to create "items" objects for each entry instead of using a model.
    Model's tend to be a bit more complicated.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QList<Object>into QListView or QTree

    thx for replying back.

    I have done the list widget and there no problem with that.
    Its working good but I want to use the QListView aswell (learning porpuse).

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList<Object>into QListView or QTree

    I see.

    Then your next step is to create a subclass of QAbstractListModel and somehow given that object access to your QList.
    You will have to implement QAbstractItemModel::rowCount() and QAbstractItemModel::data().

    In rowCount() you can ignore the function's argument since you are only implementing a list (argument only needed for trees).

    In data() you only need the row information from the index argument.
    You can also just return a default constructed QVariant for all roles that are not Qt::DisplayRole

    Cheers,
    _

Similar Threads

  1. how to add check box inside qtree widget?
    By aurora in forum Qt Programming
    Replies: 4
    Last Post: 25th May 2012, 01:16
  2. Signal from QList Object to Slot from parent
    By Rocken in forum Qt Programming
    Replies: 4
    Last Post: 9th April 2012, 14:34
  3. Chancing values to a QList<object>
    By terhje in forum Newbie
    Replies: 2
    Last Post: 19th May 2011, 12:01
  4. QList & QPointer to store object list
    By maddog_fr in forum Qt Programming
    Replies: 12
    Last Post: 8th August 2009, 20:39
  5. Qtree in C++
    By rrrrcem in forum General Programming
    Replies: 3
    Last Post: 12th July 2008, 22:39

Tags for this Thread

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.