Results 1 to 7 of 7

Thread: QTableView with icons in rows

  1. #1
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableView with icons in rows

    I have a QTableView showing rows of a database table. In this table I have a column called data type and I have icon images for each type. How can I add these icons in front of each data type?

    p.s.-
    I found a thread discussing a topic much similar to this. Link ->http://www.qtcentre.org/threads/9655-QTableView-icons
    But I'm afraid I didn't quite understand what it said. So if someone can explain this a little bit further, it is much appreciated. Thank you.

  2. #2
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView with icons in rows

    I would use a QStyledItemDelegate

    If you subclass it you can reimplement the paint Method to paint the icon.

  3. #3
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView with icons in rows

    This is my question too, but the QSqlTableModel ignores every role other than DisplayRole and EditRole. why?
    Impossible = I'mpossible

  4. #4
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView with icons in rows

    If you have a look at the Qt Namespace doc you will see that DisplayRole and EditRole are the same. How would a QSqlTableModel now which data to save as DecorationRole or whatever? You have to do that yourself, either by setting that data yourself or by using a Delegate to paint the Icons.

  5. #5
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView with icons in rows

    either by setting that data yourself
    but the output of setData in the following code is false:
    Qt Code:
    1. ui->tblv_table->model()->setData( ui->tblv_table->model()->index(row, col, QModelIndex() ),
    2. QIcon("icon.png"), Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 
    tblv_table is a QTableView
    Impossible = I'mpossible

  6. #6
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView with icons in rows

    You could try setIconSize
    Otherwise you will have to try a QStyledItemDelegate, I guess

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView with icons in rows

    Quote Originally Posted by .:saeed:. View Post
    but the output of setData in the following code is false:
    Qt Code:
    1. ui->tblv_table->model()->setData( ui->tblv_table->model()->index(row, col, QModelIndex() ),
    2. QIcon("icon.png"), Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 
    tblv_table is a QTableView
    It is false because the underlying generic SQL database has nowhere to store the image that you have set as the Decoration role. You can either use a delegate on the view to draw the relevant icon or you could subclass QSqlTableModel and override data() to return appropriate icons when the DisplayRole is requested by the view (and pass other requests on to the base class).

Similar Threads

  1. Disable row/rows in QTableView
    By scott_hollen in forum Qt Programming
    Replies: 6
    Last Post: 28th March 2011, 18:16
  2. Embed rows of icons in a QTreeView?
    By joeld42 in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2010, 08:36
  3. Color the rows in QTableView
    By grub87 in forum Qt Programming
    Replies: 11
    Last Post: 17th June 2009, 20:41
  4. QTableView icons
    By radu_d in forum Qt Programming
    Replies: 4
    Last Post: 16th October 2007, 22:02
  5. Copying QTableView rows
    By derrickbj in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2006, 00:00

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.