当前位置: 首页 > news >正文

QT作业1

1> 手写unique_ptr指针指针

代码:

#include <iostream>using namespace std;// 自定义的unique_ptr类模板
template <typename T>
class unique_ptr
{
public:// 构造函数,接收一个指针explicit unique_ptr(T* ptr = nullptr) noexcept : ptr_(ptr) {}// 析构函数,释放指针所指向的内存~unique_ptr(){delete ptr_;}// 禁用拷贝构造函数和赋值函数unique_ptr(const unique_ptr&) = delete;unique_ptr& operator=(const unique_ptr&) = delete;// 右值引用构造函数,接管另一个unique_ptr的所有权unique_ptr(unique_ptr&& other) noexcept : ptr_(other.ptr_){other.ptr_ = nullptr;}// 右值引用赋值函数,接管另一个unique_ptr的所有权unique_ptr& operator=(unique_ptr&& other) noexcept{if (this != &other){delete ptr_;ptr_ = other.ptr_;other.ptr_ = nullptr;}return *this;}// 重载*操作符,返回所指向对象的引用T& operator*() const{return *ptr_;}// 重载->操作符,返回所指向对象的指针T* operator->() const{return ptr_;}// 获取所指向对象的指针T* get() const noexcept{return ptr_;}// 释放所指向的对象,并将指针置为nullptrvoid reset(T* new_ptr = nullptr) noexcept{delete ptr_;ptr_ = new_ptr;}// 检查指针是否为空explicit operator bool() const noexcept{return ptr_ != nullptr;}private:T* ptr_; // 内置的指针,指向所管理的对象
};// 测试用例
struct MyClass
{MyClass(int value) : value_(value){cout << "用值调用MyClass构造函数: " << value << endl;}~MyClass(){cout << "用值调用MyClass析构函数: " << value_ << endl;}void printValue(){cout << "值: " << value_ << endl;}int value_;
};int main()
{// 使用unique_ptr管理MyClass对象的生命周期unique_ptr<MyClass> ptr(new MyClass(1));ptr->printValue(); // 使用->操作符访问成员函数(*ptr).printValue(); // 使用*操作符解引用并访问成员函数return 0;
}

运行结果:

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

代码:

MyWnd.h

#ifndef MYWND_H
#define MYWND_H    //防止文件重复包含#include <QWidget>        //QWidget类所在的头文件,父类头文件
#include<iostream>
#include<QDebug>
#include<QIcon>
#include<QPushButton>
#include<QLabel>
#include<QMovie>
#include<QLineEdit>QT_BEGIN_NAMESPACE
namespace Ui { class MyWnd; }          //命名空间的声明
QT_END_NAMESPACE//定义属于自己的类  MyWnd是类名,公共继承自QWidget
class MyWnd : public QWidget
{Q_OBJECT        //信号与槽的元对象public:MyWnd(QWidget *parent = nullptr);         //构造函数的声明,有一个默认参数的形参~MyWnd();                           //析构函数额声明public slots:void my_login();           //自定义槽函数的声明private:Ui::MyWnd *ui;             //后期可以通过ui指针找到ui界面上拖拽出来的组件QPushButton *btn1;QPushButton *btn3;QLineEdit *edit1;QLineEdit *edit2;QLabel *lab4;QLabel *lab5;
};
#endif // MYWND_H

MyWnd.cpp

#include "mywnd.h"              //自己的头文件
#include "ui_mywnd.h"          //ui界面对应的头文件MyWnd::MyWnd(QWidget *parent)            //构造函数的定义: QWidget(parent)                  //显性调用父类的构造函数完成对子类从父类继承下来成员的初始化工作, ui(new Ui::MyWnd)                //对自己类中的指针成员开辟空间
{ui->setupUi(this);         //给拖拽出来的组件实例化空间//1.有关当前界面的大小操作qDebug("hello %d",520);qDebug() << "hello world" << 520;//2.有关组件尺寸大小的相关内容qDebug()<<"this->size = "<<this->size();qDebug()<<"width = "<<this->width()<<"    heigh"<<this->height();qDebug()<<"width = "<<this->rect().width()<<"    heigh"<<this->rect().height();this->resize(400,300);      //更改当前界面尺寸this->resize(QSize(1000,800));  //使用类对象更改尺寸this->setMaximumSize(1000,900);     //设置最大尺寸this->setMinimumSize(200,100);      //设置最小尺寸this->setFixedSize(500,400);        //设置固定尺寸this->setWindowIcon(QIcon("C:\\Users\\xyh\\Desktop\\1.png"));//1.使用无参构造,构造按钮this->btn1 = new QPushButton;btn1->setParent(this);btn1->setText("登录");qDebug()<<"btn1.size = "<<btn1->size();btn1->resize(80,40);btn1->move(100,200);
//    btn1->setStyleSheet("background-color:skyblue; border-radius:10;");btn1->setIcon(QIcon("C:\\Users\\xyh\\Desktop\\1.png"));connect(this->btn1, &QPushButton::clicked, this, &MyWnd::my_login);//2.构造按钮时指定父组件
//    QPushButton *btn2 = new QPushButton(this);
//    btn2->setText("按钮2");
//    btn2->move(btn1->x()+btn1->width()+2,btn1->y());
//    btn2->resize(btn1->size());
//    btn2->setEnabled(false);//3.构造按钮时指定父组件并且设置文本this->btn3 = new QPushButton("取消",this);btn3->resize(btn1->size());btn3->move(btn1->x()+btn1->width()+2,btn1->y());connect(this->btn3, SIGNAL(clicked()), this, SLOT(close()));//4.构造按钮时指定父组件并且设置文本,并且设置按钮图标
//    QPushButton *btn4 = new QPushButton(QIcon("C:\\Users\\xyh\\Desktop\\1.png"),"注册",this);
//    btn4->resize(btn1->size());
//    btn4->move(btn3->x()+btn3->width()+2,btn3->y());//1.使用无参构造完成一个标签QLabel *lab1 =new QLabel;lab1->setParent(this);lab1->setText("账号:");lab1->move(btn1->x(),btn1->y()-90);//有参构造QLabel *lab2 =new QLabel("密码:",this);lab2->move(lab1->x(),lab1->y()+40);//有参构造QLabel *lab3 =new QLabel(this);lab3->resize(500,100);lab3->setStyleSheet("background-color:pink;");//有参构造this->lab4 =new QLabel("登录成功",this);lab4->resize(300,30);lab4->move(btn3->x(),btn3->y()+100);this->lab4->hide();//有参构造this->lab5 =new QLabel("登录失败,请重新登录",this);lab5->resize(300,30);lab5->move(btn3->x(),btn3->y()+100);this->lab5->hide();//给标签设置GIFQMovie *movie = new QMovie("C:\\Users\\xyh\\Desktop\\1.gif");lab3->setMovie(movie);movie->start();lab3->setScaledContents(true);//给标签设置静态图lab1->resize(30,30);lab1->setPixmap(QPixmap("C:\\Users\\xyh\\Desktop\\1.png"));lab1->setScaledContents(true);lab2->resize(30,30);lab2->setPixmap(QPixmap("C:\\Users\\xyh\\Desktop\\1.png"));lab2->setScaledContents(true);//无参构造行编辑器this->edit1 = new QLineEdit;edit1->setParent(this);edit1->resize(300,30);edit1->move(lab1->x()+lab1->width()+2,lab1->y());edit1->clear();edit1->setPlaceholderText("账号");//有参构造行编辑器this->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);}MyWnd::~MyWnd()            //析构函数的定义
{delete ui;             //释放ui指针的内存
}void MyWnd::my_login()
{if(this->edit1->text() == this->edit2->text()){this->lab5->hide();this->lab4->show();
//        this->close();}else{this->lab4->hide();this->lab5->show();this->edit2->clear();}
}

运行结果:

3>思维导图

http://www.lryc.cn/news/432929.html

相关文章:

  • Mybatis-设计模式总结
  • 【笔记】Java | 三目运算符和Math函数的比较
  • 信创实践(2):利用Leapp工具迁移CentOS至AnolisOS,实现系统升级与自主可控
  • 数据库死锁查询SQL
  • vLLM (4) - LLMEngine上篇
  • Java重修笔记 第五十天 HashSet 和 TreeSet 的去重机制比较
  • 提前购|基于SSM+vue的创新型产品提前购平台(源码+数据库+文档)
  • 上海市计算机学会竞赛平台2024年7月月赛丙组求和问题
  • 【LVI-SAM】激光雷达点云处理特征提取LIO-SAM 之FeatureExtraction实现细节
  • [数据集][目标检测]血细胞检测数据集VOC+YOLO格式2757张4类别
  • opencart journal 3 在价格前添加文本prefix
  • c++ string类的模拟实现的注意事项
  • Unity3D中控制3D场景中游戏对象显示层级的详解
  • 代码执行漏洞-Log4j2漏洞 vulhub CVE-2021-44228
  • uniapp / uniapp x UI 组件库推荐大全
  • 花8000元去培训机构学习网络安全值得吗,学成后就业前景如何?
  • PhpStorm 下调试功能配置
  • MVC(Model-View-Controller)和MVVM(Model-View-ViewModel)
  • 【H2O2|全栈】关于HTML(4)HTML基础(三)
  • 关于找不到插件 ‘org.springframework.boot:spring-boot-maven-plugin:‘的解决方案
  • 深入RabbitMQ世界:探索3种队列、4种交换机、7大工作模式及常见概念
  • 将目标检测模型导出到C++|RT-DETR、YOLO-NAS、YOLOv10、YOLOv9、YOLOv8
  • 【Windows】解决新版 Edge 浏览器开机自启问题(简单有效)
  • 如何给3D人物换衣服CC4
  • 如何对列表、字符串进行分组
  • 【GEE代码实例教程详解:NDVI时间序列趋势分析】
  • 51单片机-DS1302(RTC实时时钟芯片)
  • FreeRTOS学习笔记—②RTOS的认识及任务管理篇
  • 【C++从练气到飞升】22---C++中的异常
  • 前端:HTML、CSS、JS、Vue