Results 1 to 3 of 3

Thread: QItemDelegate painting stretched pixmap

  1. #1
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QItemDelegate painting stretched pixmap

    hi, i'm trying to make a custom delegate for a QTableView, the image is displayed, but is ugly because it's streched to the cell size, there is anyway to avoid that? just show the image with normal scale inside the cell.
    this is my paint method:
    Qt Code:
    1. void rating_delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    2.  
    3. int rate = index.data(Qt::DisplayRole).toInt();
    4. if(rate > 0) {
    5. QPixmap *star = new QPixmap(QString("Skin/%1/Sys/rate%2.png").arg(CurrentSkin).arg(rate));
    6. painter->drawPixmap(option.rect, *star, star->rect());
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    And the results:
    6gliskDRKj-b27SF_mMn.png

  2. #2
    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: QItemDelegate painting stretched pixmap

    Quote Originally Posted by aguayro View Post
    hi, i'm trying to make a custom delegate for a QTableView, the image is displayed, but is ugly because it's streched to the cell size, there is anyway to avoid that? just show the image with normal scale inside the cell.
    As the QPainter::drawPixmap() documentation describes:
    Draws the rectangular portion source of the given pixmap into the given target in the paint device.
    Note: The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
    So, determine a rectangle the same size as the pixmap that fits within the available painting rectangle and use that as the first argument. If the available space is smaller than the pixmap then determine the largest rectangle of same aspect ratio that will fit.

    BTW: The QPixmap at line 5 does not need to be on the heap and is a memory leak as it stands.

  3. #3
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QItemDelegate painting stretched pixmap

    Thanks fot he answer.
    Now i can draw the bitmap on screen at real scale, but if i resize the column, the pixmap doesn't hide, its stay drawed with same size, any idea?

Similar Threads

  1. Cannot assign to pixmap during painting
    By fanatos in forum Qt Programming
    Replies: 9
    Last Post: 22nd February 2016, 17:56
  2. Replies: 2
    Last Post: 18th July 2011, 00:25
  3. QItemDelegate for painting QComboBox in QTreeWidget
    By naturalpsychic in forum Qt Programming
    Replies: 7
    Last Post: 23rd May 2011, 15:38
  4. Replies: 2
    Last Post: 24th December 2010, 18:08
  5. Area that can be stretched
    By pippo42 in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2010, 08:46

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.