Solved Replacing a QGroupbox with another QGroupBox (with intention of just changing the DISPLAYED title)
-
@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 -
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 ?
-
@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?
-
@pgiovanni
There have been a lot of suggestions as to what might or might not be happening. Just put in some someqDebug()
statements, particularly for thesetTiitle()
! -
@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?
-
@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) }
btw: What Qt version and OS do you use?
QT 6.2.1 and Linux Mint 20.2
@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; }
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.
-
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?
-
You should use the debugger and go step by step in your code to see what is happening.
-
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; }
-
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.