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. signal from one class into main app class
Forum Updated to NodeBB v4.3 + New Features

signal from one class into main app class

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 703 Views 1 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.
  • jsulmJ jsulm

    @Lightshadown said in signal from one class into main app class:

    "use of deleted function 'QApplication::QApplication()'

    Where exactly in your code does this error come from?

    L Offline
    L Offline
    Lightshadown
    wrote on last edited by Lightshadown
    #5

    @jsulm from the Connect statement, im sure its because the main.cpp file its not declared as a QObject, its just a plain c++ file and i call all my widgets via pointer, so every signal falls out of scope and that way i cant use the slot system, i did try to move everything to my login file, but i ran into the same issue, how i return or call or whatever to my main file and simply show my main widget? the idea is as follows

    load main window -> load login -> show login -> wait for serial/username -> hide login -> show main window

    connect statement:

    QObject::connect(login->findChild<QLineEdit*>("Serial_edit"), &QLineEdit::returnPressed, [=] {    //login->Check();
               login->Check();
               if(login->validSerial == true){  //if serial is valid, triger when Check() happends   como carajos llamo la funcion Check aqui???
               login->findChild<QPushButton*>("Aceptar")->setCheckable(true); // 
               login->findChild<QPushButton*>("Aceptar")->setStyleSheet("border-width:1px; background-color:transparent; color:red");  // change button color
               //winPrin.show();    // base.show()  pantalla principal;
               base->CreateConfigFile(app.applicationDirPath(), "");  // Create config File, pass the valid serial here
             }
             if (login->validSerial == true){base->Log("Hubo un cambio, valid serial es true ");} else{base->Log("valid serial es False"); }
           });
    

    Im seriusly thinking on morphing my main.cpp into a Qobject, not so sure if thats a good idea but if it works its fine for me

    jsulmJ 1 Reply Last reply
    0
    • L Lightshadown

      @jsulm from the Connect statement, im sure its because the main.cpp file its not declared as a QObject, its just a plain c++ file and i call all my widgets via pointer, so every signal falls out of scope and that way i cant use the slot system, i did try to move everything to my login file, but i ran into the same issue, how i return or call or whatever to my main file and simply show my main widget? the idea is as follows

      load main window -> load login -> show login -> wait for serial/username -> hide login -> show main window

      connect statement:

      QObject::connect(login->findChild<QLineEdit*>("Serial_edit"), &QLineEdit::returnPressed, [=] {    //login->Check();
                 login->Check();
                 if(login->validSerial == true){  //if serial is valid, triger when Check() happends   como carajos llamo la funcion Check aqui???
                 login->findChild<QPushButton*>("Aceptar")->setCheckable(true); // 
                 login->findChild<QPushButton*>("Aceptar")->setStyleSheet("border-width:1px; background-color:transparent; color:red");  // change button color
                 //winPrin.show();    // base.show()  pantalla principal;
                 base->CreateConfigFile(app.applicationDirPath(), "");  // Create config File, pass the valid serial here
               }
               if (login->validSerial == true){base->Log("Hubo un cambio, valid serial es true ");} else{base->Log("valid serial es False"); }
             });
      

      Im seriusly thinking on morphing my main.cpp into a Qobject, not so sure if thats a good idea but if it works its fine for me

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @Lightshadown said in signal from one class into main app class:

      im sure its because the main.cpp file its not declared as a QObject

      No, you are connecting a lambda, so there is no need for main to "be" a QObject.
      Where exactly in your connect does the error come from? I mean is it somewhere inside the lambda?

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

      L 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #7

        The problem is the lazy and stupid usage of = in the lambda for no reason except... fix it by only passing what you need, and prefer a reference instead copying. You can't copy a Q(Core)Application or any other QObject.

        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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #8

          There's one thing here: encapsulation done wrong. There should be no need to put that much logic that belongs to your login screen in your main function.

          The idea rather is:

          LoginDialog dialog;
          int result = dialog.exec();
           
          if (result == QDialog::Rejected) {
              return 0;
          }
          // Do some setup
          // Main window creation
          return app.exec();
          

          And in your dialog you handle the login and validation.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • jsulmJ jsulm

            @Lightshadown said in signal from one class into main app class:

            im sure its because the main.cpp file its not declared as a QObject

            No, you are connecting a lambda, so there is no need for main to "be" a QObject.
            Where exactly in your connect does the error come from? I mean is it somewhere inside the lambda?

            L Offline
            L Offline
            Lightshadown
            wrote on last edited by
            #9

            @jsulm actually the problem its the connect/lambda itself, i cant show my mainwindow using my login widget, at least not directly, right now i created a new qwidget and call it from the main.cpp file, then call my login and mainwindow from there, that way i can use the slot/signal system, its the best i can come with.

            main.cpp --> launcher widget (show/hide) --> login.show --> user/serial correct --> login.hide --> show Mainwindow

            @SGaist im not sure im following, most of the logic inside main.cpp its for the mainwindow, also that return inside the if how can i used it properly? that might help me when the user hits the button accept after typing the serial/user

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #10

              @Lightshadown said in signal from one class into main app class:

              im not sure im following, most of the logic inside main.cpp its for the mainwindow, also that return inside the if how can i used it properly? that might help me when the user hits the button accept after typing the serial/user

              And that's one issue, you are implementing your whole application business logic in your main.cpp file.

              Each widget should be self-contained. All these calls to findChild shall not exists. I recommend you take the time to looks through some of Qt's examples and tutorials.

              When the user clicks that button, the validation should happen and the dialog accepted only if said validation succeeded.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              L 1 Reply Last reply
              1
              • SGaistS SGaist

                @Lightshadown said in signal from one class into main app class:

                im not sure im following, most of the logic inside main.cpp its for the mainwindow, also that return inside the if how can i used it properly? that might help me when the user hits the button accept after typing the serial/user

                And that's one issue, you are implementing your whole application business logic in your main.cpp file.

                Each widget should be self-contained. All these calls to findChild shall not exists. I recommend you take the time to looks through some of Qt's examples and tutorials.

                When the user clicks that button, the validation should happen and the dialog accepted only if said validation succeeded.

                L Offline
                L Offline
                Lightshadown
                wrote on last edited by
                #11

                @SGaist well i went to a diferent approach, i did an intermediated widget and thats the only one i call from my main now so its something like this

                QApplication app(argc, argv);
                Launcher* lanch = new Launcher();
                lanch->Init(app.applicationDirPath() );
                return app.exec;
                

                the program runs but wont show me any window execpt the one from Launcher itself, but the program stay on the background, so i suppose the windows are created and destroyed at the same time, i use Init(QString) to initialized both windows and any log or anything that happends inside works, I even manage to emit a signal to show my main window as follows

                Inside Login.h

                signals:
                     void ShowMainSignal();
                

                Inside Launcher.h

                public slots:
                    void ShowMainWindow();
                

                Launcher.cpp

                Silo* base = new Silo;
                Login* login = new Login;
                
                QObject::connect(&login, SIGNAL(ShowMainSignal), this, SLOT(ShowMainWindow()) );
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  What do you do in the slot ShowMainWindow ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  L 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    What do you do in the slot ShowMainWindow ?

                    L Offline
                    L Offline
                    Lightshadown
                    wrote on last edited by Lightshadown
                    #13

                    @SGaist im calling the main window from there, i manage to make it work but now everytime i close the main window a dialog appears " The document as change, do you wana save?", also now i have to figure it out how to close the login window, i believe im making it even worst, heres my actual code, i moved all the stuff to their respective widgets, so no more findChild

                    i belive i need to add something like this, to know when to hide the login dialog

                    //boolverifier.h
                    #include <cassert>
                    
                    class BoolVerifier
                    {
                    public:
                         BoolVerifier() = default;
                         explicit BoolVerifier(bool b) {assert(b); (void)(b);}
                         explicit BoolVerifier& operator=(bool b) {assert(b); (void)(b); return *this; }
                    };
                    

                    and use it like this

                    BoolVerifier b;
                    b = QObject::connect((login, SIGNAL(ShowMainSignal(QString)), this, SLOT(ShowMainWindow(QString))))
                    

                    Launcher.h

                    
                    #ifndef _LAUNCHER_H
                    #define _LAUNCHER_H
                    
                    #include <QLineEdit>
                    #include <QObject>
                    #include <QMessageBox>
                    #include <QSqlDatabase> // crea conecciones a base de datos
                    #include <QSqlQuery> // hace las busquedas y agrega data a los registros
                    #include <QSqlError> //hacer debug sql
                    #include <QPixmap>
                    #include <QLabel>
                    
                    #include "ui_Launcher.h"
                    #include "Silo_SaS.h"
                    #include "Login.h"
                    #include "Serial.h"
                    
                    //#include "framelesswindow.h"
                    #include "../Qt-Frameless-Window-DarkStyle/DarkStyle.h"  // se debia agregar el DarkStyle.cpp a sources files del projecto
                    #include "../Qt-Frameless-Window-DarkStyle/framelesswindow/framelesswindow.h"  // tambien se agrega el Framelesswindow.cpp al sources files
                    
                    
                    class Launcher : public QWidget {
                        Q_OBJECT
                    public:
                        Launcher();
                        void Init(QString);
                        virtual ~Launcher();
                    public slots:
                        void ShowMainWindow(QString);
                        
                    private:
                        Ui::Launcher widget;
                    };
                    
                    

                    Launcher.cpp

                    
                    #include "Launcher.h"
                    
                    Launcher::Launcher() {
                        widget.setupUi(this);
                        
                    }
                    void Launcher::Init(QString App){
                    
                        Silo* base = new Silo;
                        //Silo base;
                        base->Log("....                 \n..........               \nInicio del programa");
                        
                        //*********** Login Screen ******************//
                      
                        Login* login = new Login(); 
                        login->InitApp = App;  // here i pass the location of the app
                        QObject::connect(login, SIGNAL(ShowMainSignal(QString)), this, SLOT(ShowMainWindow(QString)));   // connection so i can show the main window
                        
                        login->setFixedSize(440,600);
                        login->setWindowTitle("Login");
                        login->setWindowIcon(QIcon("images/Icono_App_Silo_2.png"));
                        login->CheckLogin();  // check if the config file exist
                        login->show();        //login screen
                        
                        //Serial* Conecserial = new Serial;    // coneccion a bascula, *pendiente
                        //Conecserial->Connect_RS232();
                    
                        base->Log("Ok");      // indica que todo salio bien
                        
                    }
                    
                    void Launcher::ShowMainWindow(QString App){
                        //********** Show the main screen of the program, finally*****//  
                        
                        Silo* base = new Silo;
                        
                        base->setFixedSize(1920,1080);
                        base->setWindowState(Qt::WindowMaximized);
                        base->setWindowTitle("SAAS");
                        base->setWindowIcon(QIcon("images/Icono_App_Silo_1.png"));
                        //FramelessWindow winPrin;     // frameless window library wont work, it simply shows and hide at the same time
                        //winPrin.setStyle(new DarkStyle);
                        //winPrin.setContent(base);
                        //winPrin.setFixedSize(1920,1080);
                        //winPrin.setWindowState(Qt::WindowMaximized);
                        //winPrin.setWindowTitle("SAAS");
                        //winPrin.setWindowIcon(QIcon("images/Icono_App_Silo_1.png"));
                        
                        base->Log("Pantalla Principal");
                        base->InitSilo(App);  // inicialized the Main Screen, pantalla princiapal
                        base->InitDb(App);    // initialized the database
                        //winPrin.show();
                        base->show();
                        
                    }
                    Launcher::~Launcher() {
                    }
                    
                    

                    Login.h

                    class Login : public QWidget {
                        Q_OBJECT
                    public:
                        Login();  //(QString, QApplication);
                        virtual ~Login();
                        bool SortMAC(const QStringList &sort, QString);   // check if the given Mac Address its the same as the Mac stored in Config
                        QStringList getMacAddress();      // get the macaddres
                        bool checkConfigFile(QString);    // check if config file exist or not
                        QString CheckSerial(QString Cb);  //takes the mac addres and converts it from Hexa to Decimal
                        void CheckLogin();                // check if the config file exist and the serial is correct
                        QString InitApp;                  // here i pass the apps location
                        //bool validSerial = false; 
                        
                    signals:
                        void ShowMainSignal(QString);     // tells login when to show the main window
                    public slots:
                        void Check(); //(QString, QApplication);   // checks if the given serial is valid or not
                        void BotonAceptar();               // just the ok Button
                         
                    

                    Silo_Sas.h

                    class Silo : public QMainWindow {
                        Q_OBJECT
                    
                    public:
                        Silo();
                        virtual ~Silo();
                        void InitSilo(QString);  // initialized icons, background, etc
                        void InitDb(QString);  // initialized the Databases
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      Where does that message come from ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      L 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Where does that message come from ?

                        L Offline
                        L Offline
                        Lightshadown
                        wrote on last edited by Lightshadown
                        #15

                        @SGaist it simply happends everytime i close the window or invoque close(), i use close this way this->close() , nothing happends if i hit save or discard, its like a bug actually, do not affect the main program behaivior

                        c9abcf3c-6c1b-4e58-b1af-8c60d8024cb7-imagen.png

                        edit: ok forget it, already fix it, it was an old close warning that i completly forgot about it, everything is working fine now thanks a lot for the help.

                        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