Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    Mobile and Embedded
    3
    4
    2781
    Loading More Posts
    • 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
      mmesarina last edited by

      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 Reply Quote 0
      • G
        giesbert last edited by

        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 Reply Quote 0
        • M
          mmesarina last edited by

          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 Reply Quote 0
          • G
            goetz last edited by

            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 Reply Quote 0
            • First post
              Last post