Results 1 to 9 of 9

Thread: Resize Pixmap to Fit Cell in QSqlTableModel

  1. #1
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Resize Pixmap to Fit Cell in QSqlTableModel

    Hi, I have a column that displays an image, but it is "cut off" where it is bigger than the cell size. I do not want to make the cell fit the image, just scale down the image, like a thumbnail. I have sub-classed QSqlTableModel and display the image in data() like this:
    Qt Code:
    1. if(header == "Image")
    2. {
    3. QString link = QSqlTableModel::data(idx, Qt::DisplayRole).toString();
    4. QPixmap pixmap(link);
    5.  
    6. switch(role)
    7. {
    8. case Qt::DisplayRole:
    9. return QString();
    10. break;
    11. case Qt::DecorationRole:
    12. return pixmap;
    13. break;
    14. case Qt::UserRole:
    15. return link;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    Is there some way to get the cell size, so I can return something like pixmap.scaled(cellSize), or find the lesser of scaledToHeigth() and scaledToWidth()? Or am I trying to go this in the wrong place?

    I use images in a different column, but I am free to use small enough images that they fit. Ultimately, I plan to use the tool tip to show the image full sized.

  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: Resize Pixmap to Fit Cell in QSqlTableModel

    Quote Originally Posted by admkrk View Post
    ...Or am I trying to go this in the wrong place?...
    Yes

    Use QAbstractItemDelegate
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    admkrk (23rd April 2017)

  4. #3
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize Pixmap to Fit Cell in QSqlTableModel

    Thanks, I am using QStyledItemDelegate for the editor, but I assume it is mostly the same. I also assume I will need to use the paint function.

    At least now I can look in the right direction.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize Pixmap to Fit Cell in QSqlTableModel

    At least now I can look in the right direction.
    The thing that's tricky to understand about the Model / View architecture is that the model doesn't know anything about the view, its sizes, or anything else. You can have the same model displaying the same information in two totally different ways in different views. One of your views might show the pixmap as an icon, another might show it full-sized in a picture browser. It's up to the view to determine how to display the information served up by the model.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #5
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize Pixmap to Fit Cell in QSqlTableModel

    It's up to the view to determine how to display the information served up by the model.
    That makes it sound like I should be doing this with the view. I might be able to do that if I was only dealing with one table. In my case, I might be displaying a table that does not even contain that column.

    I have figured out how to get the height and width from the delegate. Now I just need to figure out how the paint() function works.

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize Pixmap to Fit Cell in QSqlTableModel

    Have you looked at the Star Delegate example?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #7
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize Pixmap to Fit Cell in QSqlTableModel

    Among others, yes.

    It looks like I also need to implement sizeHint(), along with paint(), and that is were I do the scaling. I should be able to figure it out once I start experimenting.

  9. #8
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize Pixmap to Fit Cell in QSqlTableModel

    It looks like sizeHint() is not needed, after all. I have it working, for the most part.
    Qt Code:
    1. void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. QString header = model->headerData(index.column(),
    5. Qt::Horizontal).toString();
    6. if(header == "Image")
    7. {
    8. QString link = index.data().toString();
    9. QPixmap pixmap(link);
    10.  
    11. pixmap = pixmap.scaled(option.rect.size(), Qt::KeepAspectRatio);
    12.  
    13. painter->drawPixmap(option.rect.topRight() / 2, pixmap);
    14. }
    15. else
    16. QStyledItemDelegate::paint(painter, option, index);
    17. }
    To copy to clipboard, switch view to plain text mode 
    The main problem I have now is that it is not quite centered. If my math is right, subtracting half the width of the pixmap should center it. Unfortunately, that is an int and I am not sure how to convert that to a QPoint. Time for more reading.

  10. #9
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize Pixmap to Fit Cell in QSqlTableModel

    That seemed to work, until I went past the first row. Made a small change and it all seems to be working as intended now, just need to check different sized images and figure out why a relative path is not working.
    Qt Code:
    1. void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. QString header = model->headerData(index.column(),
    5. Qt::Horizontal).toString();
    6. if(header == "Image")
    7. {
    8. QString link = index.data().toString();
    9. QPixmap pixmap(link);
    10.  
    11. pixmap = pixmap.scaled(option.rect.size(), Qt::KeepAspectRatio);
    12. int centerCell = option.rect.width() / 2;
    13. int centerPixmap = pixmap.width() / 2;
    14.  
    15. painter->drawPixmap(centerCell - centerPixmap, option.rect.y(),
    16. pixmap);
    17. }
    18. else
    19. QStyledItemDelegate::paint(painter, option, index);
    20. }
    To copy to clipboard, switch view to plain text mode 
    Thanks all.

Similar Threads

  1. Replies: 3
    Last Post: 9th November 2012, 19:55
  2. Replies: 2
    Last Post: 18th July 2011, 01:25
  3. Replies: 1
    Last Post: 19th April 2011, 12:17
  4. Replies: 8
    Last Post: 30th March 2011, 21:06
  5. Replies: 1
    Last Post: 7th December 2009, 19:56

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.