1.用代码实现简单的图形化界面(并将工程文件注释)
头文件
#ifndef MYWIDGET_H
#define MYWIDGET_H //防止头文件冲突
#include <QWidget> //父类的头文件
class MyWidget : public QWidget //自定义自己的界面类,公共继承自QWidget,父类中重写了绘制事件处理函数
{
Q_OBJECT //信号与槽的元对象,没有这个对象,信号与槽就不能使用了
public:
MyWidget(QWidget *parent = nullptr); //构造函数的声明,并且带有一个默认的形参
~MyWidget(); //析构函数的声明
};
#endif // MYWIDGET_H
源文件
#include "mywidget.h"
#include<QIcon>
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
this->resize(500,300);
this->setWindowTitle("我的第一个图形化弹窗");
this->setWindowIcon(QIcon("D:\\QTcreate\\Day8\\04homework\\icon\\logo"));
//创建三个标签
QLabel *lab1 = new QLabel(this);
lab1->resize(500,120);
// lab1->setAlignment(Qt::AlignCenter); //用来消除空白的
lab1->setPixmap(QPixmap("D:\\QTcreate\\Day8\\04homework\\icon\\logo"));
lab1->setScaledContents(true);
QLabel *lab2 = new QLabel(this);
lab2->resize(40,40);
lab2->move(100,140);
// lab2->setAlignment(Qt::AlignCenter);
lab2->setPixmap(QPixmap("D:\\QTcreate\\Day8\\04homework\\icon\\userName"));
lab2->setScaledContents(true);
QLabel *lab3 = new QLabel(this);
lab3->resize(40,40);
lab3->move(100,190);
// lab3->setAlignment(Qt::AlignCenter);
lab3->setPixmap(QPixmap("D:\\QTcreate\\Day8\\04homework\\icon\\passwd"));
lab3->setScaledContents(true);
//创建两个行编译文本
QLineEdit *edit1 = new QLineEdit(this);
edit1->resize(200,30);
edit1->move(lab2->x()+50,lab2->y());
edit1->setEchoMode(QLineEdit::Password);
edit1->setPlaceholderText("账号/手机/邮箱");
QLineEdit *edit2 = new QLineEdit(this);
edit2->resize(200,30);
edit2->move(lab3->x()+50,lab3->y());
edit2->setEchoMode(QLineEdit::Password);
edit2->setPlaceholderText("密码");
//创建两个按钮
QPushButton *btn1 = new QPushButton("取消",this);
btn1->resize(100,40);
btn1->move(100,240);
btn1->setIcon(QIcon("D:\\QTcreate\\Day8\\04homework\\icon\\cancel"));
btn1->setStyleSheet("background-color:skyblue; border-radius:10px;");
QPushButton *btn2 = new QPushButton("登陆",this);
btn2->resize(btn1->size());
btn2->move(btn1->x()+200, btn1->y());
btn2->setIcon(QIcon("D:\\QTcreate\\Day8\\04homework\\icon\\login"));
btn2->setStyleSheet("background-color:skyblue; border-radius:10px;");
}
MyWidget::~MyWidget()
{
}
测试文件
#include "mywidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();
return a.exec();
}
代码结果: