创建登录界面
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(700,800);
//1.实例化一个标签,设置上面界面
QLabel *lab1 = new QLabel(this);//设置父组件
lab1 -> resize(700,300);//设置大小
lab1->setPixmap(QPixmap("C:\\Users\\孟孟孟婷丽\\Desktop\\wangzhe.jpg"));//设置内容为图片
lab1->setScaledContents(true);//设置内容自适应
//2.实例化一个标签,设置账号
QLabel *lab2 = new QLabel(this);//设置父组件
lab2 -> move(150,400);//设置位置
lab2->setPixmap(QPixmap("C:\\Users\\孟孟孟婷丽\\Desktop\\denglu.png"));//设置内容为图片
//3.实例化一个标签,设置密码
QLabel *lab3 = new QLabel(this);//设置父组件
lab3 -> move(150,lab2->y()+50);//设置位置
lab3->setPixmap(QPixmap("C:\\Users\\孟孟孟婷丽\\Desktop\\mima.png"));//设置内容为图片
//1.构造一个行编辑器,构造时给定父组件
QLineEdit *edit1 = new QLineEdit(this);
edit1->setPlaceholderText("QQ/手机/邮箱"); //设置编辑器的占位文本
edit1->resize(300,40); //设置尺寸
edit1->move(lab1->x()+250,400);//移动位置
//edit1->setEnabled(false); //设置不可用状态
//2.构造一个行编辑器,构造时给定父组件以及文本内容
QLineEdit *edit2 = new QLineEdit("llllll",this);
qDebug() << edit2->text(); //获取行文本编辑器中文本内容
edit2->resize(edit1->size());
edit2->move(lab1->x()+250,edit1->y()+50);
edit2->setEchoMode(QLineEdit::Password); //设置回显模式
//1.构造一个按钮时,指定父组件
QPushButton *btn1 = new QPushButton(this); //将当前界面设置成父组件
btn1->setText("微信登录");
btn1->resize(btn1->size());
btn1->move(edit2->x(),edit2->x()+300);
btn1->setIcon(QIcon("C:\\Users\\孟孟孟婷丽\\Desktop\\w.png"));
//2.构造一个按钮时,指定父组件
QPushButton *btn2 = new QPushButton(this); //将当前界面设置成父组件
btn2->setText("qq登录");
btn2->resize(btn1->size());
btn2->move(btn1->x()+100,edit2->x()+300);
btn2->setIcon(QIcon("C:\\Users\\孟孟孟婷丽\\Desktop\\qie.png"));
//2.构造一个按钮时,指定父组件
QPushButton *btn3 = new QPushButton(this); //将当前界面设置成父组件
btn3->setText("取消登录");
btn3->resize(btn1->size());
btn3->move(btn1->x()+200,edit2->x()+300);
btn3->setIcon(QIcon("C:\\Users\\孟孟孟婷丽\\Desktop\\cuowu.png"));
}
Widget::~Widget()
{
delete ui;
}
1.配置文件 。pro文件
QT += core gui
#QT工程所需的类库 core是核心库 gui图形化界面相关类库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#版本超过4.0,会加上widgets
CONFIG += c++11
#该编译器支持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 \
widget.cpp
#管理头文件
HEADERS += \
widget.h
#管理ui文件
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2.头文件
#ifndef WIDGET_H
#define WIDGET_H //防止文件重复包含
#include <QWidget> //QWidget类所在的头文件,父类头文件
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }//命名空间的声明
QT_END_NAMESPACE
//定义属于自己的类,Widget是类名,公共继承自QWidget
class Widget : public QWidget
{
Q_OBJECT //信号与槽的元对象
public:
Widget(QWidget *parent = nullptr); //构造函数的声明,有一个默认参数的形参
~Widget(); //析构函数的声明
private:
Ui::Widget *ui;//后期可以通过ui指针找到ui界面上拖拽出来的组件
};
#endif // WIDGET_H
3.源文件
#include "widget.h" //自己的头文件
#include "ui_widget.h" //ui界面对应的头文件
Widget::Widget(QWidget *parent) //构造函数的定义
: QWidget(parent) //显性调用父类的构造函数完成对子类从父类继承下来成员的初始化工作
, ui(new Ui::Widget)//对自己类中的指针成员开辟空间
{
ui->setupUi(this); //给拖拽出来的组件实例化空间
}
Widget::~Widget() //析构函数的定义
{
delete ui; //释放ui指针的内存
}
4.主程序
#include "widget.h" //图形化界面的头文件
#include <QApplication> //应用程序的头文件
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //实例化一个应用程序的对象,调用的是有参构造
Widget w; //在栈区实例化自定义类的对象
w.show(); //调用show函数,展示图形化界面,该函数是父类提供的,直接用即可
return a.exec();//为了阻塞界面不被关闭,等待相关事情发生
} //等待信号与槽,时间处理,等待用户操作