Solved signal from one class into main app class
-
@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?
-
@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
-
@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? -
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. -
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.
-
@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
-
@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.
-
@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 followsInside 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()) );
-
What do you do in the slot ShowMainWindow ?
-
@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
-
Where does that message come from ?
-
@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 behaivioredit: 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.