制作一个登陆界面
login.pro文件
QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += 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.0SOURCES += \main.cpp \widget.cppHEADERS += \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
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_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();//使用无参构造标签QLabel *label1=new QLabel; //背景标签QLabel *label2=new QLabel; //用户文本框标签QLabel *label3=new QLabel; //密码文本框标签QLineEdit *edit1=new QLineEdit;//用户行编辑器QLineEdit *edit2=new QLineEdit;//密码行编辑器QPushButton *btn1=new QPushButton; //登陆按钮QPushButton *btn2=new QPushButton; //退出按钮
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
widget.cpp
#include "widget.h"Widget::Widget(QWidget *parent): QWidget(parent)
{//1.固定当前界面的尺寸this->setFixedSize(800,600); //调用函数设置宽和高//2.窗口标题的设置this->setWindowTitle("Blog");//3.设置窗口图标this->setWindowIcon(QIcon("C:\\Users\\Administrator\\Desktop\\Insert\\QT\\icon\\blog.jfif"));//4.设置背景色,可以使用qss代码完成样式表this->setStyleSheet("background-color:lightyellow;");//5.设置窗口透明度this->setWindowOpacity(1.0);//设置标签//利用标签设置背景label1->setParent(this);label1->resize(800,300);label1->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\Insert\\QT\\icon\\background.jpg")); //设置图片标签label1->setScaledContents(true); //设置内容自适应//利用标签设置 用户和密码//用户文本框标签label2->setParent(this);label2->resize(40,40);label2->move(200,350);label2->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\Insert\\QT\\icon\\login.png")); //设置图片标签label2->setScaledContents(true); //设置内容自适应//密码文本框标签label3->setParent(this);label3->resize(40,40);label3->move(200,450);label3->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\Insert\\QT\\icon\\pwd.png")); //设置图片标签label3->setScaledContents(true); //设置内容自适应//用户行编辑器edit1->setParent(this); //设置父组件edit1->resize(250,40); //重新设置尺寸edit1->move(270,350); //移动位置edit1->setPlaceholderText("username");//密码行编辑器edit2->setParent(this);edit2->resize(edit1->size());edit2->move(270,450);edit2->setEchoMode(QLineEdit::Password); //设置密文模式edit2->setPlaceholderText("password"); //设置占位符//登陆按钮btn1->setParent(this); //把当前界面当成父组件btn1->setText("Login"); //设置按钮上的文本内容btn1->resize(100,50);btn1->move(240,530); //移动按钮的位置btn1->setStyleSheet("background-color:khaki;"); //设置背景色btn1->setIcon(QIcon("C:\\Users\\Administrator\\Desktop\\Insert\\QT\\icon\\login_1.png")); //设置按钮图标//退出按钮btn2->setParent(this); //把当前界面当成父组件btn2->setText("Exit"); //设置按钮上的文本内容btn2->resize(btn1->size());btn2->move(480,530); //移动按钮的位置btn2->setStyleSheet("background-color:khaki;"); //设置背景色btn2->setIcon(QIcon("C:\\Users\\Administrator\\Desktop\\Insert\\QT\\icon\\exit.png")); //设置按钮图标
}Widget::~Widget()
{
}
界面展示
思维导图