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>思维导图