目录
将工程文件进行注释
实现如下界面
头文件:widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H
源文件:widget.cpp:
#include "widget.h"
#include <QIcon>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//将图形化界面的名字改成Login screen(登录界面)
this->setWindowTitle("Login screen");
//将ui的图标改成想要的
this->setWindowIcon(QIcon("G:\\hq\\regular\\QT\\icon\\wodepeizhenshi.png"));
//设置ui界面的大小为合适的大小
this->setFixedSize(QSize(400,300));
//插入一个label,它的宽度与ui等宽,高度大约为整个ui界面高度的4/9
QLabel *lab1 = new QLabel(this);
lab1->resize(QSize(400,133));
lab1->move(0,0);
//label的内容要是一张图片
lab1->setPixmap(QPixmap("G:\\hq\\regular\\QT\\icon\\logo.png"));
//设置图片填充
lab1->setScaledContents(true);
//插入两个行编辑器,第一个用于输入账号
QLineEdit *edit1 = new QLineEdit(this);
edit1->resize(QSize(240,40));
edit1->move(110,150);
edit1->setPlaceholderText("账号");
//第二个用于输入密码
QLineEdit *edit2 = new QLineEdit(this);
edit2->resize(QSize(240,40));
edit2->move(edit1->x(),edit1->y()+55);
edit2->setPlaceholderText("密码");
//第二个回显模式是密码模式
edit2->setEchoMode(QLineEdit::Password);
//每个行编辑器前都要有一个label,内容为图片
QLabel *lab2 = new QLabel(this);
lab2->resize(50,40);
lab2->setPixmap(QPixmap("G:\\hq\\regular\\QT\\icon\\userName.jpg"));
lab2->setScaledContents(true);
lab2->move(edit1->x()-60,edit1->y());
QLabel *lab3 = new QLabel(this);
lab3->resize(50,40);
lab3->setPixmap(QPixmap("G:\\hq\\regular\\QT\\icon\\passwd.jpg"));
lab3->setScaledContents(true);
lab3->move(edit2->x()-60,edit2->y());
//要有两个pushbutton,一个是登录另一个是取消
QPushButton *btn1 = new QPushButton("登录",this);
QPushButton *btn2 = new QPushButton("取消",this);
//重设他们的尺寸
btn1->resize(QSize(60,30));
btn2->resize(btn1->size());
//移动他们的位置到合适
btn1->move(120,edit2->y()+55);
btn2->move(btn1->x()+120,edit2->y()+55);
//内容都会有一个图片
btn1->setIcon(QIcon("G:\\hq\\regular\\QT\\icon\\login.png"));
btn2->setIcon(QIcon("G:\\hq\\regular\\QT\\icon\\cancel.png"));
}
Widget::~Widget()
{
}