четверг, 5 августа 2021 г.

Login Password Project

Login Password project

(Gui Programming)


1. LoginPassword.pro


QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    
# disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    secDialog.cpp \
    widget.cpp

HEADERS += \
    secDialog.h \
    widget.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target



2. Headers - widget.h


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLineEdit>
#include <QPushButton>

#include"secDialog.h"

class Widget : public QWidget
{
    Q_OBJECT  //macros object

public:
    Widget(QWidget * parent = 0);

private slots:
    void onPushButtonLoginClicked();
    void onQuitMessage();

private:
    QLineEdit *username;
    QLineEdit *password;
    QPushButton *login;
    QPushButton *cancel;
};

#endif // WIDGET_H



3. Headers - secDialog.h


#ifndef SECDIALOG_H
#define SECDIALOG_H

#include <QDialog>
#include <QTextEdit>
#include <QLabel>

class secDialog : public QDialog
{
    Q_OBJECT

public:
     secDialog();

private:
    QLabel *text;

};

#endif // SECDIALOG_H



4. Sources - main.cpp


#include "widget.h"

#include <QApplication>

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

    w.setWindowTitle("Login");

    return a.exec();
}



5. Sources - widget.cpp


#include "widget.h"

#include <QVBoxLayout>
#include <QMessageBox>

Widget::Widget(QWidget * parent) : QWidget(parent)
{
    username = new QLineEdit();
    username->setStyleSheet("color : rgb(140, 0, 140, 250)}");
    username -> setPlaceholderText("Username");

    password = new QLineEdit();
    password->setEchoMode(QLineEdit::Password);
    password->setStyleSheet("color : rgb(150, 0, 150, 250)}");
    password -> setPlaceholderText("Password");

    login = new QPushButton("Login");
    cancel = new QPushButton ("Close");


    QVBoxLayout * lyt = new QVBoxLayout(this);
    lyt->addWidget(username);
    lyt->addWidget(password);
    lyt->addWidget(login);
    lyt->addWidget(cancel);
    setLayout(lyt);


    connect(login, SIGNAL(clicked()), this,
 SLOT(onPushButtonLoginClicked()));
    connect(cancel, SIGNAL(clicked()), this, SLOT(onQuitMessage()));

    setFixedWidth(225);
    setFixedHeight(150);

}


void Widget::onPushButtonLoginClicked()
{
    QString userText = username->text();
    QString passwordText = password ->text();


    if(userText == "gui" && passwordText == "programming")  

    {

                close();  //username, password window is closing
                secDialog secdialog;
                secdialog.setModal(true);   //main page
                secdialog.exec();
    }
    else
    {
     QMessageBox::warning(this,"Login",
                          "Username and password is not correct");
    }
}



void Widget::onQuitMessage()
  {
    int o = QMessageBox::question(this, "Exit", "Are u sure to exit?",
                                  QMessageBox::Yes, QMessageBox::No);
    if (o == QMessageBox::Yes)
    {
      this->close();
    }

   }



6. Sources - secDialog.cpp


#include "secDialog.h"
#include <QVBoxLayout>

secDialog::secDialog()
{

    setFixedWidth(225);   //laynutyuny fix
    setFixedHeight(150);

    text = new QLabel("This is your page");
    text->setStyleSheet("color : rgb(238, 130, 238, 255)}");

    QVBoxLayout *vlyt = new QVBoxLayout();
    vlyt->addWidget(text);
    setLayout(vlyt);

}



Result:



































Комментариев нет:

Отправить комментарий

Login Password Project

Login Password project (Gui Programming) 1. LoginPassword.pro QT += core gui greaterThan ( QT_MAJOR_VERSION , 4): QT += widge...