'Qt: Change MainWindow Label Text By Click From Dialog
Case: App starts, MainWindow opens, then auto-show dialog window (call this A). There's a button in A called 'btn'. In MainWindow, I have a label called 'sometext_label'. What I want is, when 'btn' is clicked, change 'sometext_label' text to "Hi!" for example, and then close dialog A. When dialog A is closed, the label's text should already be changed to "Hi!".
I have written my code and it's trash. It doesn't work. So, how you do it? I've tried using connect function, the signal and slot, but it's not changing anything..
//MainWindow.h
...
private slots:
void Get();
...
//MainWindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
A = new DialogA; //DialogA *A in MainWindow.h
connect(A, SIGNAL(Send()), this, SLOT(Get()));
}
void MainWindow::Get(){
ui->Sometext_label->setText("Hi!");
}
//DialogA.h
...
signals:
void Send();
...
//DialogA.cpp
void DialogA::on_btn_clicked()
{
emit Send();
}
Solution: In my case, I use dialog.exec() function to call the dialog window. Instead, just use dialog.show() function or it won't do good...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
