Solved signal from one class into main app class
-
Hello, im doing a user login for my program, the problem is i want to trigger a response only if the serial is valid after the user hit enter inside a QLineEdit and after that launch the program, im abble to do it with this code, wich is located in a separate file
QObject::connect(widget_Login.Serial_edit, SIGNAL(returnPressed()), this, SLOT(Check())); // senal de enter al introducir el serial
and i check if its valid here
void Login::Check(){ Silo* silo = new Silo; if (SortMAC(getMacAddress(), widget_Login.Serial_edit->text())){ silo->Log("Serial Valido"); widget_Login.Serial_aviso->setStyleSheet("color:green"); widget_Login.Serial_aviso->setText("Serial Valido"); validSerial = true; }else{ silo->Log("Serial NO VALIDO"); widget_Login.Serial_aviso->setStyleSheet("color:red"); widget_Login.Serial_aviso->setText("No Valido, intentelo de Nuevo"); validSerial = false; } }
but i need to know if its valid inside this code in the last if, this is located in my main program file,
if(login->checkConfigFile(app.applicationDirPath()) == true){ //check if file exist // check mac inside config vs machines mac QSqlQuery mac(QSqlDatabase::database("Config.db")); mac.exec("SELECT Mac FROM Config"); if(mac.lastError().isValid()){ base->Log("No se Puede obtener la Mac" + mac.lastError().text()); } if(login->SortMAC(login->getMacAddress(), mac.value("Mac").toString())){ // return true or false SortMac mac comparision goes here login->findChild<QPushButton*>("Aceptar")->setCheckable(true); // aceptar its checkable login->findChild<QLineEdit*>("Serial_edit")->setVisible(false); // hide or show serial_edit } else{ base->Log("No se puede comparar la Mac"); } }else{ login->findChild<QPushButton*>("Aceptar")->setCheckable(false); // hide the button login->findChild<QPushButton*>("Aceptar")->setStyleSheet("border-width:0px;background-color:white;"); // change button color login->findChild<QLineEdit*>("Serial_edit")->setVisible(true); // show lineedit for serial login->findChild<QLabel*>("Serial_aviso")->setText("Por favor Introduzca un Serial Valido"); if(){ // <<------if serial is valid, triger when Check() happends 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 } } winLog.show(); //login.show() pantalla de login; winPrin.show(); // base.show() pantalla principal;
i cant use the slots on my main program file because theres no Header file, and honestly i have no idea how to call it, any idea?
i need to give access only when the user enters a valid serial, theres any way to call for a slot or just the function?
do i need to move the entire Check() function to the main program and add somehitng else?
im lost on this one -
@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.
-
Hi,
One way to do that is to use a QDialog and an infinite loop in your main.cpp.
Continue presenting the dialog until either cancel is pressed or whatever condition you have gets good.
And the either exit or continue based on the dialog result.
-
sadly it didnt work, a simple do while only loops indefitnly, i did try using a lambda but it saids
QObject::connect(login->findChild<QLineEdit*>("Serial_edit"), &QLineEdit::returnPressed, [=]{ /* my code; */ } );
"use of deleted function 'QApplication::QApplication()'
meabe its the place where im trying to call the connect statement, i have 3 separate class files, im calling it inside the main.cpp file and that one its a normal file its not a QObject , im adding a pastebin link with my code .
actual code
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-copy" #include <QApplication> #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 "Silo_SaS.h" // #include "Pantalla_Principal.cpp" Cambiado a Silo_SaS.cpp #include "Serial.h" // Coneccion Serial a los puertos #include "Login.h" // Pantalla de login // framework DarkStyle de Jurgen-VikingGod #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 #pragma GCC diagnostic pop //extern QString ciclo_actual(){return QStringLiteral("CICLO-OI-19-20.db");} //extern QString users(){return QStringLiteral("Utilisateurs.db");} //extern QString adeudos(){return QStringLiteral("Dettes_OI_19-20.db");} int main(int argc, char *argv[]) { QApplication app(argc, argv); //app.setWindowIcon(QIcon("/images/Logo.png")); app.setWindowIcon(QIcon("/images/Icono_App_Silo_1")); Silo* base = new Silo; base->Log(". "); base->Log(". "); base->Log("Inicio del programa"); //********** Muestra la pantalla Principal del sistema*****// FramelessWindow winPrin; 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")); //*********** Pagina de Login*****************************// FramelessWindow winLog; Login* login = new Login; winLog.setStyle(new DarkStyle); winLog.setContent(login); winLog.setFixedSize(460,635); winLog.setWindowTitle("Login"); winLog.setWindowIcon(QIcon("images/Icono_App_Silo_2.png")); // winPrin.hide(); usar el hide asta que login sea true // revisar mariadbcpp y mariadb-connector-c // usar mysql_install_db para incializar la estructura de datos de mariadb, si existe pasar de largo // mysql.server start inicia el servidor mariadb // mysql -u root -p conecta a la base de datos (poner usuario y contraseƱa aqui) /**************************** Activar Serial ******************/ Serial* Conecserial = new Serial; Conecserial->Connect_RS232(); //******************* Fondos ****************************// QString icon_boletas = app.applicationDirPath() + "/images/Bascula1_edit.png"; QString icon_liqui = app.applicationDirPath() + "/images/Liquidacion1_edit.png"; QString icon_Salidas = app.applicationDirPath() + "/images/Bascula1_edit.png"; QString icon_reg = app.applicationDirPath() + "/images/Registros1_edit.png"; QString icon_produ = app.applicationDirPath() + "/images/Silo1_edit.png"; QString icon_comp = app.applicationDirPath() + "/images/Compradores1_edit.png"; QString icon_Silos = app.applicationDirPath() + "/images/Silo1_edit.png"; QString Logo_Emp = app.applicationDirPath() + "/images/Logo.png"; QString fondo = app.applicationDirPath() + "/images/SorgoPlanta1.jpg"; QString fondoApp = app.applicationDirPath() + "/images/SorgoPlanta2.jpg" ; QPixmap logo; if (!logo.load(fondo)){ base->Log("imposible cargar la imagen: " + fondo); }else{ base->findChild<QLabel*>("SplashScreenLogo")->setPixmap(logo); login->findChild<QLabel*>("LoginSplash")->setPixmap(logo); } if (!logo.load(fondoApp)){ base->Log("imposible cargar la imagen: " + fondoApp); }else{ base->findChild<QLabel*>("Fondo_1")->setPixmap(logo); base->findChild<QLabel*>("Fondo_2")->setPixmap(logo); base->findChild<QLabel*>("Fondo_3")->setPixmap(logo); base->findChild<QLabel*>("Fondo_4")->setPixmap(logo); base->findChild<QLabel*>("Fondo_5")->setPixmap(logo); base->findChild<QLabel*>("Fondo_6")->setPixmap(logo); base->findChild<QLabel*>("Fondo_7")->setPixmap(logo); base->findChild<QLabel*>("Fondo_8")->setPixmap(logo); base->findChild<QLabel*>("Fondo_9")->setPixmap(logo); base->findChild<QLabel*>("Fondo_10")->setPixmap(logo); base->findChild<QLabel*>("Fondo_11")->setPixmap(logo); } // operadores terciarios son igual al if !logo.load(icon_boletas) ? base->Log("imposible cargar la imagen: " + icon_boletas) : base->findChild<QLabel*>("icon_Boletas")->setPixmap(logo); !logo.load(icon_liqui) ? base->Log("imposible cargar la imagen: " + icon_liqui) : base->findChild<QLabel*>("icon_Liq")->setPixmap(logo); !logo.load(icon_Salidas) ? base->Log("imposible cargar la imagen: " + icon_Salidas) : base->findChild<QLabel*>("icon_Salidas")->setPixmap(logo); !logo.load(icon_reg)? base->Log("imposible cargar la imagen: " + icon_reg) : base->findChild<QLabel*>("icon_Reg")->setPixmap(logo); !logo.load(icon_Silos) ? base->Log("imposible cargar la imagen: " + icon_Silos) : base->findChild<QLabel*>("icon_Silos")->setPixmap(logo); !logo.load(icon_produ) ? base->Log("imposible cargar la imagen: " + icon_produ) : base->findChild<QLabel*>("icon_Prod")->setPixmap(logo); !logo.load(icon_comp) ? base->Log("imposible cargar la imagen: " + icon_comp) : base->findChild<QLabel*>("icon_Comp")->setPixmap(logo); !logo.load(Logo_Emp) ? base->Log("imposible cargar la imagen: " + Logo_Emp) : base->findChild<QLabel*>("Nombre_Programa_Landing")->setPixmap(logo); !logo.load(Logo_Emp) ? base->Log("imposible cargar la imagen: " + Logo_Emp) : base->findChild<QLabel*>("Nombre_Programa")->setPixmap(logo); base->findChild<QLabel*>("Nombre_Programa_Landing")->setFixedSize(430,620); base->findChild<QLabel*>("Nombre_Programa")->setFixedSize(290,250); /**************** Creacion de las Bases de Datos *************************/ // usar los if para revisar que existan las bases de datos, si no crearlas base->Log("Creacion de Bases de Datos"); QMessageBox MBox; QPushButton *boton_OK = MBox.addButton("Ok", QMessageBox::AcceptRole); QPushButton *boton_CANCEL = MBox.addButton(QMessageBox::Cancel); MBox.setWindowTitle("Error de Base Datos"); MBox.setDefaultButton(boton_OK); if (base->ChecarDB(app.applicationDirPath(), base->ciclo_actual()) == false){ // QMessageBox::critical( base, "Error de Database", "No se pudo encontrar la Base de Datos " + base->ciclo_actual(), QMessageBox::Ok | QMessageBox::Cancel); MBox.setText("No se Pudo encontrar la base de datos: " + base->ciclo_actual()); MBox.setInformativeText("Desea crearla?"); MBox.exec(); if (MBox.clickedButton() == boton_OK){ base->CrearDB(app.applicationDirPath(), base->ciclo_actual()); base->Log("Se creo la DB: " + base->ciclo_actual()); }else if(MBox.clickedButton() == boton_CANCEL){ } }/*else{ base->CrearDB(app.applicationDirPath(), base->ciclo_actual()); }*/ if (base->ChecarDB(app.applicationDirPath(), base->users()) == false){ // QMessageBox::critical( base, "Error de Database", "No se pudo encontrar la Base de Datos " + base->users() ,QMessageBox::Ok); MBox.setText("No se Pudo encontrar la base de datos: " + base->users()); MBox.setInformativeText("Desea crearla?"); MBox.exec(); if (MBox.clickedButton() == boton_OK){ base->CrearDB(app.applicationDirPath(), base->users()); base->Log("Se creo la DB: " + base->users()); } else if(MBox.clickedButton() == boton_CANCEL){ } }/*else{ base->ChecarDB(app.applicationDirPath(), base->users()); }*/ if (base->ChecarDB(app.applicationDirPath(), base->adeudos()) == false){ //QMessageBox::critical( base, "Error de Database", "No se pudo encontrar la Base de Datos " + base->adeudos() ,QMessageBox::Ok); MBox.setText("No se Pudo encontrar la base de datos: " + base->adeudos()); MBox.setInformativeText("Desea crearla?"); MBox.exec(); if (MBox.clickedButton() == boton_OK){ base->CrearDB(app.applicationDirPath(), base->adeudos()); base->Log("Se creo la DB: " + base->adeudos()); } else if(MBox.clickedButton() == boton_CANCEL){ } }/*else{ base->ChecarDB(app.applicationDirPath(), base->adeudos()); }*/ base->Contrasenas(); base->Creditos(); base->Ciclo(); base->Dry_n_Wet(); // secado //********* Manejo del Serial y mostrar pantallas *********************// //login(); //(app.applicationDirPath(), app); // le paso la direccion de la app winLog.show(); //login.show() pantalla de login; if(login->checkConfigFile(app.applicationDirPath()) == true){ //check if file exist // check mac inside config vs machines mac QSqlQuery mac(QSqlDatabase::database("Config.db")); mac.exec("SELECT Mac FROM Config"); if(mac.lastError().isValid()){ base->Log("No se Puede obtener la Mac" + mac.lastError().text()); } if(login->SortMAC(login->getMacAddress(), mac.value("Mac").toString())){ // return true or false SortMac mac comparision goes here login->findChild<QPushButton*>("Aceptar")->setCheckable(true); // aceptar its checkable login->findChild<QPushButton*>("Aceptar")->setStyleSheet("background-color:green;"); login->findChild<QLineEdit*>("Serial_edit")->setVisible(false); // hide or show serial_edit } else{ base->Log("No se puede comparar la Mac"); } }else{ login->findChild<QPushButton*>("Aceptar")->setCheckable(false); // hide the button login->findChild<QPushButton*>("Aceptar")->setStyleSheet("border-width:0px;background-color:white;"); // change button color login->findChild<QLineEdit*>("Serial_edit")->setVisible(true); // show lineedit for serial login->findChild<QLabel*>("Serial_aviso")->setText("Por favor Introduzca un Serial Valido"); base->Log("antes del connect"); 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"); } }); } //winLog.show(); //login.show() pantalla de login; //if(login->validSerial == true){ // winPrin.show(); // base.show() pantalla principal; //} base->Log("Se Muestran las pantallas"); return app.exec(); }
pastebin link...... here
-
@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.