QT day01

news/2024/9/29 4:51:57 标签: qt, 开发语言

自定义实现登录界面:

#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界面组件的空间
}


http://www.niftyadmin.cn/n/5682406.html

相关文章

mysql学习教程,从入门到精通,SQL LIKE 运算符(28)

1、SQL LIKE 运算符 在SQL中&#xff0c;LIKE运算符主要用于在WHERE子句中搜索列中的指定模式。它通常与通配符一起使用&#xff0c;如%&#xff08;代表零个、一个或多个字符&#xff09;和_&#xff08;代表单个字符&#xff09;&#xff0c;以执行模糊匹配。下面是一个使用…

DeepSpeed笔记--利用Accelerate实现DeepSpeed加速

1--参考文档 Accelerate官方文档 acceleratedeepspeed多机多卡训练-适用集群环境 DeepSpeed & Accelerate 2--安装过程 # 安装accelerate pip install accelerate pip install importlib-metadata # 获取默认配置文件 python -c "from accelerate.utils import wr…

linux脚本工具

目录 shell工具查看Nvidia GPU状态查看某个监听端口是否存在设置局部代理查找关键字相关进程根据日常所需&#xff0c;持续更新 shell工具 减少重复性工作&#xff0c;简化工作流程&#xff0c;提高工作效率 将所编写的shell脚本赋予可执行权限 chmod x <脚本文件> 在…

Python(七)- 文件操作

目录 文件操作 打开文件 读数据 写数据 关闭文件 文件读写实例 文件写 文件读 读数据类型 备份文件 os模块 目录的具体操作 文件操作 在Python中操作文件记录信息的步骤&#xff1a; &#xff08;1&#xff09;打开文件&#xff0c;或新建一个文件&#xff1b; o…

Java: 数据类型与变量和运算符

目录 一 .字面常量 二.数据类型 三.变量 1.语法格式 2.整型变量 (1).整型变量 (2). 长整型变量 (3).短整型变量 (4).字节型变量 3.浮点型变量 (1).双精度浮点型 (2).单精度浮点型 4.字符型变量 5.布尔型变量 四.类型转换 1.自动类型转换(隐式) 2.强制类型转换(…

docker笔记_数据卷、挂载

docker数据存储 概述数据卷&#xff08;Volumes&#xff09;特点操作 绑定挂载&#xff08;Bind Mounts&#xff09;内存挂载&#xff08;tmpfs&#xff09;总结 概述 docker官方文档 镜像构建过程中&#xff0c;所产生的layer都是只读层&#xff0c;只有在创建容器时才会生成…

Vue 3 文件编译流程详解与 Babel 的使用

文章目录 一、背景二、结论三、vitejs/plugin-vue 插件调试前物料准备vuePlugin 入口buildStart 方法transform 方法 四、vue/compiler-sfc 核心包parse 方法compileScript、rewriteDefault 方法compileTemplate 方法 五、整体架构六、总结参考资料 一、背景 最近正在研究 rea…

STM32F745IE 能进定时器中断,无法进主循环

当你遇到STM32F745IE这类问题,即能够进入定时器中断但无法进入主循环(main() 函数中的循环),可能的原因和解决方法包括以下几个方面: 检查中断优先级和嵌套: 确保没有其他更高优先级的中断持续运行并阻止了主循环的执行。使用调试工具查看中断的进入和退出情况。检查中断…