QT核心内容(9.6)

1> 手写unique_ptr智能指针

代码:

cpp 复制代码
#include <iostream>
#include <cassert>

using namespace std;
template<typename T>
class my_unique_ptr {
private:
    T* ptr;

    // 禁止拷贝构造函数和拷贝赋值操作符
    my_unique_ptr(const my_unique_ptr&) = delete;
    my_unique_ptr& operator=(const my_unique_ptr&) = delete;

public:
    // 默认构造函数
    my_unique_ptr() : ptr(nullptr) {}

    // 构造函数,接受一个指针
    explicit my_unique_ptr(T* p) : ptr(p) {}

    // 移动构造函数
    my_unique_ptr(my_unique_ptr&& other) noexcept : ptr(other.ptr) {
        other.ptr = nullptr;
    }

    // 移动赋值操作符
    my_unique_ptr& operator=(my_unique_ptr&& other) noexcept {
        if (this != &other) {
            delete ptr;
            ptr = other.ptr;
            other.ptr = nullptr;
        }
        return *this;
    }

    // 析构函数
    ~my_unique_ptr() {
        delete ptr;
        cout<<"my_unique_ptr 析构"<<endl;
    }

    // 解引用操作符
    T& operator*() const {
        assert(ptr != nullptr);
        return *ptr;
    }

    // 箭头操作符
    T* operator->() const {
        assert(ptr != nullptr);
        return ptr;
    }

    // 释放所有权
    T* release() noexcept {
        T* temp = ptr;
        ptr = nullptr;
        return temp;
    }

    // 重置指针
    void reset(T* p = nullptr) {
        if (ptr != p) {
            delete ptr;
            ptr = p;
        }
    }

    // 检查是否为空
    bool operator==(nullptr_t) const noexcept {
        return ptr == nullptr;
    }

    // 检查是否非空
    bool operator!=(nullptr_t) const noexcept {
        return ptr != nullptr;
    }
};

// 使用示例
class Test {
public:
    Test() { cout << "Test 构造"<<endl; }
    ~Test() { cout << "Test 析构"<<endl; }
    void sayHello() { cout << "Hello"<<endl; }
};

int main() {
    my_unique_ptr<Test> ptr(new Test());
    ptr->sayHello();
    // ptr.reset(); // 取消注释以提前释放资源
    return 0; // Test 对象在这里被自动销毁
}

运行结果:

2> 手写登录界面,不允许拖拽,要求尽可能的美观

代码:

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>      //QT中信息调试类,用于输出数据,无需使用该类实例化对象,直接使用成员函数即可
#include <QIcon>
#include <QPushButton>
#include <QLabel>
#include <QMovie>
#include <QLineEdit>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);


    this->resize(400,300);          //更改当前界面的尺寸
    this->setMaximumSize(1000,900); //设置最大尺寸
    this->setMinimumSize(200,100);  //设置最小尺寸
    this->setFixedSize(500,300);    //设置固定尺寸


    //设置窗体图标
    this->setWindowIcon(QIcon("D:\\24061C++\\QT_day2\\1.png"));




    /**********************有关按钮的操作***********************/


    //构造按钮时,直接指定父组件
    QPushButton *btn1 = new QPushButton(this);
    btn1->setText("注册");
    btn1->move(0,260);                    //移动按钮位置
    btn1->resize(80,40);                    //设置按钮尺寸
    btn1->setIcon(QIcon("D:\\24061C++\\QT_day2\\pictrue\\zhuce.png"));                   //设置按钮图标

    //构造按钮时,指定父组件并且设置文本内容
    QPushButton *btn2 = new QPushButton("确定",this);
    btn2->resize(btn1->size());
    btn2->move(150,200);
    btn2->setIcon(QIcon("D:\\24061C++\\QT_day2\\pictrue\\queding.png"));

    //构造函数时,指定父组件并设置文本内容,并设置按钮图标
    QPushButton *btn3 = new QPushButton(QIcon("D:\\24061C++\\QT_day2\\pictrue\\quxiao.png"),"取消",this);
    btn3->resize(btn1->size());
    btn3->move(btn2->x()+btn2->width()+50,btn2->y());




    /************************标签文本*****************************/
    //1、使用无参构造完成构造一个标签
    QLabel *lab1 = new QLabel;
    lab1->setParent(this);          //设置父组件
    lab1->setText("账号:");         //设置文本内容
    lab1->move(100,110);     //设置坐标

    //2、使用有参构造完成构造一个标签
    QLabel *lab2 = new QLabel("密码:",this);
    lab2->move(lab1->x(),lab1->y()+40);

    //3、调用有无参构造,指定父组件,构造一个lab
    QLabel *lab3 = new QLabel(this);
    lab3->resize(500,100);
    lab3->setStyleSheet("background-color:pink;");

    //给标签设置动图
    //创建一个mocie对象
    QMovie *movie = new QMovie("D:\\24061C++\\QT_day2\\pictrue\\zz.gif");
    //将动图对象放入标签中
    lab3->setMovie(movie);
    //让动图动起来
    movie->start();

    //让标签内容自适应大小
    lab3->setScaledContents(true);

    //给标签设置静态图
    lab1->resize(30,30);
    lab1->setPixmap(QPixmap("D:\\24061C++\\QT_day2\\pictrue\\userName.jpg"));
    lab1->setScaledContents(true);
    lab2->resize(30,30);
    lab2->setPixmap(QPixmap("D:\\24061C++\\QT_day2\\pictrue\\passwd.jpg"));
    lab2->setScaledContents(true);





    /******************行编辑器类(QLIneEdit)***********************/
    //1、使用无参构造,构造一个行编辑器
    QLineEdit *edit1 = new QLineEdit;
    edit1->setParent(this);             //指定一个父组件
    edit1->resize(300,30);               //重新设置大小
    edit1->move(lab1->x()+lab1->width()+2,lab1->y());           //移动位置
    edit1->setPlaceholderText("账号");            //设置占位文本

    //2、使用有参构造完成构造一个行编辑器
    QLineEdit *edit2 = new QLineEdit("密码",this);
    edit2->resize(300,30);              //重新设置大小
    edit2->move(lab2->x()+lab2->width()+2,lab2->y());            //移动位置
    edit2->clear();             //清空内容
    edit2->setPlaceholderText("密码");            //设置占位文本
    edit2->setEchoMode(QLineEdit::Password);     //设置回显模式



}

Widget::~Widget()
{
    delete ui;
}

运行结果:

3> 思维导图

相关推荐
末央&7 分钟前
【C++】特化妙技与分文件编写 “雷区”
开发语言·c++·算法
一个天蝎座 白勺 程序猿17 分钟前
Python(19)Python并发编程:深入解析多线程与多进程的差异及锁机制实战
开发语言·python
GSDjisidi1 小时前
日本IT|车载C#开发工程师的前途及职业发展
开发语言·c#
卓豪终端管理1 小时前
如何安全地管理固定功能设备?
java·大数据·开发语言·网络·人工智能·安全
进阶的小木桩1 小时前
VSTO幻灯片退出播放(C#模拟键盘鼠标的事件)
开发语言·c#·计算机外设
代码程序猿RIP1 小时前
C++(22)—内存管理
开发语言·数据结构·c++·算法
灏瀚星空2 小时前
AI 模型高效化:推理加速与训练优化的技术原理与理论解析
开发语言·人工智能·深度学习·程序人生·机器人·智慧城市·量子计算
孞㐑¥2 小时前
C++之哈希
开发语言·c++·经验分享·笔记
勇敢牛牛_2 小时前
【Rust基础】crossbeam带来的阻塞问题
开发语言·笔记·rust
东雁西飞2 小时前
MATLAB 控制系统设计与仿真 - 34
开发语言·单片机·算法·matlab·工业机器人