sec.h
#include "widget.h"
#include "sec.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
sec s;
// s.show();
QObject::connect(&w,&Widget::mysignal,&s,&sec::open_slot);
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
QLabel *lab;
QLabel *lab1;
QLabel *lab2;
QLineEdit *edit1;
QLineEdit *edit2;
QPushButton *btn1;
QPushButton *btn2;
signals:
void mysignal();
public slots:
void cancel_slot();
void login_slot();
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include "sec.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
sec s;
// s.show();
QObject::connect(&w,&Widget::mysignal,&s,&sec::open_slot);
return a.exec();
}
sec.cpp
#include "sec.h"
#include "ui_sec.h"
sec::sec(QWidget *parent) :
QWidget(parent),
ui(new Ui::sec)
{
ui->setupUi(this);
}
sec::~sec()
{
delete ui;
}
void sec::open_slot()
{
this->show();
}
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setFixedSize(700,500); //设置固定尺寸
this->setWindowTitle("鹏哥登录"); //设置窗口标题
this->setWindowIcon(QIcon(":/icon/QQ.png"));//设置图标
this->setMaximumSize(1000,800); //设置最大尺寸
this->setMinimumSize(100,100); //设置最小尺寸
lab = new QLabel(this);//设置标签
lab->resize(700,200); // 设置大小
lab->setPixmap(QPixmap(":/icon/logo.png")); //填充图片
lab->setScaledContents(true); //图片自适应
lab1 = new QLabel(this);
lab1->setPixmap(QPixmap(":/icon/denglu.png"));
lab1->resize(40,40);
lab1->move(150,220);
lab1->setScaledContents(true);
lab2 = new QLabel(this);
lab2->setPixmap(QPixmap(":/icon/denglumima.png"));
lab2->resize(40,40);
lab2->move(150,280);
lab2->setScaledContents(true);
edit1 = new QLineEdit(this); //设置行编辑
edit1->resize(300,40); //重新设置尺寸
edit1->move(200,220); //移动
edit1->setPlaceholderText("账号"); //设置占位符
edit2 = new QLineEdit(this);
edit2->resize(300,40); //重新设置尺寸
edit2->move(200,280); //移动
edit2->setEchoMode(QLineEdit::Password); //设置密文模式
edit2->setPlaceholderText("密码"); //设置占位符
btn1 = new QPushButton;
btn1->setParent(this); //将当前界面设置成父组件
// btn1->setIcon(QIcon("E:\\032QT\01program\\icon_h8db9qyxft\\denglu_1.png")); //设置按钮图标
btn1->setIcon(QIcon(":/icon/login.png")); //设置按钮图标
btn1->resize(40,40); //重新设置组件的大小
btn1->move(250,350); //移动组件
connect(btn1,SIGNAL(clicked()),this,SLOT(cancel_slot()));
btn2 = new QPushButton;
btn2->setParent(this); //将当前界面设置成父组件
btn2->setIcon(QIcon(":/icon/quxiao.png")); //设置按钮图标
btn2->resize(40,40); //重新设置组件的大小
btn2->move(380,350); //移动组件
connect(btn2,&QPushButton::clicked,this,&Widget::login_slot);
}
void Widget::cancel_slot()
{
if(edit1->text()=="admin"&&edit2->text()=="123456")
{
qDebug()<<"成功";
}
else{
qDebug()<<"失败";
}
emit mysignal();
this->close();
}
void Widget::login_slot()
{
this->close();
}
Widget::~Widget()
{
}
工程文件
QT += core gui
#表示引用qt所需的类库,如核心库、图形化界面库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#如果超过4.0版本,系统会自动加上widgets库
CONFIG += c++11
#该版本的qt支持C++11后的语法
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
#管理源文件
SOURCES += \
main.cpp \
sec.cpp \
widget.cpp
#管理头文件
HEADERS += \
sec.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
RESOURCES += \
icond.qrc
DISTFILES +=
#管理ui文件
FORMS += \
sec.ui