Qt Tutorials For Beginners – Creating Simple Login Form in QT




Qt Tutorials For Beginners
Qt Tutorials For Beginners

In this tutorial we will learn How to create a Simple Login Form in QT

File->New File or Project…

Applications->Qt Gui Application->Choose…

We keep the class as MainWindow as given by default.

Now We will create the second dialog by following step

Right Click Project -> New -> Qt -> Qt Designer Form class -> Choose -> Dialog without Button

Give the class name “SecDialog” and click finish.

qt Login App Project Structure
qt Login App Project Structure

LoginApp.pro

#-------------------------------------------------
#
# Project created by QtCreator 2016-04-18T22:31:03
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = LoginApp
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    secdialog.cpp

HEADERS  += mainwindow.h \
    secdialog.h

FORMS    += mainwindow.ui \
    secdialog.ui

RESOURCES += \
    resource.qrc

Now we will design our mainwindow.ui file

Qt Login fom design

Qt Login fom design

qt second form design
qt second form design

main.cpp

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

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

    return a.exec();
}

 

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "secdialog.h"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_Login_clicked();

private:
    Ui::MainWindow *ui;
    SecDialog *secDialog;
};

#endif // MAINWINDOW_H

 

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QPixmap>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPixmap pix(":/resources/img/login-icon.png");
    int w = ui->label_pic->width();
    int h = ui->label_pic->height();
    ui->label_pic->setPixmap(pix.scaled(w,h,Qt::KeepAspectRatio));

}

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

void MainWindow::on_pushButton_Login_clicked()
{
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_password->text();

    if(username ==  "test" && password == "test") {
        QMessageBox::information(this, "Login", "Username and password is correct");
        //hide();
        secDialog = new SecDialog(this);
        secDialog->show();
    }
    else {
        QMessageBox::warning(this,"Login", "Username and password is not correct");
    }
}

 

secdialog.h

#ifndef SECDIALOG_H
#define SECDIALOG_H

#include <QDialog>

namespace Ui {
class SecDialog;
}

class SecDialog : public QDialog
{
    Q_OBJECT

public:
    explicit SecDialog(QWidget *parent = 0);
    ~SecDialog();

private:
    Ui::SecDialog *ui;
};

#endif // SECDIALOG_H

 

secdialog.cpp

#include "secdialog.h"
#include "ui_secdialog.h"

SecDialog::SecDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SecDialog)
{
    ui->setupUi(this);
}

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

 

resource.qrc

<RCC>
    <qresource prefix="/resources">
        <file>img/login-icon.png</file>
    </qresource>
</RCC>

 

Output

Qt Login fom login fail
Qt Login fom login fail
Qt Login fom output success
Qt Login fom output success
Qt Login second fom
Qt Login second fom

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





4 Comments

  1. I tried this code but when i build, Error occurs that “‘class Ui::MainWindow’ has no member named ‘lineEdit_username’QString username = ui->lineEdit_username->text()”
    What should i do 😕

Leave a Reply

Your email address will not be published.


*