Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to enter and read an Icon image into/from a ListWidget data model with a Delegate
Forum Updated to NodeBB v4.3 + New Features

How to enter and read an Icon image into/from a ListWidget data model with a Delegate

Scheduled Pinned Locked Moved Mobile and Embedded
4 Posts 3 Posters 3.1k 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.
  • M Offline
    M Offline
    mmesarina
    wrote on last edited by
    #1

    Hi,
    I am using a Delegate to retrieve data from a ListWidget data model to be able to control how I display the data in the ListWidgetItem items. My code compiles fine but when I debug the Delegate I can see that the retrieved Icon points to "null". So it seems that the Icon is not being found in the model. I wonder if someone can tell what the problem is. Here are the snippets of the code that are revelant. Thank you!

    In MainWindow construtor: I enter the Icon into the data model of ListWidget

    @
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QVBoxLayout *hlayout = new QVBoxLayout();
    QListWidget *myListWidget = new QListWidget();
    // Setting Delegate
    myListWidget->setItemDelegate(new ListDelegate(myListWidget));

    QListWidgetItem *item = new QListWidgetItem();
    QPixmap pixmap(":/images/arrow.png");
    QIcon ic(pixmap);
    QVariant v(QMetaType::QIcon, &ic);

    //Add Icon to model with custom role
    item->setData(Qt::UserRole + 3, v);

    // Add strings
    item->setData(Qt::DisplayRole, "Wine");
    //item->setData(Qt::UserRole + 1, "Description");
    myListWidget->addItem(item);

    // BEER
    QListWidgetItem *item2 = new QListWidgetItem();
    item2->setData(Qt::DisplayRole, "Beer");
    myListWidget->addItem(item2);
    
    
    myListWidget->setLayout(hlayout);
    setCentralWidget(myListWidget);
    

    @

    In the Delegate: Read Icon and display in ListWidget--

    @
    void ListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    QRect r = option.rect; //rectangle of listitem

    // THE FOLLOWING FUNCTION RETURNS A NULL POINTER for ic
    QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::UserRole + 3)));
    r = option.rect.adjusted(5, 10, -10, -10);
    ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignRight);
    }

    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      you are casting the cariant to a p9xmap, but it contains an icon.
      so add a pixmap in the c'tor or get a qicon out of the variant.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mmesarina
        wrote on last edited by
        #3

        That is so funny, how just telling others about your bug, helps you find the solution. I fiddled some more and realized what you just said so I fixed it. Thanks Gerolf for confirming.

        So I replaced this line in the Delegate:
        @
        QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::UserRole + 3)));
        @

        With:
        @

        QVariant v = index.data(Qt::UserRole + 3);
        QIcon ic = v.value<QIcon>();@

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          And instead of

          @
          QVariant v(QMetaType::QIcon, &ic);
          @

          it is ok to write (and you actually should do this):

          @
          QVariant v(ic);
          @

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0

          • Login

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