Replacing a QGroupbox with another QGroupBox (with intention of just changing the DISPLAYED title)
-
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 } -
AH I FIGURED IT OUT. So for whatever reason, since i was creating a new QGroupBox
pieGroupevery 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. -
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 }@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(); } -
@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(); }@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()); } -
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(). -
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(). -
@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()); }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 ?
-
@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()); }@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?
-
@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()); }@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()! -
@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@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?
-
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 ?
@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
pieGroupevery 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.