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.
  • C Offline
    C Offline
    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 Offline
        C Offline
        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 Offline
                C Offline
                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

                      20/20

                      29 Jun 2020, 16:25

                      • Login

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