Results 1 to 14 of 14

Thread: QTableView with a column of images

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 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

  2. #2
    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 ?

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 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

  4. #4
    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?

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

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 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..

  7. #7
    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

  8. #8
    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 ?

  9. #9
    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?

  10. #10
    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

  11. #11
    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.

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
  •  
Qt is a trademark of The Qt Company.