Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QListWidget: problem with "itemWidget"



  • I try to reach a widget in a QListWidget, but the method "itemWidget" returns nothing, whereas the method "item" works correctly.
    Extract of the code:

    QWidget *currentWidget = nullptr;
        QListWidgetItem *currentItem = nullptr;
        QFont fontWidget;
        int numItem;
    
        numItem = listWidgetMain->currentRow();
    
        if ((numItem >= 0) && (numItem < listWidgetMain->count()))
        {
            currentItem = new QListWidgetItem();
            currentWidget = new QWidget();
            // The item is correctly selected
            currentItem = listWidgetMain->item(numItem);
            // The problem is here: the widget is not returned
            currentWidget = listWidgetMain->itemWidget(currentItem);
            fontWidget = currentWidget->font();
        }
    

    The "currentItem" is ok, whereas the "currentWidget" is empty after the call to "itemWidget", and I don't understand why...
    Could someone explain me:

    • Why it does not work
    • How to get the Widget address

    The complete code is:

    • mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QListWidget>
    #include <QVBoxLayout>
    #include <QPushButton>
    #include <QWidget>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    public slots:
        void Test();
    
    private:
        QWidget *centralWidget;
        QListWidget *listWidgetMain;
        QVBoxLayout *vertLayoutMain;
        QPushButton *BT_test;
    };
    #endif // MAINWINDOW_H
    
    
    • mainwindow.cpp:
    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        int numLine;
    
        // QListWidget object
        listWidgetMain = new QListWidget();
        for (numLine = 0; numLine < 5; numLine++)
        {
            listWidgetMain->addItem("text_" + QString::number(numLine + 1));
        }
    
        // Test button
        BT_test = new QPushButton("Test");
        QObject::connect(BT_test, SIGNAL(clicked()), this, SLOT(Test()));
    
        // Layout with the list built
        vertLayoutMain = new QVBoxLayout();
        vertLayoutMain->addWidget(listWidgetMain);
        vertLayoutMain->addWidget(BT_test);
    
        // Main widget
        centralWidget = new QWidget();
        setCentralWidget(centralWidget);
        centralWidget->setLayout(vertLayoutMain);
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::Test()
    {
        QWidget *currentWidget = nullptr;
        QListWidgetItem *currentItem = nullptr;
        QFont fontWidget;
        int numItem;
    
        numItem = listWidgetMain->currentRow();
    
        if ((numItem >= 0) && (numItem < listWidgetMain->count()))
        {
            currentItem = new QListWidgetItem();
            currentWidget = new QWidget();
            // The item is correctly selected
            currentItem = listWidgetMain->item(numItem);
            // The problem is here: the widget is not returned
            currentWidget = listWidgetMain->itemWidget(currentItem);
            fontWidget = currentWidget->font();
        }
    }
    

    Thank you in advance for your help!
    Laurent



  • @Combas said in QListWidget: problem with "itemWidget":

        // The problem is here: the widget is not returned
        currentWidget = listWidgetMain->itemWidget(currentItem);
    

    What widget do you think should be returned? You obviously do not have any widget there, else it would be!

            currentItem = new QListWidgetItem();
            currentWidget = new QWidget();
    

    I don't know what you are (intending to) doing here? You don't don't use these two newed items, so they are just a memory leak.

    You must have something conceptually wrong. If you never put your widget into the list you'll never get it back from it. You have no setItemWidget() anywhere, so you're not.

    P.S.
    If you think

     listWidgetMain->addItem("text_" + QString::number(numLine + 1));
    

    adds a widget it does not. It only adds an item. And unless you have some use case, you should not be using setItemWidget() or accessing itemWidget() anyway.



  • @JonB Thank you for your quick and clear answer.
    Indeed, I thought that the "addItem" method added a widget... That was my mistake!



  • @Combas
    Items are not widgets; they can be made to hold widgets (setItemWidget()), but don't do so unless you make them do so. If you can, try to stay away from putting any widgets of your own into tables/lists, they are "expensive". You don't need to unless it's some special case, normally just work with the items it naturally uses.


Log in to reply