Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Form Login with QDialog



  • Hi Guys,

    I'm trying to create a login form using QDialog, but when I click Login, I can not give .exec () on mainWindow which would be the panel for my program.

    I wanted to do something so that I would type the login and open the panel and delete the form login.

    Does anyone have any ideas?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Please show the code you are using.



  • MAIN.CPP

    #include "logar.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    logar w;
    w.show();

    return a.exec();
    

    }

    LOGAR.CPP

    #include "logar.h"
    #include "ui_logar.h"
    #include <QMessageBox>
    #include "menuinicial.h"
    #include <fstream>
    #include <cadastrar.h>

    logar::logar(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::logar)
    {
    ui->setupUi(this);
    connect(ui->btnEntrar,SIGNAL(on_btnEntrar_clicked()),this,SLOT(validacaoLogin()));
    }

    logar::~logar()
    {
    delete ui;
    }

    bool logar::validacaoLogin(){
    QString usuario = ui->txtNome->text();
    QString senha = ui->txtSenha->text();

    using namespace std;
    ofstream fout("C:\\Users\\Caio\\Documents\\login.txt", ios::app);
    
    fout <<usuario.toStdString()<<endl;
    fout <<senha.toStdString()<<endl;
    
    if(usuario == "Caio Sousa" && senha =="12345"){
        return true;
    
    }else{
        return false;
    }
    

    }

    void logar::validacaoFuncionarios(){
    using namespace std;
    ifstream fin("C:\Users\Caio\Documents\login.txt");
    char temp;
    QString buffer;
    while (fin.get(temp)) {
    buffer.push_back(QChar(temp));

    }
    

    }

    void logar::on_btnEntrar_clicked()

    {

     validacaoFuncionarios();
    
    if(validacaoLogin()==true){
    
        //Here should show the mainWindow(Menu)
    
    
    
    }else{
        QMessageBox::warning(this,"Login","Usuario ou senha incorretos");
    }
    

    }

    void logar::on_btnCadastrar_clicked()
    {
    cadastrar cadastra;
    cadastra.exec();
    }

    LOGAR.H

    #ifndef LOGAR_H
    #define LOGAR_H

    #include <QDialog>
    #include <QtCore>

    namespace Ui {
    class logar;
    }

    class logar : public QDialog
    {
    Q_OBJECT

    public:
    explicit logar(QWidget *parent = nullptr);
    ~logar();

    private slots:
    void on_btnEntrar_clicked();
    bool validacaoLogin();
    void validacaoFuncionarios();

    void on_btnCadastrar_clicked();
    

    private:
    Ui::logar *ui;
    QFile *file;
    QTextStream stream;
    };

    #endif // LOGAR_H

    MENUINICIAL.H

    #ifndef MENUINICIAL_H
    #define MENUINICIAL_H

    #include <QMainWindow>

    namespace Ui {
    class MenuInicial;
    }

    class MenuInicial : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MenuInicial(QWidget *parent = nullptr);
    ~MenuInicial();

    private slots:
    void on_btnBuscar_clicked();

    private:
    Ui::MenuInicial *ui;
    };

    #endif // MENUINICIAL_H

    MENUINICIAL.CPP

    #include "menuinicial.h"
    #include "ui_menuinicial.h"
    #include "cadastroquarto.h"

    MenuInicial::MenuInicial(QWidget *pai) :
    QMainWindow(pai),
    ui(new Ui::MenuInicial)
    {
    ui->setupUi(this);
    }

    MenuInicial::~MenuInicial()
    {
    delete ui;
    }

    void MenuInicial::on_btnBuscar_clicked()
    {
    cadastroQuarto quarto;
    quarto.exec();
    }


  • Lifetime Qt Champion

    You're doing way too much stuff in your login dialog.

    You should only be doing the validation there, and in the main function show the the MainWindow if the validation passes. You can use QDialog's exec function to run your dialog before continuing.



  • I'm still learning using QT Creator, but the tutorial I'm seeing is not teaching you how to use the main class the right way


  • Lifetime Qt Champion

    What main class ? If you mean the main function, you won't find tutorials for each and every possible way to write an application.


Log in to reply