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.
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
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
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 😕
Please watch this video https://youtu.be/6_elY8O20I8
can you provide the file:
“:/resources/img/login-icon.png”
hello i want to make a loginApp and combine the webengineview how can i do that both ?