Calling a function from another window & class to change QLabel content [beginner]
-
Hey there, i am trying to create a system and building the "Settings" page. I have a mainWindow window and also languagePopup window. When i click a button it opens a popup to select a language. There are 2 language options. What i want is when i click one of the QPushButtons on languagePopup, i want to change a QLabel on mainWindow Settings page. Here is what i tried;
on mainWindow.h:
public slots:
void setTurkish();on mainWindow.cpp
void MainWindow::setTurkish(){
ui->page_settings_language_value->setText("Turkish");
}page_settings_language_value is the name of my QLabel object
Then i right clicked to button on languagePopup window and selected go to slot -> clikcked(), created a function.
on languagePopup.h
private slots:
void on_language_turkish_clicked();on languagePopup.cpp
void languagePopup::on_language_turkish_clicked()
{
MainWindow *setLanguageToTurkish= new MainWindow;
setLanguageToTurkish-> setTurkish();
}Probably there is something wrong with languagePopup.cpp part code. Since i am a beginner, solutions on web did not work for me. Probably i don't have enough experience to do right connections. How can i call setTurkish function by clicking a PushButton on languagePopup ui screen.
Appreciated to any help, thanks.
-
Hey there, i am trying to create a system and building the "Settings" page. I have a mainWindow window and also languagePopup window. When i click a button it opens a popup to select a language. There are 2 language options. What i want is when i click one of the QPushButtons on languagePopup, i want to change a QLabel on mainWindow Settings page. Here is what i tried;
on mainWindow.h:
public slots:
void setTurkish();on mainWindow.cpp
void MainWindow::setTurkish(){
ui->page_settings_language_value->setText("Turkish");
}page_settings_language_value is the name of my QLabel object
Then i right clicked to button on languagePopup window and selected go to slot -> clikcked(), created a function.
on languagePopup.h
private slots:
void on_language_turkish_clicked();on languagePopup.cpp
void languagePopup::on_language_turkish_clicked()
{
MainWindow *setLanguageToTurkish= new MainWindow;
setLanguageToTurkish-> setTurkish();
}Probably there is something wrong with languagePopup.cpp part code. Since i am a beginner, solutions on web did not work for me. Probably i don't have enough experience to do right connections. How can i call setTurkish function by clicking a PushButton on languagePopup ui screen.
Appreciated to any help, thanks.
@utkuyilmaz1903 said in Calling a function from another window & class to change QLabel content [beginner]:
MainWindow *setLanguageToTurkish= new MainWindow;
This creates a brand new (not visible)
MainWindow
instance, nothing to do with anyMainWindow
you may already have, and cannot be right.Do not attempt to have
languagePopup
access/change anything inMainWindow
. Either have it as a (modal) dialog whichMainWindow
opens, leaves the user to interact with, and returns something whichMainWindow
acts on. Or havelanguagePopup
emit a signal on click to whichMainWindow
has attached a slot and thenMainWindow
acts on that.Either way the important thing is do not try to have
languagePopup
make the change inMainWindow
, haveMainWindow
do that. -
@utkuyilmaz1903 said in Calling a function from another window & class to change QLabel content [beginner]:
MainWindow *setLanguageToTurkish= new MainWindow;
This creates a brand new (not visible)
MainWindow
instance, nothing to do with anyMainWindow
you may already have, and cannot be right.Do not attempt to have
languagePopup
access/change anything inMainWindow
. Either have it as a (modal) dialog whichMainWindow
opens, leaves the user to interact with, and returns something whichMainWindow
acts on. Or havelanguagePopup
emit a signal on click to whichMainWindow
has attached a slot and thenMainWindow
acts on that.Either way the important thing is do not try to have
languagePopup
make the change inMainWindow
, haveMainWindow
do that.@JonB Hey Jon, first of all thank you for helping me.
I tried to listen to you and added a signal-slot stuff in languagePopup.
Here is my languagePopup.cpp
#include "languagepopup.h" #include "ui_languagepopup.h" #include "mainwindow.h" languagePopup::languagePopup(QWidget *parent) : QDialog(parent), ui(new Ui::languagePopup) { ui->setupUi(this); connect(ui->language_turkish,&QPushButton::clicked,static_cast<MainWindow*>(parent),&MainWindow::setTurkish); // tried this one first, did not work. connect(ui->language_turkish, SIGNAL(clicked()), static_cast<MainWindow*>(parent),SLOT(&MainWindow::setTurkish)); // tried this one also, did not work } languagePopup::~languagePopup() { delete ui; }
Both of my tryings did not work with signal-slot logic. How a step should i take? Since i started to work with QT like 3 days ago, i'm having some misunderstandings. I hope i can solve this issue and keep moving.
-
@JonB Hey Jon, first of all thank you for helping me.
I tried to listen to you and added a signal-slot stuff in languagePopup.
Here is my languagePopup.cpp
#include "languagepopup.h" #include "ui_languagepopup.h" #include "mainwindow.h" languagePopup::languagePopup(QWidget *parent) : QDialog(parent), ui(new Ui::languagePopup) { ui->setupUi(this); connect(ui->language_turkish,&QPushButton::clicked,static_cast<MainWindow*>(parent),&MainWindow::setTurkish); // tried this one first, did not work. connect(ui->language_turkish, SIGNAL(clicked()), static_cast<MainWindow*>(parent),SLOT(&MainWindow::setTurkish)); // tried this one also, did not work } languagePopup::~languagePopup() { delete ui; }
Both of my tryings did not work with signal-slot logic. How a step should i take? Since i started to work with QT like 3 days ago, i'm having some misunderstandings. I hope i can solve this issue and keep moving.
@utkuyilmaz1903
Hey, for a beginner this is not a bad stab! At least you tried something, sensible :)You are making it a bit harder for yourself by choosing to want to react in main window immediately upon user clicking push button rather than e.g. letting using pick language in dialog and then click on "OK". That would not require signals & slots.
But since you're here. I want to be brief, I have done too many answers today!
Some points in your code:
- We do not do
connect()
s where the signaller is, we do them where the slot is. So noconnect()
insidelanguagePopup
. - Nothing else (like
languagePopup
) should even know aboutMainWindow
class/instance. Do not even includeMainWindow.h
header file anywhere other thanMainWindow.cpp
. - For all practical/beginner purposes, don't ever use old-style
SIGNAL
/SLOT()
macros. The new-style you did on the other line is the right way to go.
So what you want to do is in
MainWindow.cpp
connect(languagePopup->ui->language_turkish, &QPushButton::clicked, this, &MainWindow::setTurkish);
The problem is that
languagePopup
'sui
/ui->language_turkish
are private so can't be accessed inMainWindow
....-
The lazy/beginner way. Export a public "getter" method from
languagePopup
returning itsui->language_turkish
. Then you can use that for the aboveconnect()
. Do not let an expert know I mentioned this, it is simple but "naughty"! -
The better way. Define a new signal in
languagePopup
which just reports that "language has been changed to Turkish", or whatever. LetlanguagePopup
connect the internal pushbutton to re-emit that on click; let the outside world attach their slots to that signal.
For the second way you will need something like
// In "languagePopup.h", inside `class languagePopup` signals: void changeToTurkish(); // In "languagePopup.cpp", inside `class languagePopup::languagePopup()` connect(ui->language_turkish, &QPushButton::clicked, this, &languagePopup::changeToTurkish); // In "MainWindow.cpp" connect(languagePopupInstance, &languagePopup::changeToTurkish, this, &MainWindow::setTurkish);
- We do not do