Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drawing pixmaps through delegates for qcombobox
Forum Updated to NodeBB v4.3 + New Features

Drawing pixmaps through delegates for qcombobox

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 596 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andaharoo
    wrote on last edited by
    #1

    So I'm trying to add different images to each item of qcombobox by overriding paint functionality. From a hour or so of googling I found I need to subclass a delegate object, override it's paint function, and set that delegate to the combobox. That or use a qicon which didn't work either. I prefer this route though for later flexibility with painting.

    So I use addItem("", MyPixmap) to give the image to the item then when paint is called in the delegate I do:

    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
    	QPixmap image = index.data().value<QPixmap>();
    	painter->drawPixmap(0, 0, option.rect.width(), option.rect.height(), image);
    }
    

    I also tried:

    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
    	QPixmap image = index.data().value<QPixmap>();
    	QApplication::style()->drawItemPixmap(painter, option.rect, 0, image);
    }
    

    Neither works just a blank area is displayed. I should say my image is 1x100 image whose height I would like to be stretched to fill the items container/rect. Any idea what I'm doing wrong?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Try
      QPixmap image = index.data().value<QPixmap>();
      qDebug() <<"image ok" << image.isNull();

      to check if it get a valid pixmap ?

      Anyway,
      the docs for addItem says the second parameter (MyPixmap)
      is stored in Qt::UserRole
      but you read with data with no role and default is
      data(int role = Qt::DisplayRole) const

      so i think you need at least to do
      QPixmap image = index.data(Qt::UserRole).value<QPixmap>();
      to get the pixmap again.

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved