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. Replacing a QGroupbox with another QGroupBox (with intention of just changing the DISPLAYED title)
Qt 6.11 is out! See what's new in the release blog

Replacing a QGroupbox with another QGroupBox (with intention of just changing the DISPLAYED title)

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 4.0k Views 3 Watching
  • 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.
  • JoeCFDJ JoeCFD

    QWidget *wdg = new QWidget; <<== can cause memory leak.

    pgiovanniP Offline
    pgiovanniP Offline
    pgiovanni
    wrote on last edited by
    #3

    @JoeCFD alright let me fix up my shortened code. Im just trying to abbreviate it. Give me a half hour or so

    1 Reply Last reply
    0
    • pgiovanniP pgiovanni

      So this slot opens a window and displays a widget below another one. Every time this function is called i want to replace the original QGroupBox with another one. The title visible title is hard to change. Thats all i really want to do, but setTitle() doesn't change the displayed title.

      void Window::clicked () {
      QWidget *wdg = new QWidget;
      wdg->show();
      
      QGridLayout *windowLayout = new QGridLayout(wdg);
      
      QGridLayout *layout = new QGridLayout;
      QPushButton *button1 = new QPushButton ("&button1", this);
      QPushButton *button2 = new QPushButton ("&button2", this);
      
      layout->addWidget(button1, 0, 0);
      layout->addWidget(button2, 1, 0);
      
      QGroupbox groupBox = new QGroupBox(tr("title one"));
      groupBox->setLayout(layout);
      
      
      if(clickedOnce)
        windowLayout->addWidget(pieGroup, 1, 0);
      if(clickedMorethanOnce)
        //replace original widget with new widget
      }
      
      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @pgiovanni said in Replacing a QGroupbox with another QGroupBox (with intention of just changing the DISPLAYED title):

      Thats all i really want to do, but setTitle() doesn't change the displayed title.

      Works fine for me:

      int main(int argc, char **argv)
      {
        QApplication app(argc, argv);
        QGroupBox gb("Title1");
        QHBoxLayout hLay;
        QPushButton pb("PushMe");
        hLay.addWidget(&pb);
        gb.setLayout(&hLay);
        gb.show();
        int i = 1;
        QObject::connect(&pb, &QPushButton::clicked, [&]() { gb.setTitle(QString("Title%1").arg(++i));});
        return app.exec();
      }
      

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

      pgiovanniP 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @pgiovanni said in Replacing a QGroupbox with another QGroupBox (with intention of just changing the DISPLAYED title):

        Thats all i really want to do, but setTitle() doesn't change the displayed title.

        Works fine for me:

        int main(int argc, char **argv)
        {
          QApplication app(argc, argv);
          QGroupBox gb("Title1");
          QHBoxLayout hLay;
          QPushButton pb("PushMe");
          hLay.addWidget(&pb);
          gb.setLayout(&hLay);
          gb.show();
          int i = 1;
          QObject::connect(&pb, &QPushButton::clicked, [&]() { gb.setTitle(QString("Title%1").arg(++i));});
          return app.exec();
        }
        
        pgiovanniP Offline
        pgiovanniP Offline
        pgiovanni
        wrote on last edited by pgiovanni
        #5

        @Christian-Ehrlicher that changes your displayed title huh? i gotta be doing something wrong then. I think the difference is i'm not using a signal to connect function to change the title. The firs title is set once it's clicked. Then when it's clicked again it's supposed to change the title according to the button that was clicked. I'm using a QPieSeries and when each slice is clicked the QGroupBox 's title is to change according to the QPieSlices label() attribute. As of right now the title is set according to the first click. Each following click doesn't change the title.

         if(clickCount == 1){
                 chartLayout->addWidget(pieGroup, 1, 0);
            }
        
            else{
                pieGroup->setTitle(slice->label().toStdString().c_str());
            }
        
        SGaistS Christian EhrlicherC JonBJ 3 Replies Last reply
        0
        • M Offline
          M Offline
          mchinand
          wrote on last edited by
          #6

          Print out the string from slice->label(), to make sure it's different from your QGroupBox's current title. Also, you don't have to convert to a std::string and then a char *, setTitle() takes a QString which is the output type of QPieSlice::label().

          pgiovanniP 1 Reply Last reply
          0
          • M mchinand

            Print out the string from slice->label(), to make sure it's different from your QGroupBox's current title. Also, you don't have to convert to a std::string and then a char *, setTitle() takes a QString which is the output type of QPieSlice::label().

            pgiovanniP Offline
            pgiovanniP Offline
            pgiovanni
            wrote on last edited by pgiovanni
            #7

            @mchinand It prints out the correct string but the title isn't set right. Doing std::cerr << slice->label().toStdString << std::endl; prints the right string

            M 1 Reply Last reply
            0
            • pgiovanniP pgiovanni

              @Christian-Ehrlicher that changes your displayed title huh? i gotta be doing something wrong then. I think the difference is i'm not using a signal to connect function to change the title. The firs title is set once it's clicked. Then when it's clicked again it's supposed to change the title according to the button that was clicked. I'm using a QPieSeries and when each slice is clicked the QGroupBox 's title is to change according to the QPieSlices label() attribute. As of right now the title is set according to the first click. Each following click doesn't change the title.

               if(clickCount == 1){
                       chartLayout->addWidget(pieGroup, 1, 0);
                  }
              
                  else{
                      pieGroup->setTitle(slice->label().toStdString().c_str());
                  }
              
              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8

              Hi,

              @pgiovanni said in Replacing a QGroupbox with another QGroupBox (with intention of just changing the DISPLAYED title):

              clickCount == 1

              Where are you incrementing clickCount ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              pgiovanniP 1 Reply Last reply
              0
              • pgiovanniP pgiovanni

                @Christian-Ehrlicher that changes your displayed title huh? i gotta be doing something wrong then. I think the difference is i'm not using a signal to connect function to change the title. The firs title is set once it's clicked. Then when it's clicked again it's supposed to change the title according to the button that was clicked. I'm using a QPieSeries and when each slice is clicked the QGroupBox 's title is to change according to the QPieSlices label() attribute. As of right now the title is set according to the first click. Each following click doesn't change the title.

                 if(clickCount == 1){
                         chartLayout->addWidget(pieGroup, 1, 0);
                    }
                
                    else{
                        pieGroup->setTitle(slice->label().toStdString().c_str());
                    }
                
                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @pgiovanni said in Replacing a QGroupbox with another QGroupBox (with intention of just changing the DISPLAYED title):

                think the difference is i'm not using a signal to connect function to change the title.

                It does not matter

                slice->label()

                and you're sure this does return what you expect? I would say no.

                Simplify your code until it works. You're doing something wrong.

                btw: What Qt version and OS do you use?

                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
                0
                • pgiovanniP pgiovanni

                  @Christian-Ehrlicher that changes your displayed title huh? i gotta be doing something wrong then. I think the difference is i'm not using a signal to connect function to change the title. The firs title is set once it's clicked. Then when it's clicked again it's supposed to change the title according to the button that was clicked. I'm using a QPieSeries and when each slice is clicked the QGroupBox 's title is to change according to the QPieSlices label() attribute. As of right now the title is set according to the first click. Each following click doesn't change the title.

                   if(clickCount == 1){
                           chartLayout->addWidget(pieGroup, 1, 0);
                      }
                  
                      else{
                          pieGroup->setTitle(slice->label().toStdString().c_str());
                      }
                  
                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #10

                  @pgiovanni
                  There have been a lot of suggestions as to what might or might not be happening. Just put in some some qDebug() statements, particularly for the setTiitle()!

                  1 Reply Last reply
                  1
                  • pgiovanniP pgiovanni

                    @mchinand It prints out the correct string but the title isn't set right. Doing std::cerr << slice->label().toStdString << std::endl; prints the right string

                    M Offline
                    M Offline
                    mchinand
                    wrote on last edited by
                    #11

                    @pgiovanni Are you printing out that string right before the call to setTitle()? You are not in some kind of loop that would prevent the UI from updating?

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      @pgiovanni said in Replacing a QGroupbox with another QGroupBox (with intention of just changing the DISPLAYED title):

                      clickCount == 1

                      Where are you incrementing clickCount ?

                      pgiovanniP Offline
                      pgiovanniP Offline
                      pgiovanni
                      wrote on last edited by pgiovanni
                      #12

                      @SGaist it increments only up to 2 at the top of my clicked() function.

                      Where are you incrementing clickCount ?

                      //int `clickCount` initialized to zero outside of function
                      //sliceID is passed via a connect function and a lambda expression from a for loop that loops the pie series 
                        for(qsizetype i = 0; i < series->count(); i++){
                              QPieSlice *slice = series->slices().at(i);
                              QString sliceID = slice->label();
                              connect(series->slices().at(i), &QPieSlice::clicked, this, [this, sliceID] { sliceClicked(sliceID); });
                          }
                      
                      
                      void Window::pieSliceClicked(QString sliceID) {
                      
                      if(clickCount == 0)
                           clickCount = clickCount + 1;
                      else if(clickCount == 1)
                           clickCount = clickCount + 1;
                      
                      
                      //rest of function
                      
                      if(clickCount == 1) 
                           layout->addWidget(pieGroup, 1, 0);
                      else
                           pieGroup->setTitle(sliceID)
                      
                      }
                      

                      @Christian-Ehrlicher

                      btw: What Qt version and OS do you use?

                      QT 6.2.1 and Linux Mint 20.2

                      @mchinand

                      @pgiovanni Are you printing out that string right before the call to setTitle()? You are not in some kind of loop that would prevent the UI from updating?

                      No loop. I print it after setTitle() . Hang on i got an idea now.

                          if(clickCount == 1){
                               chartLayout->addWidget(pieGroup, 1, 0);
                          }
                      
                          else{
                              pieGroup->setTitle(sliceIdentity);
                              std::cerr << sliceIdentity.toStdString() << std::endl;
                              std::cerr << pieGroup->title().toStdString() << std::endl;
                          }
                      

                      @JonB

                      There have been a lot of suggestions as to what might or might not be happening. Just put in some some qDebug() statements, particularly for the setTiitle()!

                      ok i'll give it another debug whirl. Thanks for the suggestions.

                      1 Reply Last reply
                      0
                      • pgiovanniP Offline
                        pgiovanniP Offline
                        pgiovanni
                        wrote on last edited by pgiovanni
                        #13

                        Question and theory:

                        If i locate the actual place where the title is supposed to be updating in the UI, I might be able to figure out why it's NOT updating in the UI even though the value is being changed correctly. Does anyone know where that happens or if that sounds accurate?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          You should use the debugger and go step by step in your code to see what is happening.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1
                          • pgiovanniP Offline
                            pgiovanniP Offline
                            pgiovanni
                            wrote on last edited by pgiovanni
                            #15

                            check this out. I actually got it to work on another file.

                            mainwindow.cpp:

                            #include "mainwindow.h"
                            #include "ui_mainwindow.h"
                            #include <QPushButton>
                            #include <QGridLayout>
                            #include <QGroupBox>
                            #include <iostream>
                            
                            MainWindow::MainWindow(QWidget *parent)
                                : QMainWindow(parent)
                                , ui(new Ui::MainWindow)
                            {
                                ui->setupUi(this);
                            
                                QWidget * wdg = new QWidget(this);
                                wdg->show();
                            
                                QWidget *wdg2 = new QWidget(this);
                                wdg->show();
                            
                                QGridLayout *wdg2layout = new QGridLayout (wdg2);
                                gb2 = new QGroupBox(tr("gb2 title"));
                                QPushButton *button3 = new QPushButton (wdg2);
                                QPushButton *button4 = new QPushButton (wdg2);
                                button3->setText("button3");
                                button4->setText("button4");
                            
                                wdg2layout->addWidget(button3, 0, 0);
                                wdg2layout->addWidget(button4, 1, 0 );
                                gb2->setLayout(wdg2layout);
                            
                                QPushButton *button = new QPushButton (wdg);
                                QPushButton *button2 = new QPushButton (wdg);
                                button->setText("button 1");
                                button2->setText("button2");
                                QGridLayout *layout = new QGridLayout(wdg);
                                groupB = new QGroupBox(tr("qgroup box"));
                            
                                layout->addWidget(button, 0, 0);
                                layout->addWidget(button2, 1, 0);
                                layout->addWidget(gb2, 2, 0);
                            
                                groupB->setLayout(layout);
                            
                                setCentralWidget(wdg);
                                wdg->setLayout(layout);
                            
                                QString button3Text = button3->text();
                                QString button4Text = button4->text();
                            
                            
                                connect(button3, &QPushButton::clicked, this, [this, button3Text]{buttonClicked(button3Text);});
                                connect(button4, &QPushButton::clicked, this, [this, button4Text]{buttonClicked(button4Text);});
                            
                            }
                            
                            void MainWindow::buttonClicked(QString buttonName) {
                            
                                gb2->setTitle(buttonName);
                                std::cerr << groupB->title().toStdString() << std::endl;
                            
                            }
                            
                            
                            MainWindow::~MainWindow()
                            {
                                delete ui;
                            }
                            
                            1 Reply Last reply
                            0
                            • pgiovanniP Offline
                              pgiovanniP Offline
                              pgiovanni
                              wrote on last edited by pgiovanni
                              #16

                              AH I FIGURED IT OUT. So for whatever reason, since i was creating a new QGroupBox pieGroup every time the function was called that somehow prevented the title from updating. So I changed it to create the new groupbox on the first click only. Now all the other clicks just update the title. I don't know what's going on under the covers. but it works now.

                              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