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. adding widgets to UI on the fly

adding widgets to UI on the fly

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 842 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by mzimmers
    #1

    Hi, all -

    Amazingly, I don't think I've ever done this before, and my first effort is not working.

    I need to populate a UI with radio buttons based on the results of a message I receive. I need one radio button for each element in the list I build from the message. (My UI is an empty QDialog.) Here's what I've tried:

    void BootPartition::displayPartitions(QString qsLabels)
    {
        QRadioButton labelButtons[3];
        int x = 100;
        int y = 100;
        int w = 100;
        int h = 20;
    
        QStringList qsl = qsLabels.split(QChar(PART_LABEL_DELIMITER));
        for (int i = 0; i < qsl.size(); ++i)
        {
            labelButtons[i].setText(qsl[i]);
            labelButtons[i].setGeometry(x, y, w, h);
            labelButtons[i].setVisible(true);
            y += 30;
        }
    }
    

    What might I be doing wrong (or not at all)? Thanks.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      QRadioButton labelButtons[3];

      • it's on the stack so they will be deleted as soon as the function ends
      • you don't add them to a layout/re-parent them so each of them is an independent window as of now
      • you just have a 3 elements array while qsl.size() could be >3
      • to make them work as mutually exclusive you probably also want to add them to a QButtonGroup

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      6
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        Thanks, VRonin. It's still early here, so I guess I'm still waking up. Your first two points were the cause of my problem; to your third point, I'll get rid of the magic numbers once I have the essential functionality down.

        1 Reply Last reply
        1
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by mzimmers
          #4

          For anyone keeping score at home, here's an updated code snippet. I don't claim it's the ideal way to go, but it works and is (hopefully) easily understood. Not sure my destructor is quite right, so feel free to comment on it.

          void BootPartition::displayPartitions(QString qsLabels, QString qsCurrentLabel)
          {
              QString text;
              QFont font;
              int x = 50;
              int y = 60;
              int w = 200;
              int h = 20;
          
              QStringList qsl = qsLabels.split(QChar(PART_LABEL_DELIMITER));
              for (int i = 0; i < qsl.size(); ++i)
              {
                  m_labelButtons.push_back(new QRadioButton(this));
                  m_labelButtons[i]->setText(qsl[i]);
                  m_labelButtons[i]->setGeometry(x, y, w, h);
                  if (m_labelButtons[i]->text() == qsCurrentLabel)
                  {
                      font = m_labelButtons[i]->font();
                      font.setBold(true);
                      m_labelButtons[i]->setFont(font);
                      text = m_labelButtons[i]->text() + " (current default)";
                      m_labelButtons[i]->setText(text);
                  }
                  m_labelButtons[i]->setVisible(true);
                  y += 20;
              }
          }
          ...
          // the below has been changed from the original posting.
          BootPartition::~BootPartition()
          {
              qDeleteAll(m_labelButtons);
              m_labelButtons.clear();
              delete ui;
          }
          
          mrjjM J.HilkJ 2 Replies Last reply
          0
          • mzimmersM mzimmers

            For anyone keeping score at home, here's an updated code snippet. I don't claim it's the ideal way to go, but it works and is (hopefully) easily understood. Not sure my destructor is quite right, so feel free to comment on it.

            void BootPartition::displayPartitions(QString qsLabels, QString qsCurrentLabel)
            {
                QString text;
                QFont font;
                int x = 50;
                int y = 60;
                int w = 200;
                int h = 20;
            
                QStringList qsl = qsLabels.split(QChar(PART_LABEL_DELIMITER));
                for (int i = 0; i < qsl.size(); ++i)
                {
                    m_labelButtons.push_back(new QRadioButton(this));
                    m_labelButtons[i]->setText(qsl[i]);
                    m_labelButtons[i]->setGeometry(x, y, w, h);
                    if (m_labelButtons[i]->text() == qsCurrentLabel)
                    {
                        font = m_labelButtons[i]->font();
                        font.setBold(true);
                        m_labelButtons[i]->setFont(font);
                        text = m_labelButtons[i]->text() + " (current default)";
                        m_labelButtons[i]->setText(text);
                    }
                    m_labelButtons[i]->setVisible(true);
                    y += 20;
                }
            }
            ...
            // the below has been changed from the original posting.
            BootPartition::~BootPartition()
            {
                qDeleteAll(m_labelButtons);
                m_labelButtons.clear();
                delete ui;
            }
            
            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi
            what about m_labelButtons.clear() ?
            if you want to delete the pointers in the list.
            Not really needed unless m_labelButtons will try to delete the actual objects pointed to.
            ( if a custom class)

            1 Reply Last reply
            1
            • mzimmersM mzimmers

              For anyone keeping score at home, here's an updated code snippet. I don't claim it's the ideal way to go, but it works and is (hopefully) easily understood. Not sure my destructor is quite right, so feel free to comment on it.

              void BootPartition::displayPartitions(QString qsLabels, QString qsCurrentLabel)
              {
                  QString text;
                  QFont font;
                  int x = 50;
                  int y = 60;
                  int w = 200;
                  int h = 20;
              
                  QStringList qsl = qsLabels.split(QChar(PART_LABEL_DELIMITER));
                  for (int i = 0; i < qsl.size(); ++i)
                  {
                      m_labelButtons.push_back(new QRadioButton(this));
                      m_labelButtons[i]->setText(qsl[i]);
                      m_labelButtons[i]->setGeometry(x, y, w, h);
                      if (m_labelButtons[i]->text() == qsCurrentLabel)
                      {
                          font = m_labelButtons[i]->font();
                          font.setBold(true);
                          m_labelButtons[i]->setFont(font);
                          text = m_labelButtons[i]->text() + " (current default)";
                          m_labelButtons[i]->setText(text);
                      }
                      m_labelButtons[i]->setVisible(true);
                      y += 20;
                  }
              }
              ...
              // the below has been changed from the original posting.
              BootPartition::~BootPartition()
              {
                  qDeleteAll(m_labelButtons);
                  m_labelButtons.clear();
                  delete ui;
              }
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              hi @mzimmers ,

              first of you gave your RadioButtons a parent, therefore qt's parent/child mechanismn will take care of deleting all children, when the destructor of the main class is called.

              second, this

              for (int i = 0; i < m_labelButtons.size(); ++i)
                  {
                      m_labelButtons.pop_front();
                  }
              

              is a horrible way to clear a Container of its contents, because you don't!
              the m_labelButtons.size() is checked each increment of i and you end up only removing half of all elements!

              If you want to clear the container yourself do it like this:

              for( auto *rBtn : m_labelButtons)
                    rBtn->deleteLater();
              m_labelButtons.clear();
              
              //or for the type lazy
              qDeleteAll(m_labelButtons);
              m_labelButtons.clear();
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

                Hey guys - thanks for the corrections. I've edited my prior post, correcting the destructor.

                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