Results 1 to 14 of 14

Thread: QTableView with a column of images

  1. #1
    Join Date
    May 2011
    Location
    Paris
    Posts
    9
    Qt products
    Platforms
    Windows

    Default QTableView with a column of images

    I have a QTableView to display some informations of a database in the form of a grid. One of the fields is a path to an image and I would like to display these images in my table.

    I tried something with a delegate, but I'm not really confortable with them and I couldn't get anything working. I also tried something with the role :
    Qt Code:
    1. if index.column() == 4:
    2. if role == QtCore.Qt.DecorationRole:
    3. label = QtGui.QLabel()
    4. path = "path/to/my/picture.jpg"
    5. image = QtGui.QImage(str(path))
    6. pixmap = QtGui.QPixmap.fromImage(image)
    7. label.setPixmap(pixmap)
    8. return label
    To copy to clipboard, switch view to plain text mode 

    This piece of code is inspired by something I found in another forum and that was supposed to work. However it doesn't do anything for me, only slows down the execution of my code.

    Any idea why it's not working ? If you have an example with a delegate I'd appreciate it also!

    Thanks for your attention
    Last edited by elanari; 24th June 2011 at 10:43.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableView with a column of images

    try returning pixmap, an also make sure that image is available in the path

  3. #3
    Join Date
    May 2011
    Location
    Paris
    Posts
    9
    Qt products
    Platforms
    Windows

    Default Re: QTableView with a column of images

    I also tried returning a pixmap with no result. But maybe it is not an issue with the pixmap. I just tried to return a color
    Qt Code:
    1. return QtCore.QVariant(QtGui.QBrush(QtGui.QColor(QtCore.Qt.red)))
    To copy to clipboard, switch view to plain text mode 

    And it didn't change the color of the cells. However it prints something if I do
    Qt Code:
    1. print "test"
    To copy to clipboard, switch view to plain text mode 
    in the condition block.

    Any idea ?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableView with a column of images

    You should be supplying the images / pixmap / color from the view's model, and not from the delegate

  5. #5
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QTableView with a column of images

    Have you tried Qt::BackgroundRole instead of Qt:ecorationRole?

  6. #6
    Join Date
    May 2011
    Location
    Paris
    Posts
    9
    Qt products
    Platforms
    Windows

    Default Re: QTableView with a column of images

    @Rachol : Indeed it makes sense that it did not work with the color ! It works fine with the backgroundRole for the color, so it is a probleme with the pixmap.

    @Santosh : I'm supplying it from my model, I don't have a custom delegate

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableView with a column of images

    post you undated code which returns pixmap..

  8. #8
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QTableView with a column of images

    For BackgroundRole you must always return QBrush. You can use void QBrush::setTexture ( const QPixmap & pixmap ) to set the image use to draw your background. But then it must be a correct size, so it is not drawn couple of times.

    The only reasonable solution would be to subclass QStyledItemDelagate (not QItemDelegate) and reimplement paint method. If you have painted a QWidget before, this should be very straight forward.

    Use setItemDelegateForColumn method to set a delegate on your TableView

  9. #9
    Join Date
    May 2011
    Location
    Paris
    Posts
    9
    Qt products
    Platforms
    Windows

    Default Re: QTableView with a column of images

    I'm trying something with a delegate but so far I got nothing, maybe you can help me to get this straight.
    I created a custom delegate inheriting QStyledItemDelegate :
    Qt Code:
    1. class ImageDelegate(QtGui.QStyledItemDelegate):
    2.  
    3. def __init__(self, parent):
    4. QtGui.QStyledItemDelegate.__init__(self, parent)
    5.  
    6. def paint(self, painter, option, index):
    7. # Get Item Data
    8.  
    9. path = "path\to\my\image.jpg"
    10.  
    11. style = QtGui.QApplication.style()
    12. opt.rect = option.rect
    13. pixmap = QtGui.QPixmap(path)
    14.  
    15. painter.drawPixmap(opt.rect, pixmap)
    To copy to clipboard, switch view to plain text mode 

    And I call it like this :
    Qt Code:
    1. self.delegateImage = ImageDelegate(self)
    2. self.setItemDelegateForColumn(2, self.delegateImage)
    To copy to clipboard, switch view to plain text mode 

    And I am sure the path is correct

    What am I missing ?

  10. #10
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QTableView with a column of images

    Okay, I am not a specialist on python, couldn't even call myself a beginner. However I think you don't need this:
    Qt Code:
    1. style = QtGui.QApplication.style()
    2. opt.rect = option.rect
    To copy to clipboard, switch view to plain text mode 

    Just use option.rect directly.

    Appart from that there is nothing I could say is wrong or unnecessary. Have you tried to fill option.rect with some color?

  11. #11
    Join Date
    May 2011
    Location
    Paris
    Posts
    9
    Qt products
    Platforms
    Windows

    Default Re: QTableView with a column of images

    Filling the cells with a color works fine :

    Qt Code:
    1. painter.fillRect(option.rect, QtGui.QColor(191,222,185))
    To copy to clipboard, switch view to plain text mode 

    But still no changes with the pixmap

  12. #12
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QTableView with a column of images

    The there are only 2 things can go wrong:

    1. Your pixmap is not loaded(wrong path)
    2. Your pixmap is not scalled to the size of the area you are drawing into.

  13. #13
    Join Date
    May 2011
    Location
    Paris
    Posts
    9
    Qt products
    Platforms
    Windows

    Default Re: QTableView with a column of images

    Well I got it working.
    You were right, there was a problem in my path. I tried to rescale the pixmap and only then it said that it was null. So I put an image in my code folder and it displays it. The thing is that the path is correct, but it couldn't access it. Maybe it's due to the fact that's on another drive. And that's a different problem !

    Thank you for your time !

    I'm just posting my working piece of code as a reminder :

    Qt Code:
    1. class ImageDelegate(QtGui.QStyledItemDelegate):
    2.  
    3. def __init__(self, parent):
    4. QtGui.QStyledItemDelegate.__init__(self, parent)
    5.  
    6. def paint(self, painter, option, index):
    7.  
    8. painter.fillRect(option.rect, QtGui.QColor(191,222,185))
    9.  
    10. path = "image.jpg"
    11.  
    12. image = QtGui.QImage(str(path))
    13. pixmap = QtGui.QPixmap.fromImage(image)
    14. pixmap.scaled(50, 40, QtCore.Qt.KeepAspectRatio)
    15. painter.drawPixmap(option.rect, pixmap)
    To copy to clipboard, switch view to plain text mode 


    Added after 21 minutes:


    About the path : I just needed to double the backslashes, everything's perfect now
    Last edited by elanari; 27th June 2011 at 13:26.

  14. #14
    Join Date
    Feb 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView with a column of images

    Hi,
    We are working with the Browser developed using 4.5 and we are trying to read XML files from the internet and trying to show the images that comes in the RSS feeds along with the title and link to the article.

    Except for the image all the data is visible on the label in the QT Window Application form. Even the console window of MSVC2008 also shows no error.

    Searched the net for answers and could not find any.

    Any help in this regard would be highly appreciated.

    Thanks
    Sunil M

Similar Threads

  1. how to disbale a column in a QTableView
    By rdjenner in forum Qt Programming
    Replies: 8
    Last Post: 14th August 2019, 15:44
  2. QTableView set column Decimals
    By ottoshmidt in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2010, 05:00
  3. Currently sorted column in QTableView
    By borges in forum Newbie
    Replies: 1
    Last Post: 21st September 2007, 16:52
  4. Fixating column from QTableView ?
    By pinktroll in forum Qt Programming
    Replies: 2
    Last Post: 4th September 2007, 10:53
  5. QTableView without the first counter column?
    By pmaktieh.sirhc in forum Qt Programming
    Replies: 2
    Last Post: 4th January 2007, 22:03

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.