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. Instantiate widgets with a list of check boxes and QPair
Forum Updated to NodeBB v4.3 + New Features

Instantiate widgets with a list of check boxes and QPair

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 377 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.
  • D Offline
    D Offline
    diego-qt
    wrote on last edited by diego-qt
    #1

    Hello, I would like to create a widget with a list of QCheckBox, which the user can choose from, and a Start button that instantiates and shows other windows (widgets) according to the checked boxes, knowing that all these widgets are different classes.

    Yesterday I found out about the existence of QPair and I had the idea to pair a check box and a null instantiation of a widget, e.g.:

    QVector<QPair<QCheckBox*, QWidget*>> vectorOptions;
    vectorOptions << qMakePair(new QCheckBox("Option 1"), (WidgetChild1*) nullptr);
    vectorOptions << qMakePair(new QCheckBox("Option 2"), (WidgetChild2*) nullptr);
    

    The idea is to connect a signal produced when the Start button is pressed, to the slot handleStartButton(). The problem is that I can't instantiate a QWidget pointer in QPair::second and I don't understand why.

    void Class::handleStartButton()
    {
        QVectorIterator<QPair<QCheckBox*, QWidget*>> itPairVector(vectorOptions);
        QPair<QCheckBox*, QWidget*> current;
    
        while(itPairVector.hasNext())
        {
            current = itPairVector.next();
            if(current.first->isChecked() && current.second == nullptr)
            {
                WidgetChild1 *tmp = new WidgetChild1;
                current = qMakePair(current.first, tmp);
                //current = qMakePair(current.first, new WidgetChild1);
                //line above would be equal to the two previous ones
                qDebug() << "in if: second initialized?" << current.second;
            }
        }
    
        QVectorIterator<QPair<QCheckBox*, QWidget*>> itPairVector2(vectorOptions);
        while(itPair2.hasNext())
        {
            qDebug() << "after: second initialized?" << itPair2.next().second;
        }
    }
    

    With this code, I am able to instantiate a WidgetChild1 object, show it and the debug prints something like

    in if: second initialized? WidgetChild1(0x55562182c540)
    

    However, I have the warning Potential leak of memory pointed to by field 'second' at the end of the first while loop, which means that the widgets do not stick to QPair::second.
    I can verify this with the second while loop: if QPair:second was correctly initialized, it would show something similar to the first debug, but it prints

    after: second initialized? QWidget(0x0)
    after: second initialized? QWidget(0x0)
    

    so it still points to nullptr.

    I would appreciate some information as to how I can correctly instantiate the widget.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @diego-qt said in Instantiate widgets with a list of check boxes and QPair:

      current = itPairVector.next();

      This creates a copy of your QPair.
      Use a range-based for loop:

      for (auto &current : vectorOptions)
          {
              if(current.first->isChecked() && current.second == nullptr)
              {
                  current.second = new WidgetChild1;
              }
          }
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • D Offline
        D Offline
        diego-qt
        wrote on last edited by
        #3

        Thank you very much! This worked instantly. I see that I was not enough documented about QPair. I will dig into the range-based loops because I didn't know about them.
        However I have a warning unused variable 'current'. It doesn't make sense to me because I use it in the loop. Do you know why this happens?

        Christian EhrlicherC 1 Reply Last reply
        0
        • D diego-qt

          Thank you very much! This worked instantly. I see that I was not enough documented about QPair. I will dig into the range-based loops because I didn't know about them.
          However I have a warning unused variable 'current'. It doesn't make sense to me because I use it in the loop. Do you know why this happens?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @diego-qt said in Instantiate widgets with a list of check boxes and QPair:

          Do you know why this happens?

          You did not remove your variable current which you defined (for unknown reasons) before your while loop.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • D Offline
            D Offline
            diego-qt
            wrote on last edited by
            #5

            Yep, sorry. I didn't erase because I thought it was necessary but it is already declared in the for loop. Thanks!

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved