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?
Forum Updated to NodeBB v4.3 + New Features

Can you create slots in main.cpp file?

Scheduled Pinned Locked Moved Solved General and Desktop
threadconnectionslotssignals
20 Posts 5 Posters 5.9k Views 3 Watching
  • 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 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 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
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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 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?

          Pablo J. RoginaP 1 Reply Last reply
          0
          • P ples76

            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?

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on 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 last edited by ples76
              #16
              This post is deleted!
              Pablo J. RoginaP 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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

                  This post is deleted!

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on 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 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!!!

                    Pablo J. RoginaP 1 Reply Last reply
                    0
                    • P ples76

                      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!!!

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on 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

                      • Login

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