Results 1 to 5 of 5

Thread: Using images in QComboBox

  1. #1
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Using images in QComboBox

    Hi,

    What is the correct way to get a combobox to display images?
    (The images are 120x90)

    Do I need to create an ItemModel (derived from QAbstractItemModel) and then use QComboBox::setModel() ?

    Or is there an easier way?

    Thanks

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using images in QComboBox

    you can try to create your own delegate.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using images in QComboBox

    so, you should get something like this
    Qt Code:
    1. void PixmapDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. const QPixmap pixmap = qVariantValue<QPixmap>(index.data(Qt::UserRole));
    4. painter->drawPixmap(option.rect, pixmap);
    5. QItemDelegate::paint(painter, option, index);
    6. }
    7.  
    8. QSize PixmapDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    9. {
    10. Q_UNUSED(option);
    11. const QPixmap pixmap = qVariantValue<QPixmap>(index.data(Qt::UserRole));
    12. return pixmap.size();
    13. }
    14. ...
    15. QComboBox *cb = new QComboBox();
    16. cb->setItemDelegate(new PixmaDelegate(cb));
    17. for (int i = 0; i < 10; ++i) {
    18. QPixmap pixmap(100, 100);
    19. pixmap.fill(QColor(qrand() % 255, qrand() % 255, qrand() % 255));
    20. cb->addItem(QString("item%1").arg(i), pixmap);
    21. }
    22. QVBoxLayout *vbl = new QVBoxLayout(this);
    23. vbl->addWidget(cb);
    24. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. The following user says thank you to spirit for this useful post:

    jonks (8th December 2009)

  5. #4
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using images in QComboBox

    Thanks - I tried that but it does no give me the exact effect I need because when using it the current selection is not rendered as an image.

    However, using:-

    1) QAbstractItemModel and calling QComboBox::setIconSize to ensure the images are not scaled.

    and 2) QItemDelegate for the drop list, I get the exact effect I need
    Last edited by jonks; 8th December 2009 at 14:30.

  6. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using images in QComboBox

    reimplement QItemDelegate::drawFocus. you need write something like this
    Qt Code:
    1. void PixmapDelegate::drawFocus ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect ) const
    2. {
    3. Q_UNUSED(painter);
    4. Q_UNUSED(option);
    5. Q_UNUSED(rect);
    6. //nothig to do
    7. }
    To copy to clipboard, switch view to plain text mode 
    IMHO, you don't need any custom model for this task.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Loading corrupt jpeg images with QImage
    By mikeee7 in forum Qt Programming
    Replies: 15
    Last Post: 3rd December 2010, 02:59
  2. Moving images and change their DIP
    By yazwas in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2009, 18:46
  3. QScrollArea not displaying large number of images
    By lpkincaid in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2009, 10:58
  4. import large number of images
    By sriluyarlagadda in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2008, 11:26
  5. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 20:25

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.