day50——QT

1> 手写unique_ptr智能指针

cpp 复制代码
#include <iostream>

template<typename T>
class UniquePtr {
public:
    // 构造函数
    UniquePtr(T* ptr = nullptr) : ptr_(ptr) {}

    // 拷贝构造函数和赋值操作符被删除,确保唯一所有权
    UniquePtr(const UniquePtr&) = delete;
    UniquePtr& operator=(const UniquePtr&) = delete;

    // 移动构造函数
    UniquePtr(UniquePtr&& other) noexcept : ptr_(other.ptr_) {
        other.ptr_ = nullptr;
    }

    // 移动赋值操作符
    UniquePtr& operator=(UniquePtr&& other) noexcept {
        if (this != &other) {
            delete ptr_;
            ptr_ = other.ptr_;
            other.ptr_ = nullptr;
        }
        return *this;
    }

    // 析构函数
    ~UniquePtr() {
        delete ptr_;
    }

    // 解引用操作符
    T& operator*() const {
        return *ptr_;
    }

    // 箭头操作符
    T* operator->() const {
        return ptr_;
    }

private:
    T* ptr_;
};

int main() {
    UniquePtr<int> p1(new int(42));
    std::cout << *p1 << std::endl; // 输出42

    // UniquePtr不能被复制,只能通过移动语义进行转移所有权
    // UniquePtr<int> p2 = p1; // 编译错误
    UniquePtr<int> p3(std::move(p1)); // 正确,将p1的所有权转移给p3
    std::cout << *p3 << std::endl; // 输出42
    if (!p1) {
        std::cout << "p1 is empty" << std::endl; // 输出p1 is empty
    }
    if (p3) {
        std::cout << "p3 is not empty" << std::endl; // 输出p3 is not empty
    }

    return 0;
}

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

widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
private slots:
    void on_btn1_clicked();
void close_btn2();
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

main.cpp

cpp 复制代码
#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

widget.cpp

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include<QtDebug>
#include <QIcon>
#include <QPushButton>
#include<QLineEdit>
#include<QLabel>
#include<QMovie>
#include<QString>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //1、有关QT中的信息调试类的使用
    qDebug("hello %d", 520) ;         //类似于printf的使用
    qDebug() << "hello world" << 520;  //类似于cout
    
    //2、有关组件尺寸大小的相关内容
    qDebug() << "this->size = "<< this->size();     //800  600
    qDebug()<<"width = "<<this->width()<<"    heigh = "<<this->height()<<endl;   //800   600
    qDebug()<<"width = "<<this->rect().width()<<"    height = "<< this->rect().height();  //800  600
    
    this->resize(400, 300);        //更改当前界面的尺寸
    this->resize(QSize(1000,800));  //使用类对象进行更改尺寸
    this->setMaximumSize(1000,900);     //设置最大尺寸
    this->setMinimumSize(200,100);         //设置最小尺寸
    this->setFixedSize(500,400);          //设置固定尺寸
    
    //2、有关组件的名称
    qDebug() << "this.tittle = "<<this->windowTitle();        //获取当前窗口标题
    this->setWindowTitle("My First Window");                  //设置窗口标题
    
    //3、设置窗体图标
    this->setWindowIcon(QIcon("F:\\QQfile\\pictrue\\pictrue\\logo.png"));
    //this->setWindowFlag(Qt::FramelessWindowHint);      //设置去除头部的窗口
    //this->showMaximized();                       //设置窗口最大化
    
    //4、设置窗口的样式表
    //this->setStyleSheet("background-color:pink;");
    //this->setWindowOpacity(0.1);                      //设置窗口透明度
    
    //5、有关窗口的为止
    this->move(200,300);          //相对于整个屏幕左上角进行偏移
    qDebug()<<"this.posion = "<<this->pos();    //当前窗口的为止
    
    //1、使用无参构造添加一个按钮
    QPushButton *btn1 = new QPushButton;    //无参构造
    btn1->setParent(this);    //给组件指定父组件,让其依附于界面而存在
    btn1->setText("登录");         //给组件设置文本内容
    btn1->resize(QSize(100,50));        //设置按钮组件的大小
    btn1->move(125,300);            //移动组件位置
    
    //2、构造按钮时给定文本内容以及父组件
    QPushButton *btn2 = new QPushButton("退出", this);
    btn2->resize(btn1->size());
    btn2->move(btn1->x()+150, btn1->y());
    
    //1、构造一个行编辑器,构造时给定父组件
    QLineEdit *edit1 = new QLineEdit(this);
    //edit1->setText("请输入。。。");       //设置编辑器中的文本内容
    edit1->setPlaceholderText("账号");        //设置编辑器的占位文本
    qDebug() << edit1->text();
    edit1->setObjectName("usrwd");
    edit1->resize(200,40);            //设置尺寸
    edit1->move(btn1->x()+50, btn1->y()-125);       //移动位置
    edit1->setEnabled(true);            //设置可用状态
    //2、构造一个行编辑器,构造时给定父组件以及文本内容
    QLineEdit *edit2 = new QLineEdit("", this);
    qDebug() << edit2->text();              //获取行编辑器中文本内容
    edit2->setObjectName("passwd");
    edit2->setPlaceholderText("密码");
    edit2->resize(edit1->size());
    edit2->move(edit1->x(), edit1->y()+50);
    edit2->setEchoMode(QLineEdit::Password);          //设置回显模式
    //1、实例化一个标签
    QLabel *lab1 = new QLabel("账号", this);
    lab1->resize(50,50);
    lab1->move(edit1->x()-50, edit1->y());
    lab1->setScaledContents(true);              //设置内容自适应
    //2、实例化一个标签
    QLabel *lab2 = new QLabel("密码", this);
    lab2->resize(50,50);
    lab2->move(edit2->x()-50, edit2->y());
    lab2->setScaledContents(true);              //设置内容自适应
    //3、实例化一个标签
    QLabel *lab3 = new QLabel("密码", this);
    lab3->resize(500,150);
    lab3->move(0,0);
    //1、实例化一个动图
    QMovie *movie=new QMovie("F:\\QQfile\\pictrue\\pictrue\\zz.gif");
    lab3->setMovie(movie);
    movie->start();
    lab3->setScaledContents(true);              //设置内容自适应
    connect(btn1, &QPushButton::clicked, this, &Widget::on_btn1_clicked);
    connect(btn2, &QPushButton::clicked, this, &Widget::close_btn2);
}
void Widget::on_btn1_clicked()
{
    // 获取行编辑器中的内容
    QString str1 = findChild<QLineEdit*>("usrwd")->text();
    QString str2 = findChild<QLineEdit*>("passwd")->text();  
    if (str1 == str2 ) {
        qDebug() << "登录成功";
        this->close();        //关闭当前界面
    } else {
        qDebug() << "登录失败,请重新登录";
    }
}
void Widget::close_btn2()
{
    this->close();
}

Widget::~Widget()
{
    delete ui;
}
相关推荐
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner2 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz7 天前
QML Hello World 入门示例
qt
xcyxiner10 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner11 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner11 天前
DicomViewer (添加模型类)3
qt
xcyxiner12 天前
DicomViewer (目录调整) 2
qt
xcyxiner12 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00614 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术14 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript