一、作业:
1、创捷一个类似于qq登录的界面
1)源代码
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//QPushbutton:登录、退出this->join = new QPushButton("登录",this);join->resize(100,30);join->move(250,350);this->exit = new QPushButton("退出",this);exit->resize(this->join->size());exit->move(this->join->x()+this->join->width()+30,this->join->y());this->clo = new QPushButton("关闭",this);clo->resize(this->exit->size());clo->move(this->exit->x()+this->exit->width()+30,this->exit->y());//QLabel:账号,密码标签this->zhanghu = new QLabel(this);zhanghu->resize(30,30);zhanghu->move(this->join->x(),this->join->y()-100);zhanghu->setPixmap(QPixmap("C:\\Users\\22856\\Desktop\\pictrue\\userName.jpg"));zhanghu->setScaledContents(true);this->password = new QLabel(this);password->resize(30,30);password->move(this->join->x(),this->join->y()-50);password->setPixmap(QPixmap("C:\\Users\\22856\\Desktop\\pictrue\\passwd.jpg"));password->setScaledContents(true);//QLineEdit:账号文本,密码文本this->text_zhanghu = new QLineEdit(this);text_zhanghu->resize(250,30);text_zhanghu->move(this->zhanghu->x()+this->zhanghu->width()+5,this->zhanghu->y());this->text_password = new QLineEdit(this);text_password->resize(250,30);text_password->move(this->password->x()+this->password->width()+5,this->password->y());text_password->setPlaceholderText("密码"); //设置占位文本text_password->setEchoMode(QLineEdit::Password); //设置回显模式//把登录连接到槽完成登录过程connect(this->join, &QPushButton::clicked, this, &Widget::my_slot1);//把退出连接到槽完成退出登录过程connect(this->exit, &QPushButton::clicked, this, &Widget::my_slot2);//把关闭连接到槽完成关闭界面过程connect(this->clo, &QPushButton::clicked, this, [=](){this->close();});}Widget::~Widget()
{delete ui;
}
void Widget::my_slot1()
{if(this->text_zhanghu->text()==this->text_password->text()){qDebug() <<"登录成功";//打开一个新界面}else{qDebug() <<"登录失败,请重新输入";this->text_password->clear();}
}
void Widget::my_slot2()
{this->text_password->clear();this->text_zhanghu->clear();qDebug() <<"退出登录";
}
2)头文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QDebug>
#include <QIcon>
#include <QMovie>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECT
//提前声明
public slots:void my_slot1();void my_slot2();public:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;QPushButton *join;QPushButton *exit;QPushButton *clo;QLabel *zhanghu;QLabel *password;QLineEdit *text_zhanghu;QLineEdit *text_password;
};
#endif // WIDGET_H
3)运行结果
2、创建一个自己的独占智能指针类
代码:
#include <iostream>using namespace std;template <class T>
class my_unique_ptr
{
private:T *ptr;
public:explicit my_unique_ptr(T* p)noexcept{// p = nullptr;this->ptr = p;}//构造函数~my_unique_ptr() noexcept{delete ptr;} //析构函数T& operator*() const{return *ptr;} //重载*操作符T* operator->() const noexcept{;return ptr;}//重载->操作符my_unique_ptr(const my_unique_ptr &)=delete ;//禁用拷贝构造函数my_unique_ptr & operator=(const my_unique_ptr &)=delete ;//禁用赋值函数my_unique_ptr(my_unique_ptr &&p) noexcept{this->ptr = p.ptr;}//右值拷贝构造引用my_unique_ptr& operator=(my_unique_ptr &&p) noexcept{this->ptr = p.ptr;}//右值赋值引用
};class Test
{
public:string name;
public:Test(){cout<<"无参构造"<<endl;}Test(string n):name(n){cout<<"有参构造"<<endl;}~Test(){cout<<"析构函数"<<endl;}
};int main()
{Test *p1 = new Test("张三");my_unique_ptr<Test> a(p1);cout<<(*p1).name<<endl;cout<<p1->name<<endl;cout<<(*a).name<<endl;cout<<a->name<<endl;cout<<"***********"<<endl;return 0;
}
运行结果:
二、思维导图