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. Changing the property's of widgets from another window [SOLVED]
QtWS25 Last Chance

Changing the property's of widgets from another window [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
connectguibeginner
9 Posts 2 Posters 5.2k 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.
  • B Offline
    B Offline
    Bart
    wrote on 4 Jul 2015, 12:45 last edited by Bart 7 Aug 2015, 12:41
    #1

    Hi,

    I am new to c++ and Qt so maybe this is a stupid question but I can't figure it out. I made a widget application with two windows. I got a main window and a configuration window. Now I want to change the text of a button in my mainwindow when I press a button in my configuration window but I don't know how to. If anyone could provide some kind of help or tip it would be appreciated.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 4 Jul 2015, 16:13 last edited by
      #2

      Hi, welcome to devnet.

      Emit a signal from your dialog and connect it to a slot in your main window that will change the text.

      Signlas/slots are one of the core concepts of Qt so it's important to grasp how they work and how to use them.
      You can read a lot more about them and look at some examples here.

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Bart
        wrote on 5 Jul 2015, 12:36 last edited by Bart 7 May 2015, 12:39
        #3

        @Chris-Kawa

        Hi thanks for the reply,

        I tryed it but it didn't work. I got 2 classes: MainWindow & Configuration. This is my code:

        Confgiuration.h

        signals:
          void sendSignal();
        

        Configuration.cpp

        void Configuration::on_pushButton_clicked()
        {
          emit sendSignal();
        }
        

        MainWindow.h

        private slots:
          void setVariable();
        

        MainWindow.cpp

        void MainWindow::setVariable()
        {
          ui->FrontSlot1->setText("frontslot1");
          ui->FrontSlot1->setDisabled(false);
        }
        
        void MainWindow::setUpPage()
        {
        connect(Configuration, SIGNAL(sendSignal()), this, SLOT(setVariable()));
        }
        

        And here is where I get the following error but I can't figure out why.

        error: expected primary-expression before ',' token
         connect(Configuration, SIGNAL(sendSignal()), this, SLOT(setVariable()));
        
        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 5 Jul 2015, 12:51 last edited by
          #4

          The first argument to connect() is a pointer to an instance that emits a signal, not a class name.
          Pass there a pointer to the Configuration class instance that is your dialog.

          B 1 Reply Last reply 5 Jul 2015, 13:28
          1
          • C Chris Kawa
            5 Jul 2015, 12:51

            The first argument to connect() is a pointer to an instance that emits a signal, not a class name.
            Pass there a pointer to the Configuration class instance that is your dialog.

            B Offline
            B Offline
            Bart
            wrote on 5 Jul 2015, 13:28 last edited by
            #5

            @Chris-Kawa

            Thanks for replying,

            I changed the code in my MainWindow.cpp to:

            void MainWindow::setUpPage()
            {
              Configuration *configpointer = new Configuration;
              connect(configpointer, SIGNAL(setSlots()), this, SLOT(setSlots()));
            }
            

            It compiles without any problem now but I don't see anything changing in the mainwindow.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 5 Jul 2015, 13:36 last edited by Chris Kawa 7 May 2015, 13:37
              #6

              configpointer is a local variable (that leaks memory by the way).
              You should connect it to the actual instance of the dialog you use, not some temporary that never gets shown.
              Also setSlots is neither a signal in Configuration class nor a slot in MainWindow.

              Here's an example with both Configuration and MainWindow shown at sturtup:

              int main(int argc, char *argv[]) {
                  QApplication a(argc, argv);
                  
                  MainWindow w;
                  Configuration cfg;
                  connect(&cfg, &Configuration::sendSignal, &w, &MainWindow::setVariable);
                  w.show();
                  cfg.show();
              
                  return a.exec();
              }
              

              If, on the other hand, you show your Configuration dialog in response to some button clicked in MainWindow here's how the code may look like:

              void MainWindow::showConfigurationButtonClicked() {
                  Configuration cfg;
                  connect(&cfg, &Configuation::sendSignal, this, &MainWindow::setVariable);
                  cfg.exec();
              }
              
              B 1 Reply Last reply 5 Jul 2015, 14:00
              1
              • C Chris Kawa
                5 Jul 2015, 13:36

                configpointer is a local variable (that leaks memory by the way).
                You should connect it to the actual instance of the dialog you use, not some temporary that never gets shown.
                Also setSlots is neither a signal in Configuration class nor a slot in MainWindow.

                Here's an example with both Configuration and MainWindow shown at sturtup:

                int main(int argc, char *argv[]) {
                    QApplication a(argc, argv);
                    
                    MainWindow w;
                    Configuration cfg;
                    connect(&cfg, &Configuration::sendSignal, &w, &MainWindow::setVariable);
                    w.show();
                    cfg.show();
                
                    return a.exec();
                }
                

                If, on the other hand, you show your Configuration dialog in response to some button clicked in MainWindow here's how the code may look like:

                void MainWindow::showConfigurationButtonClicked() {
                    Configuration cfg;
                    connect(&cfg, &Configuation::sendSignal, this, &MainWindow::setVariable);
                    cfg.exec();
                }
                
                B Offline
                B Offline
                Bart
                wrote on 5 Jul 2015, 14:00 last edited by Bart 7 May 2015, 14:00
                #7

                @Chris-Kawa

                Sorry for being unclear but I don't think you understood my problem.

                I got this code in my MainWindow.cpp now:

                void MainWindow::on_Configuration_clicked()
                {
                  Configuration Configurationob;
                  Configurationob.setModal(true);
                  Configurationob.exec();
                
                }
                
                void MainWindow::setSlots(){
                  ui->FrontSlot1->setText("updated");
                  ui->FrontSlot1->setDisabled(false);
                  ui->FrontSlot1->update();
                }
                
                void MainWindow::setUpPage()
                {
                  Configuration cfg;
                  connect(&cfg, SIGNAL(sendSignal()), this, SLOT(setSlots()));
                }
                

                I got a button in my mainwindow called "FrontSlot1" which is disabled by default. What I want is that when the user presses a button in the configuration window it will call the setSlots() function and execute the commands in it. I dont want to open a new window. I already did that with the void MainWindow::on_Configuration_clicked() function.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on 5 Jul 2015, 14:16 last edited by Chris Kawa 7 May 2015, 14:17
                  #8

                  I understood your problem. You didn't understood the answer, as it addresses exactly that ;)

                  Let me be more clear. Look at your code. In on_Configuration_clicked you create an instance of Configuration called Configurationob and show it. In setUpPage method you create another, totally unrelated instance of that class called cfg and connect to it. That's not gonna do what you want. Configurationob and cfg are totally separate, unrelated pieces of data.

                  The solution here is (similar to what I showed in the example) this:

                  void MainWindow::on_Configuration_clicked() {
                    Configuration Configurationob;
                    Configurationob.setModal(true);
                    connect(&Configurationob, SIGNAL(sendSignal()), this, SLOT(setSlots())); //the important part
                    Configurationob.exec();
                  }
                  
                  void MainWindow::setSlots(){
                    ui->FrontSlot1->setText("updated");
                    ui->FrontSlot1->setDisabled(false);
                    ui->FrontSlot1->update();
                  }
                  
                  B 1 Reply Last reply 5 Jul 2015, 14:23
                  1
                  • C Chris Kawa
                    5 Jul 2015, 14:16

                    I understood your problem. You didn't understood the answer, as it addresses exactly that ;)

                    Let me be more clear. Look at your code. In on_Configuration_clicked you create an instance of Configuration called Configurationob and show it. In setUpPage method you create another, totally unrelated instance of that class called cfg and connect to it. That's not gonna do what you want. Configurationob and cfg are totally separate, unrelated pieces of data.

                    The solution here is (similar to what I showed in the example) this:

                    void MainWindow::on_Configuration_clicked() {
                      Configuration Configurationob;
                      Configurationob.setModal(true);
                      connect(&Configurationob, SIGNAL(sendSignal()), this, SLOT(setSlots())); //the important part
                      Configurationob.exec();
                    }
                    
                    void MainWindow::setSlots(){
                      ui->FrontSlot1->setText("updated");
                      ui->FrontSlot1->setDisabled(false);
                      ui->FrontSlot1->update();
                    }
                    
                    B Offline
                    B Offline
                    Bart
                    wrote on 5 Jul 2015, 14:23 last edited by
                    #9

                    @Chris-Kawa

                    Thank you very much!

                    1 Reply Last reply
                    0

                    1/9

                    4 Jul 2015, 12:45

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved