当前位置: 首页> 健康> 美食 > 手写登录页面,unique_ptr智能指针

手写登录页面,unique_ptr智能指针

时间:2025/9/11 23:55:57来源:https://blog.csdn.net/gjvrnv/article/details/141966759 浏览次数:0次

手写登录页面

#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include<QIcon>
#include<QPushButton> //按钮类
#include<QLineEdit> //行编辑器类
#include<QLabel> //标签类
#include<QMovie>
#include<QLayout>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//更改当前页面的尺寸this->resize(450,300);//设置成固定尺寸this->setFixedSize(450,300);//有关组件的名称this->setWindowTitle("QQ");//设置窗体图标this->setWindowIcon(QIcon("E:\\24071QT\\day1\\LoginPage\\icon_ceefw9tu8ew\\qq_1"));//调用无参构造,构造一个按钮QPushButton *btn1=new QPushButton;//将自定义页面的页面当作按钮类的父组件btn1->setParent(this);//设置按钮文本内容btn1->setText("自动登录");//设置按钮大小btn1->resize(80,40);btn1->move(60,180);//设置按钮图标//btn1->setIcon(QIcon("E:\\24071QT\\day1\\LoginPage\\icon_vvytcnccyw\\denglu"));//4.构造按钮时,指定父组件并设置文本内容,并设置按钮图标QPushButton *btn2 =new QPushButton(QIcon("E:\\24071QT\\day1\\anquandenglu"),"安全登录",this);btn2->resize(280,30);//设置样式表btn2->setStyleSheet("background-color:skyblue;border-radius:10;");btn2->move(50,240);QPushButton *btn3 =new QPushButton("记住密码",this);btn3->resize(btn1->size());  //重置大小btn3->move(btn1->x()+btn1->width()+15,btn1->y());QPushButton *btn4 =new QPushButton("找回密码",this);btn4->resize(btn1->size());  //重置大小btn4->move(btn3->x()+btn3->width()+15,btn3->y());/**********行编辑器*********/QLineEdit *edit1 =new QLineEdit(this);edit1->move(50,90);edit1->resize(300,30);  //重新设置大小QLineEdit *edit2 =new QLineEdit("密码",this);edit2->resize(300,30); //重新设置大小edit2->move(edit1->x(),edit1->y()+edit1->height()+10);//移动位置edit2->clear(); //清空内容edit2->setPlaceholderText("密码");  //设置占位文本edit2->setEchoMode(QLineEdit::Password);//设置回显模式/**********标签类*******///调用有参构造,指定父组件,构造一个labQLabel *lab7=new QLabel(this);lab7->resize(500,80);QMovie *movie =new QMovie("E:\\24071QT\\day1\\01\\pictrue\\zz.gif");//将动图对象放入到标签中lab7->setMovie(movie);movie->start();//让标签内容自适应大小lab7->setScaledContents(true);//给标签弄成静态图QLabel *lab1 = new QLabel;lab1->setParent(this);          //设置父组件lab1->setText("账号:");          //设置文本内容lab1->move(btn1->x()-40, btn1->y()-90);    //设置坐标//2、使用有参构造完成构造一个标签QLabel *lab2 = new QLabel("密码:", this);lab2->move(lab1->x()+1, lab1->y()+40);lab1->resize(25,25);lab1->setPixmap(QPixmap("E:\\24071QT\\day1\\LoginPage\\icon_ceefw9tu8ew\\qq"));lab1->setScaledContents(true);lab2->resize(25,25);lab2->setPixmap(QPixmap("E:\\24071QT\\day1\\LoginPage\\icon_ceefw9tu8ew\\suoding"));lab2->setScaledContents(true);connect(btn2,&QPushButton::clicked,[=](){if(edit1->text()==edit2->text()){qDebug()<<"登录成功";}else{qDebug()<<"登录失败";}});
}Widget::~Widget()
{delete ui;
}

unique_ptr智能指针

#include <iostream>using namespace std;
template <typename T>
class myunique_ptr
{
public:explicit myunique_ptr(T*p)noexcept //不可用于转换函数{ptr=p;}~myunique_ptr() noexcept{delete ptr;}T&operator*()const  //重载*操作符{return *ptr;}T*operator->()const noexcept //重载->操作符{return  ptr;}myunique_ptr(const myunique_ptr&)=delete ;//禁止拷贝构造函数myunique_ptr&operator=(const myunique_ptr&)=delete ;// 禁止赋值函数myunique_ptr(myunique_ptr && other)noexcept //   拷贝构造函数右值引用{ptr=other.ptr;other.ptr=nullptr;}myunique_ptr&operator=(myunique_ptr&&other)noexcept//拷贝赋值函数{if(this!=&other)  //防止自己给自己赋值{delete this->ptr;  //释放自己之前的空间ptr=other.ptr;other.ptr=nullptr;}return *this;}
private:T* ptr;   //内置指针};
class Test
{
public:string name;          //名字Test() {cout<<name<<"::无参构造"<<endl;}            //无参构造Test(string n):name(n) {cout<<name<<"::有参构造"<<endl;}  //有参构造~Test() {cout<<name<<"::析构函数"<<endl;}              //析构函数
};int main()
{Test *p1 = new Test("张三");          //在堆区申请空间myunique_ptr<Test> up1(p1);            //使用原始指针实例化一个指针指针cout<<"name = "<<(*p1).name<<endl;        //指针找到对象使用cout<<"name = "<<p1->name<<endl;          //原始指针直接使用cout<<"name = "<<(*up1).name<<endl;       //智能指针访问cout<<"name = "<<up1->name<<endl;          //智能指针访问cout << "********程序到此结束!********" << endl;return 0;
}

思维导图

关键字:手写登录页面,unique_ptr智能指针

版权声明:

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

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

责任编辑: