Results 1 to 9 of 9

Thread: Displaying Multiple Columns in QListView

  1. #1
    Join Date
    Dec 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Displaying Multiple Columns in QListView

    Hi,

    How do I display mulitple columns in a QListView that is connected to a QSqlTableModel.

    For example I have a the following table:

    player
    -----------------------------
    id | firstname | surname
    -----------------------------

    How do I list the "surname, firstname" in the QListView using models and views?

    QListView has "setModelColumn" function but I can't use both columns.

    Thanks Tim

  2. #2
    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: Displaying Multiple Columns in QListView

    QListView represents a flow of items. It cannot have multiple columns. Use QTreeView or QTableView for multiple columns.
    J-P Nurmi

  3. #3
    Join Date
    Dec 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Displaying Multiple Columns in QListView

    How do I concat the strings from both columns to make one item for the QListView?
    For example concat "surname" and "firstname" to make one item.

    Thanks Tim

  4. #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: Displaying Multiple Columns in QListView

    You have to do it manually, for example using SQL or using a proxy model (see QAbstractProxyModel) that will concatenate two columns into one single column.

  5. #5
    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: Displaying Multiple Columns in QListView

    You might want to use QSqlQueryModel then and do it within query.
    J-P Nurmi

  6. #6
    Join Date
    Dec 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Displaying Multiple Columns in QListView

    Thanks for you help.

    Any ideas how I would do it using QAbstractProxyModel.

    Would I sub-class QAbstractProxyModel and overwrite the data function?

    Thanks Tim

  7. #7
    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: Displaying Multiple Columns in QListView

    Yes, you would And also columnCount() and all the pure abstract methods (but they are trivial to reimplement in your case).

  8. #8
    Join Date
    Dec 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Displaying Multiple Columns in QListView

    Thanks for you help.

  9. #9
    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: Displaying Multiple Columns in QListView

    Does it need to be editable?
    Qt Code:
    1. QSqlQueryModel* model = new QSqlQueryModel(this);
    2. model->setQuery("SELECT firstname || surname AS fullname FROM player"); // concat format depends on dbms
    3. listView->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. Displaying on multiple desktops?
    By nupul in forum Qt Programming
    Replies: 8
    Last Post: 6th May 2006, 18:03
  2. QCheckListItem with multiple columns?
    By ecphora in forum Qt Programming
    Replies: 7
    Last Post: 12th April 2006, 05:01

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.