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. [Solved] Subclassing QListWidgetItem

[Solved] Subclassing QListWidgetItem

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 10.5k 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.
  • R Offline
    R Offline
    reactive
    wrote on last edited by
    #1

    I'd like to subclass QListWidgetItem to store some extra data with each item.
    I'm aware of setData(role, value), but I'd like to use a subclass.
    I'm overriding data(role), but the items aren't being added to the QListWidget.
    I can see debug statements containing the right text and if I change the
    code from my subclass to a standard item it works fine.

    @// MyItem* item = new MyItem(...)
    QListWidgetItem* item = new QListWidgetItem("test");
    ui->list->addItem(item);@

    I thought overriding data(role) would be sufficient, but I guess not.
    What am I doing wrong? TIA!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      What you could be doing wrong (but we can not check, as you did not show much code), is that you forgot the const modifier for your reimplemented data method.

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

        Could you provide a complete, small working example?

        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
        • R Offline
          R Offline
          reactive
          wrote on last edited by
          #4

          Sorry, I was on my way out and the code was on a different machine. Here is an example.
          Still, the code is as I described. A naive subclass of QListWidgetItem that overrides data(role).
          You can see the debug messages in data() print (so it's getting called frequently), but there is no item in the list.

          @
          #include <QtGui/QApplication>
          #include <QListWidget>
          #include <QListWidgetItem>
          #include <QString>
          #include <QDebug>

          class Foo {
          public: QString text;
          };

          class MyListItem : public QListWidgetItem {

          public:
          MyListItem(const Foo& f) : f(f) {}
          virtual QVariant data(int role) const {
          qDebug() << "you can see this print out";
          return f.text;
          }

          private:
          Foo f;

          };

          int main(int argc, char *argv[]){

          QApplication a(argc, argv);

          Foo f;
          f.text = "why doesn't this dislay?";
          MyListItem item (f);
          QListWidget widget;
          widget.addItem(&item);
          widget.show();

          return a.exec();

          }
          @

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

            There are some bugs in your code:

            first of all, virtual functions schould not be inline.
            second, your data function returns a string for each role, but you only need it for the display role.
            so this code works:

            @

            #include <QtGui/QApplication>
            #include <QListWidget>
            #include <QListWidgetItem>
            #include <QString>
            #include <QDebug>

            class Foo {
            public: QString text;
            };

            class MyListItem : public QListWidgetItem
            {
            public:
            MyListItem(const Foo& f) : f(f) {}
            virtual QVariant data(int role) const;

            private:
            Foo f;

            };

            QVariant MyListItem::data(int role) const
            {
            if(Qt::DisplayRole == role)
            {
            qDebug() << "you can see this print out";
            return f.text;
            }
            return QListWidgetItem::data(role);
            }

            int main(int argc, char *argv[])
            {
            QApplication a(argc, argv);

            Foo f;
            f.text = "why doesn't this dislay?";
            MyListItem item (f);
            // QListWidgetItem item ("why doesn't this dislay?");
            QListWidget widget;
            widget.addItem(&item);
            widget.show();
            
            return a.exec&#40;&#41;;
            

            }
            @

            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
            • R Offline
              R Offline
              reactive
              wrote on last edited by
              #6

              That was just an SSCCE. In my dev code the virtual function isn't inlined. Also, it was checking for Qt::DisplayRole, but was returning a "null" QVariant using the default constructor. When trying to figure out why it wasn't working, I changed it to some other text like this with no luck:

              @switch (role) {
              case Qt::DisplayRole: return foo;
              //default: return QVariant();
              default: return "still nothing???"
              }@

              I used the same code (returning QVariant() for the default case) in the data(role) method of my QAbstractTableModel and it always worked fine. Apparently you can't do that with QListWidgetItem.

              Thank you so much. I should have been delegating to the parent class instead of returning garbage.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                Next time, please post real code. That saves people looking at it time pointing out errors that don't turn out to be in the problematic, real code.

                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