QT day01
自定义实现登录界面:
#include "widget.h"
#include "ui_widget.h"
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>
Widget::Widget(QWidget *parent) //定义有参构造函数
: QWidget(parent)
, ui(new Ui::Widget) //给自己类中的指针实例化空间
{
//ui->setupUi(this); //将ui界面上拖拽的组件展示到this界面上
//按钮组件
this->setWindowTitle("QQ登录");
this->resize(520,520);
this->setStyleSheet("background-color:lightblue");
QPushButton *btn1 = new QPushButton;
btn1->setParent(this);
btn1->setText("登录");
btn1->resize(60,40);
btn1->move(150,300);
btn1->setStyleSheet("background-color:lightgreen");
QPushButton *btn2 = new QPushButton(this);
btn2->setText("注册");
btn2->resize(btn1->size());
btn2->move(300,300);
btn2->setStyleSheet("background-color:lightgreen");
//行编辑器
QLineEdit *edit1 = new QLineEdit;
edit1->setParent(this);
edit1->resize(250,30);
edit1->move(150,150);
edit1->setPlaceholderText("账号");
edit1->setEchoMode(QLineEdit::Password);
QLineEdit *edit2 = new QLineEdit(this);
edit2->resize(edit1->size());
edit2->move(150,200);
edit2->setPlaceholderText("密码");
edit2->setEchoMode(QLineEdit::Password);
//创建标签
QLabel *lab1 = new QLabel;
lab1->setParent(this);
lab1->setText("账号:");
lab1->move(edit1->x()-60,edit1->y());
QLabel *lab2 = new QLabel("密码:",this);
lab2->move(edit2->x()-60,edit2->y());
}
Widget::~Widget() //定义析构函数
{
delete ui; //释放ui界面组件的空间
}
效果图:
02Demo.pro
#QT:引入的类库 core:核心库 gui:图形化界面库
QT += core gui
#当QT超过版本4时会自动加上widgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#支持C++11新特性
CONFIG += 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
widgets.h
#ifndef WIDGET_H
#define WIDGET_H //防止头文件重复包含
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; } //将其他文件中的命名空间进行声明
QT_END_NAMESPACE
//自定义的类的声明 公共继承自QWidget:QWidget
class Widget : public QWidget
{
Q_OBJECT //信号与槽的源对象
public:
Widget(QWidget *parent = nullptr); //构造函数的声明且带有默认参数
~Widget(); //虚析构函数的声明
private:
Ui::Widget *ui; //成员属性:指针
};
#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"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) //定义有参构造函数
: QWidget(parent)
, ui(new Ui::Widget) //给自己类中的指针实例化空间
{
ui->setupUi(this); //将ui界面上拖拽的组件展示到this界面上
}
Widget::~Widget() //定义析构函数
{
delete ui; //释放ui界面组件的空间
}
原文地址:https://blog.csdn.net/jay__jay_/article/details/142620270
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!