当前位置: 首页> 科技> IT业 > 封装智能指针 qt实现登录界面

封装智能指针 qt实现登录界面

时间:2025/9/16 17:57:00来源:https://blog.csdn.net/2301_76740986/article/details/142033091 浏览次数:0次

1.封装独占智能指针——unique_ptr

#include <iostream>
#include <utility> // For std::move// 命名空间
namespace custom_memory
{
template <typename T>
class myPtr
{
public:// 使用初始化列表进行初始化explicit myPtr(T* p = nullptr) noexcept : m_ptr(p) {}// 析构函数~myPtr() noexcept {delete m_ptr;}// 重载解引用操作符T& operator*() const {return *m_ptr;}// 重载成员访问操作符T* operator->() const noexcept {return m_ptr;}// 禁止拷贝构造myPtr(const myPtr&) = delete;// 禁用拷贝赋值操作myPtr& operator=(const myPtr&) = delete;// 移动构造函数myPtr(myPtr&& other) noexcept : m_ptr(other.m_ptr){other.m_ptr = nullptr;}// 移动赋值操作符myPtr& operator=(myPtr&& other) noexcept{if (this != &other) {delete m_ptr;m_ptr = other.m_ptr;other.m_ptr = nullptr;}return *this;}private:T* m_ptr; // 成员变量命名
};} // namespace custom_memory//类命名
class Person {
public:std::string name;Person() : name("Unknown") { std::cout << name << "  构造函数" << std::endl; }explicit Person(std::string n) : name(std::move(n)){std::cout << name << "  构造函数" << std::endl;}~Person() { std::cout << name << "  析构函数" << std::endl; }
};//*************************主程序****************************
int main()
{custom_memory::myPtr<Person> ptr1(new Person("hello"));std::cout << "ptr1 name = " << ptr1->name << std::endl;Person* rawPtr = new Person("nihao");custom_memory::myPtr<Person> ptr2(rawPtr);std::cout << "ptr2 name = " << ptr2->name << std::endl;ptr2 = std::move(ptr1);std::cout << "ptr2 name = " << ptr2->name << std::endl;return 0;
}

2.qt实现登录界面(不能推拽)

#include "widget.h"
#include "ui_widget.h"
#include<QIcon>
#include<QLabel>
#include<QLineEdit>   //行编辑器
#include<QPixmap>      //图标类
#include<QMovie>         //动图类
#include<QPushButton>   //按钮
#include<QDebug>
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//1>尺寸设置//设置登录页面尺寸this->resize(400,300);//固定页面尺寸this->setFixedSize(500,400);//2>组件标识//组件名称设置this->setWindowTitle("QQ");//窗体图标this->setWindowIcon(QIcon("C:\\Users\\你好.幸会\\Desktop\\1.png"));//*********************** 按钮***************************QPushButton *btn1 = new QPushButton;     //调用无参构造,构造一个按钮btn1->setParent(this);        //将自定义的界面当作按钮类的父组件btn1->setText("安全登录");           //设置按钮文本内容btn1->resize(90,40);              //设置按钮大小btn1->move(170,270);              //移动按钮位置btn1->setStyleSheet("background-color:skyblue;");    //设置样式表QPushButton *btn2 = new QPushButton;     //调用无参构造,构造一个按钮btn2->setParent(this);        //将自定义的界面当作按钮类的父组件btn2->setText("取消登录");           //设置按钮文本内容btn2->resize(90,40);              //设置按钮大小btn2->move(btn1->x()+110,btn1->y());              //移动按钮位置btn2->setStyleSheet("background-color:skyblue;");    //设置样式表//****************************行编辑器设置************************//设置行编辑器1QLineEdit *edit1 = new QLineEdit;edit1->setParent(this);          //指定父组件edit1->resize(200, 30);          //重新设置大小edit1->move(170,200);//设置行编辑器2QLineEdit *edit2 = new QLineEdit;edit2->setParent(this);          //指定父组件edit2->resize(200, 30);          //重新设置大小edit2->move(edit1->x(),edit1->y()+35);  //设置位置edit2->setEchoMode(QLineEdit::Password);   //不显示输入//***********************图标编辑器设置*************************//设置qq图标QLabel *lab1 = new QLabel;lab1->setParent(this);      //设置父组件lab1->setText("账号:");     //设置文本内容lab1->move(110,205);       //设置坐标位置//lab1->resize(30,30);// lab1->setPixmap(QPixmap("C:\\Users\\你好.幸会\\Desktop\\11.jpg"));//  lab1->setScaledContents(true);QLabel *lab2 = new QLabel;lab2->setParent(this);      //设置父组件lab2->setText("密码:");     //设置文本内容lab2->move(110,240);       //设置坐标位置//  lab2->resize(30,30);//   lab2->setPixmap(QPixmap("C:\\Users\\你好.幸会\\Desktop\\2.jpg"));//  lab2->setScaledContents(true);//调用有有参构造,指定父组件,构造一个labQLabel *lab3 = new QLabel(this);lab3->resize(500,180);//动图设置//创建一个movie对象QMovie *movie=new QMovie("C:\\Users\\你好.幸会\\Desktop\\2.gif");//将动图对象放入到标签中lab3->setMovie(movie);//让动图动起来movie->start();//让标签内容自适应大小lab3->setScaledContents(true);//账号与密码connect(btn1,&QPushButton::clicked,[=](){if(edit1->text()==edit2->text())qDebug()<<"登录成功!";elseqDebug()<<"账号或密码错误,登录失败!";});//取消登录btn2connect(btn2,&QPushButton::clicked,[=](){this->close();});}Widget::~Widget()
{delete ui;
}

思维导图

关键字:封装智能指针 qt实现登录界面

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: