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. Calling a function from another window & class to change QLabel content [beginner]
QtWS25 Last Chance

Calling a function from another window & class to change QLabel content [beginner]

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 196 Views
  • 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.
  • U Offline
    U Offline
    utkuyilmaz1903
    wrote on last edited by utkuyilmaz1903
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • U utkuyilmaz1903

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 any MainWindow you may already have, and cannot be right.

      Do not attempt to have languagePopup access/change anything in MainWindow. Either have it as a (modal) dialog which MainWindow opens, leaves the user to interact with, and returns something which MainWindow acts on. Or have languagePopup emit a signal on click to which MainWindow has attached a slot and then MainWindow acts on that.

      Either way the important thing is do not try to have languagePopup make the change in MainWindow, have MainWindow do that.

      U 1 Reply Last reply
      1
      • JonBJ JonB

        @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 any MainWindow you may already have, and cannot be right.

        Do not attempt to have languagePopup access/change anything in MainWindow. Either have it as a (modal) dialog which MainWindow opens, leaves the user to interact with, and returns something which MainWindow acts on. Or have languagePopup emit a signal on click to which MainWindow has attached a slot and then MainWindow acts on that.

        Either way the important thing is do not try to have languagePopup make the change in MainWindow, have MainWindow do that.

        U Offline
        U Offline
        utkuyilmaz1903
        wrote on last edited by
        #3

        @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.

        JonBJ 1 Reply Last reply
        0
        • U utkuyilmaz1903

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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 no connect() inside languagePopup.
          • Nothing else (like languagePopup) should even know about MainWindow class/instance. Do not even include MainWindow.h header file anywhere other than MainWindow.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's ui/ui->language_turkish are private so can't be accessed in MainWindow....

          • The lazy/beginner way. Export a public "getter" method from languagePopup returning its ui->language_turkish. Then you can use that for the above connect(). 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. Let languagePopup 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);
          
          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