[Solved] Subclassing QListWidgetItem



  • 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!



  • 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.



  • Could you provide a complete, small working example?



  • 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();

    }
    @



  • 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;;
    

    }
    @



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.