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. Can you create slots in main.cpp file?

Can you create slots in main.cpp file?

Scheduled Pinned Locked Moved Solved General and Desktop
threadconnectionslotssignals
20 Posts 5 Posters 5.5k 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.
  • P ples76
    29 Jun 2020, 12:17

    Thank you for reply. I shouldn't have mentioned the second connection. I will try to figure that out. I really need to try to get a working solution for the first one. I think I understand now why the first code setStyleSheet does not work. This is because the parameters dont match. Can I use lambda expression to change the background color of the widget? I tried changing the connection to

    QObject::connect(serialThread, SIGNAL(setReady()), [](){statusWidget->setStyleSheet("background-color:gray");});
    

    but compiler throws an error saying that statusWidget was not captured in lambda expression. I am using QT version 4.8.7. As you can tell I am new to QT and cpp. Any help would be greatly appreciated

    J Online
    J Online
    jsulm
    Lifetime Qt Champion
    wrote on 29 Jun 2020, 12:19 last edited by
    #4

    @ples76 said in Can you create slots in main.cpp file?:

    error saying that statusWidget was not captured in lambda expression

    This is exactly the issue: you do not capture anything, change to:

    QObject::connect(serialThread, SIGNAL(setReady()), [this](){statusWidget->setStyleSheet("background-color:gray");});
    

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    J 1 Reply Last reply 29 Jun 2020, 12:21
    1
    • J jsulm
      29 Jun 2020, 12:19

      @ples76 said in Can you create slots in main.cpp file?:

      error saying that statusWidget was not captured in lambda expression

      This is exactly the issue: you do not capture anything, change to:

      QObject::connect(serialThread, SIGNAL(setReady()), [this](){statusWidget->setStyleSheet("background-color:gray");});
      
      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 29 Jun 2020, 12:21 last edited by
      #5

      @jsulm still won't work, you need the new pointer to member function syntax to use a lambda as the receiving slot


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3
      • P Offline
        P Offline
        ples76
        wrote on 29 Jun 2020, 12:34 last edited by
        #6

        Sorry to show my ignorance but is there a way to add member function to main.cpp file that can be used in lambda function that will have my widget created by main in scope?

        J 1 Reply Last reply 29 Jun 2020, 12:44
        0
        • P ples76
          29 Jun 2020, 12:34

          Sorry to show my ignorance but is there a way to add member function to main.cpp file that can be used in lambda function that will have my widget created by main in scope?

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 29 Jun 2020, 12:44 last edited by
          #7

          @ples76

          int main(int argc, char *argv[])
          {
              QApplication app(argc, argv);
          
              QWidget w;
              w.resize(500,50);
              w.show();
          
              QTimer t;
              QObject::connect(&t, &QTimer::timeout, &w, [&w]()->void{w.setStyleSheet("background-color:red;");});
              t.start(5000);
          
              return app.exec();
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3
          • P Offline
            P Offline
            ples76
            wrote on 29 Jun 2020, 13:27 last edited by
            #8

            Thank you for your responses. My widget is created as a pointer like this:

            QWidget *statusWidget = new QWidget;
            

            So I created connection based on your response like this:

            	QObject::connect(serialThread, SIGNAL(setReady()),statusWidget, [statusWidget]()->void{statusWidget->setStyleSheet("background-color:gray;");});
            	
            

            Compiler throws the following error:
            main.cpp: In function ‘int main(int, char**)’:
            main.cpp:101:144: error: no matching function for call to ‘QObject::connect(LeptonSerial*&, const char [12], QWidget*&, main(int, char**)::<lambda()>)’
            QObject::connect(serialThread, SIGNAL(setReady()),statusWidget, statusWidget->void{statusWidget->setStyleSheet("background-color:gray;");});

            1 Reply Last reply
            0
            • P ples76
              29 Jun 2020, 02:02

              I have a project that all the widgets are created in the main.cpp file main() function. I have a worker thread that is emitting a signal to the main thread to change the background color of the widget created by main function. Here is an example

              QObject::connect(serialThread, SIGNAL(setReady(QString)), statusWidget, SLOT(setStyleSheet("background-color:gray")));
              

              When running the program I get an error that says the connection could not be made with the above code.

              I have also created connections to change text:

              QObject::connect(serialThread, SIGNAL(setReady(QString)), myStatusText, SLOT(setText(QString)));
              

              Neither of these are working. I do know the signals are probably being emitted because I have other signals fired at the same time which is going to another worker thread and I can see those working. I have tried creating a main.h file and adding slots to that and the main.cpp file and just having the connections fire the SLOTS but the compilers says I can't declare the slots after the main() function in the main.cpp file. Is there any way I can achieve what I am trying to do?

              P Offline
              P Offline
              Pablo J. Rogina
              wrote on 29 Jun 2020, 13:32 last edited by
              #9

              @ples76 said in Can you create slots in main.cpp file?:

              I have a project that all the widgets are created in the main.cpp file main() function

              Did you inherit the project with such way of creating Qt widgets, or just you started it that way?

              QObject::connect(serialThread, SIGNAL(setReady()),statusWidget, statusWidget->void{statusWidget->setStyleSheet("background-color:gray;");});

              As @Christian-Ehrlicher already suggested, please use the new syntax for signal & slots...

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1
              • P Offline
                P Offline
                ples76
                wrote on 29 Jun 2020, 13:55 last edited by
                #10

                I inherited the base of the project. I am trying to add some functionality.

                As for using the new syntax I was under the understanding that pr QT 5.0 I couldn't use the new syntax. Is my understanding incorrect? I am using QT 4.8.7

                1 Reply Last reply
                0
                • C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 29 Jun 2020, 13:59 last edited by
                  #11

                  If you can't use qt5 you have to define a custom slot and call the function with the correct parameter there since you can't mix old style connect and lambdas.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  3
                  • P Offline
                    P Offline
                    ples76
                    wrote on 29 Jun 2020, 14:06 last edited by
                    #12

                    Christian,
                    I was afraid that was the case. Do you know where I could locate an example of this. The problem I am having is I have found several examples of creating custom slots but they are all in classes outside of main. I have the issue of the widgets being created in the main function. Is it possible to create a custom slot that can be used in main and thus have the widget in scope?
                    This is my first project in qt and I am trying to find the easiest solution to this since I am under the gun to get this done asap.

                    1 Reply Last reply
                    0
                    • C Online
                      C Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 29 Jun 2020, 14:12 last edited by
                      #13

                      A slot must be in a class, you have no other chance.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      1
                      • P Offline
                        P Offline
                        ples76
                        wrote on 29 Jun 2020, 14:21 last edited by
                        #14

                        Thank you for your patience with my small amount of knowledge on the subject. I appreciate the help and clarification on my issue. I think my best choice of action is to try and move my code out of main and maybe create a QMainWindow class and see if everything will work that way. Do you think this is my best choice of action or will I possibly run into issues in a QMainWindow class also. I am not speaking about other code in main but specifically about the connections mentioned in this thread. Basically I am asking if am able to move the code to a QMainWindow Class will I then be able to create custom slots to achieve my goal?

                        P 1 Reply Last reply 29 Jun 2020, 14:24
                        0
                        • P ples76
                          29 Jun 2020, 14:21

                          Thank you for your patience with my small amount of knowledge on the subject. I appreciate the help and clarification on my issue. I think my best choice of action is to try and move my code out of main and maybe create a QMainWindow class and see if everything will work that way. Do you think this is my best choice of action or will I possibly run into issues in a QMainWindow class also. I am not speaking about other code in main but specifically about the connections mentioned in this thread. Basically I am asking if am able to move the code to a QMainWindow Class will I then be able to create custom slots to achieve my goal?

                          P Offline
                          P Offline
                          Pablo J. Rogina
                          wrote on 29 Jun 2020, 14:24 last edited by
                          #15

                          @ples76 said in Can you create slots in main.cpp file?:

                          move my code out of main and maybe create a QMainWindow class

                          I was about to suggest something similar, but to subclass QApplication. So to move all the widgets instantiation there, and since your MySuperDuperQApplication class is an QObject you should be able to work with custom slots as suggested.

                          I have the issue of the widgets being created in the main function

                          Just in case, could it it be possible you show the source code for the main() ?

                          Upvote the answer(s) that helped you solve the issue
                          Use "Topic Tools" button to mark your post as Solved
                          Add screenshots via postimage.org
                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          1
                          • P Offline
                            P Offline
                            ples76
                            wrote on 29 Jun 2020, 14:50 last edited by ples76
                            #16
                            This post is deleted!
                            P 1 Reply Last reply 29 Jun 2020, 15:23
                            0
                            • C Online
                              C Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 29 Jun 2020, 15:11 last edited by
                              #17

                              All this stuff should not be in main - this is really bad style. This should all go into the ctor of 'window' since this is the place where this all should happen.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              2
                              • P ples76
                                29 Jun 2020, 14:50

                                This post is deleted!

                                P Offline
                                P Offline
                                Pablo J. Rogina
                                wrote on 29 Jun 2020, 15:23 last edited by
                                #18

                                @ples76

                                This should all go into the ctor of 'window' since this is the place where this all should happen.

                                Yes, I agree with @Christian-Ehrlicher suggestion. Although you inherit the project that way, it seems there's no reason to continue that bad approach.

                                Your main() code should be reduced to something like this:

                                int main(int argc, char **argv)
                                {
                                    QApplication a(argc, argv);
                                    QWidget window;
                                    window.show();
                                    return a.exec();
                                }
                                

                                Upvote the answer(s) that helped you solve the issue
                                Use "Topic Tools" button to mark your post as Solved
                                Add screenshots via postimage.org
                                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                1 Reply Last reply
                                2
                                • P Offline
                                  P Offline
                                  ples76
                                  wrote on 29 Jun 2020, 15:54 last edited by ples76
                                  #19

                                  I have solved the problem with all of your help Specifically LeLev who gave me the final working solution through PM. I created a QWidget class for my statusWidget. I added the following in the StatusWidget.h file:

                                  #ifndef STATUSWIDGET_H
                                  #define STATUSWIDGET_H
                                  
                                  #include <QtCore>
                                  #include <QWidget>
                                  #include <QLabel>
                                  
                                  
                                  
                                  
                                  class StatusWidget : public QWidget {
                                    Q_OBJECT;
                                  
                                    public:
                                      StatusWidget(QWidget *parent = 0);
                                      ~StatusWidget();
                                  
                                    public slots:
                                      void setBgColor(QString);
                                  };
                                  
                                  #endif
                                  

                                  And the StatusWidget.cpp file:

                                  #include <ctime>
                                  #include <stdint.h>
                                  #include "StatusWidget.h"
                                  
                                  
                                  #include <QtCore>
                                  #include <QWidget>
                                  
                                  
                                  
                                  StatusWidget::StatusWidget(QWidget *parent) : QWidget(parent)
                                  {
                                  }
                                  StatusWidget::~StatusWidget()
                                  {
                                  }
                                  
                                  
                                  void StatusWidget::setBgColor(QString color) {
                                    this->setStyleSheet("background-color:" + color + ";");
                                  }
                                  

                                  I then changed my connections to the following:

                                  QObject::connect(serialThread, SIGNAL(setReady(QString)),statusWidget, SLOT(setBgColor(QString)));
                                  QObject::connect(serialThread, SIGNAL(setReading(QString)), statusWidget, SLOT(setBgColor(QString)));
                                  QObject::connect(serialThread, SIGNAL(setReReading(QString)), statusWidget, SLOT(setBgColor(QString)));
                                  QObject::connect(serialThread, SIGNAL(setPass(QString)), statusWidget, SLOT(setBgColor(QString)));
                                  QObject::connect(serialThread, SIGNAL(setFail(QString)), statusWidget, SLOT(setBgColor(QString)));
                                  

                                  I can now change my background color accordingly. Thank you all for your help!!!

                                  P 1 Reply Last reply 29 Jun 2020, 16:25
                                  0
                                  • P ples76
                                    29 Jun 2020, 15:54

                                    I have solved the problem with all of your help Specifically LeLev who gave me the final working solution through PM. I created a QWidget class for my statusWidget. I added the following in the StatusWidget.h file:

                                    #ifndef STATUSWIDGET_H
                                    #define STATUSWIDGET_H
                                    
                                    #include <QtCore>
                                    #include <QWidget>
                                    #include <QLabel>
                                    
                                    
                                    
                                    
                                    class StatusWidget : public QWidget {
                                      Q_OBJECT;
                                    
                                      public:
                                        StatusWidget(QWidget *parent = 0);
                                        ~StatusWidget();
                                    
                                      public slots:
                                        void setBgColor(QString);
                                    };
                                    
                                    #endif
                                    

                                    And the StatusWidget.cpp file:

                                    #include <ctime>
                                    #include <stdint.h>
                                    #include "StatusWidget.h"
                                    
                                    
                                    #include <QtCore>
                                    #include <QWidget>
                                    
                                    
                                    
                                    StatusWidget::StatusWidget(QWidget *parent) : QWidget(parent)
                                    {
                                    }
                                    StatusWidget::~StatusWidget()
                                    {
                                    }
                                    
                                    
                                    void StatusWidget::setBgColor(QString color) {
                                      this->setStyleSheet("background-color:" + color + ";");
                                    }
                                    

                                    I then changed my connections to the following:

                                    QObject::connect(serialThread, SIGNAL(setReady(QString)),statusWidget, SLOT(setBgColor(QString)));
                                    QObject::connect(serialThread, SIGNAL(setReading(QString)), statusWidget, SLOT(setBgColor(QString)));
                                    QObject::connect(serialThread, SIGNAL(setReReading(QString)), statusWidget, SLOT(setBgColor(QString)));
                                    QObject::connect(serialThread, SIGNAL(setPass(QString)), statusWidget, SLOT(setBgColor(QString)));
                                    QObject::connect(serialThread, SIGNAL(setFail(QString)), statusWidget, SLOT(setBgColor(QString)));
                                    

                                    I can now change my background color accordingly. Thank you all for your help!!!

                                    P Offline
                                    P Offline
                                    Pablo J. Rogina
                                    wrote on 29 Jun 2020, 16:25 last edited by
                                    #20

                                    @ples76 said in Can you create slots in main.cpp file?:

                                    I have solved the problem with all of your help

                                    Great, so please don't forget to mark your post as solved!

                                    I am trying to find the easiest solution to this since I am under the gun to get this done asap

                                    Although you find a solution now, you may want to take into account that having such a main() function is not a good idea as @Christian-Ehrlicher pointed out.
                                    So time (and stakeholders) permitting, you might want to look at refactoring your code...

                                    Upvote the answer(s) that helped you solve the issue
                                    Use "Topic Tools" button to mark your post as Solved
                                    Add screenshots via postimage.org
                                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                    1 Reply Last reply
                                    2

                                    13/20

                                    29 Jun 2020, 14:12

                                    • Login

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