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. QListWidget: problem with "itemWidget"

QListWidget: problem with "itemWidget"

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 431 Views
  • 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.
  • C Offline
    C Offline
    Combas
    wrote on last edited by
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • C Combas

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

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

      C 1 Reply Last reply
      0
      • JonBJ JonB

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

        C Offline
        C Offline
        Combas
        wrote on last edited by
        #3

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

        JonBJ 1 Reply Last reply
        0
        • C Combas

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

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

          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