100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > qt实现抽屉效果 类似qq的好友功能

qt实现抽屉效果 类似qq的好友功能

时间:2022-04-09 21:02:02

相关推荐

qt实现抽屉效果 类似qq的好友功能

qt有自带的抽屉控件,叫qtoolbox,但是我觉得比较难用,于是便自己写了一个,基本的收缩啊,添加窗体功能都有的。直接看核心代码吧

.h

#ifndef MYDRAWER_H#define MYDRAWER_H#include <QWidget>#include <QLabel>#include <QScrollArea>#include <QVBoxLayout>#include <QFrame>class MyDrawer : public QFrame{Q_OBJECTpublic:explicit MyDrawer(QWidget *parent = nullptr);~MyDrawer();void AddWidget(QWidget *Wgt);void SetTitle(QString i_title);protected:void resizeEvent(QResizeEvent *event);void mousePressEvent(QMouseEvent *event);signals:private:QWidget *m_pCenterWgt=nullptr;QLabel *m_pTextLabel=nullptr;QLabel *m_pIconLabel=nullptr;QVBoxLayout *m_pCenterLayout=nullptr;QWidget *m_ptitleWgt=nullptr;QScrollArea *m_pArea=nullptr;bool m_ScaleBool=false;};#endif // MYDRAWER_H

.cpp

#include "mydrawer.h"#include <QVBoxLayout>#include <QResizeEvent>MyDrawer::MyDrawer(QWidget *parent) : QFrame(parent){this->setFrameStyle(QFrame::Panel | QFrame::Raised);m_pCenterWgt=new QWidget(this);m_pCenterLayout=new QVBoxLayout(this);m_pCenterLayout->setMargin(5);m_pCenterLayout->setSpacing(5);m_pCenterWgt->setLayout(m_pCenterLayout);m_pTextLabel=new QLabel(this);m_pIconLabel=new QLabel(this);m_pIconLabel->setFixedSize(20,20);QPixmap pixMap(":/img/right.png");m_pIconLabel->setPixmap(pixMap);m_pTextLabel->setFixedWidth(150);m_ptitleWgt=new QWidget(this);QHBoxLayout *titleLayout=new QHBoxLayout(this);titleLayout->addWidget(m_pIconLabel);titleLayout->addWidget(m_pTextLabel);titleLayout->setMargin(0);titleLayout->setSpacing(3);m_ptitleWgt->setLayout(titleLayout);m_ptitleWgt->setFixedHeight(30);m_pArea=new QScrollArea(this);m_pArea->setWidget(m_pCenterWgt);m_pArea->hide();QVBoxLayout *mainLayout=new QVBoxLayout(this);mainLayout->addWidget(m_ptitleWgt);mainLayout->addWidget(m_pArea);mainLayout->setMargin(0);mainLayout->setSpacing(0);this->setLayout(mainLayout);}MyDrawer::~MyDrawer(){}void MyDrawer::AddWidget(QWidget *Wgt){m_pCenterLayout->addWidget(Wgt);}void MyDrawer::SetTitle(QString i_title){m_pTextLabel->setText(i_title);}void MyDrawer::resizeEvent(QResizeEvent *event){m_pCenterWgt->resize(this->width(),this->height());}void MyDrawer::mousePressEvent(QMouseEvent *event){if(event->button()==Qt::LeftButton){if(m_ptitleWgt->geometry().contains(this->mapFromGlobal(QCursor::pos()))){m_ScaleBool=!m_ScaleBool;if(m_ScaleBool){m_pArea->show();m_pIconLabel->setPixmap(QPixmap(":/img/down.png"));}else{m_pArea->hide();m_pIconLabel->setPixmap(QPixmap(":/img/right.png"));}}}}

然后看下效果图:

有什么不懂的欢迎在评论区留言。。。。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。