自己制作一个登录界面
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QApplication>
#include <QLineEdit>
#include <QLabel>
#include <QMovie>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H
源文件:
#include "widget.h"
#include <iostream>
#include <QDebug>
#include <QIcon>
#include<QPushButton>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
qDebug() <<this->size();
//重置程序框大小
this->setFixedSize(800,600);
qDebug()<<this->windowTitle();
//给程序框重命名并添加图片
this->setWindowTitle("my joker");
this->setWindowIcon(QIcon("C:\\Users\\LENOVO\\Desktop\\joker.png"));
//设置登录按钮
QPushButton *btn1=new QPushButton(QIcon("C:\\Users\\LENOVO\\Desktop\\joker.png"),"登录",this);
btn1->setParent(this);
btn1->resize(100,100);
btn1->move(150,450);
//设置取消按钮
QPushButton *btn2=new QPushButton(QIcon("C:\\Users\\LENOVO\\Desktop\\joker.png"),"取消",this);
btn2->resize(btn1->size());
btn2->move(btn1->x()+300,btn1->y());
//添加动图1
QMovie *Movie = new QMovie("C:\\Users\\LENOVO\\Desktop\\ccc.gif"); //从文件中接收gif图
QLabel *imgLabel = new QLabel(this); //创建标签
imgLabel ->setGeometry(200,250,550,200); //设置标签大小及位置
imgLabel ->setMovie(Movie); //将gif图插入标签
Movie->start(); //播放gif图,否则图片无法显示
//添加动图2
QMovie *Movie2 = new QMovie("C:\\Users\\LENOVO\\Desktop\\ddd.gif"); //从文件中接收gif图
QLabel *imgLabel2 = new QLabel(this); //创建标签
imgLabel2 ->setGeometry(200,300,550,200); //设置标签大小及位置
imgLabel2 ->setMovie(Movie2); //将gif图插入标签
Movie2->start(); //播放gif图,否则图片无法显示
Movie2->resized(QSize(50,30));
//添加账号文本框
QLineEdit *edit1=new QLineEdit(this);
edit1->resize(200,30);
edit1->move(imgLabel->x()+65,imgLabel->y()+90);
edit1->setMaxLength(11);
edit1->setPlaceholderText("joker账号/手机/邮箱");
edit1->setAlignment(Qt::AlignCenter);
//添加密码文本框
QLineEdit *edit2=new QLineEdit(this);
edit2->resize(200,30);
edit2->move(imgLabel->x()+65,imgLabel->y()+140);
edit2->setEchoMode(QLineEdit::Password);
edit2->setMaxLength(11);
edit2->setPlaceholderText("joker密码");
edit2->setAlignment(Qt::AlignCenter);
//添加顶层图片
QLabel* lab2=new QLabel(this);
lab2->setPixmap(QPixmap("C:\\Users\\LENOVO\\Desktop\\joker2.png"));
lab2->resize(800,320);
}
Widget::~Widget()
{
}
测试文件:
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
运行效果:
思维导图