QT4.7自定义标题栏简单实现
来源:互联网 发布:linux集成开发环境 编辑:程序博客网 时间:2024/05/21 06:26
QT4.7自定标题栏简单实现:功能尚未完成,但提供了编写模版。实现:窗口拖动、标题长按。
1、定义
#ifndef QTITLEBAR_H#define QTITLEBAR_H#include <QWidget>class QHBoxLayout;class QLabel;class QPalette;class QPixmap;class QBrush;class QPoiint;class QIcon;class QCursor;class QMouseEvent;class QTitleBar : public QWidget{ Q_OBJECTpublic: explicit QTitleBar(QWidget *parent = 0); ~QTitleBar(); void setSize(QSize size); void setIcon(QString sFile,QSize iconSize); void setBackground(QString sFile); void setText(QString sText); void setTitle(QString sTitle); bool isEnable(); //标题栏自定功能,可继续完善protected: void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void enterEvent(QEvent *event); void leaveEvent(QEvent *event);private: void initControl();signals: void longPressSignal(QPoint point);private slots: void onTimeOut();private: bool m_bEnable; //响应拖动 QHBoxLayout *m_pLayout; QLabel *m_pIcon;//图标 QLabel *m_pText;//标题 bool m_bMousePressed; QPoint m_oMousePressPos; QPoint m_oMouseReleasePos; QPoint m_oMouseMovePos; //响应长按 QTimer *m_pTimer; unsigned m_uTimerCntSet; unsigned m_uTimerCnt;};#endif // QTITLEBAR_H
2、实现
#include <QHBoxLayout>#include <QLabel>#include <QPalette>#include <QPixmap>#include <QBrush>#include <QPoint>#include <QIcon>#include <QCursor>#include <QMouseEvent>#include <QTimer>#include "QTitleBar.h"#include "QHelper.h"QTitleBar::QTitleBar(QWidget *parent) : QWidget(parent){ m_bEnable = true; m_pLayout = NULL; m_pIcon = NULL; m_pText = NULL; m_bMousePressed = false; m_pTimer = NULL; m_uTimerCntSet = 1; m_uTimerCnt = 0; this->initControl();}void QTitleBar::initControl(){ m_pLayout = new QHBoxLayout(this); m_pIcon = new QLabel(this); m_pText = new QLabel(this); m_pTimer = new QTimer(this); if(NULL == m_pLayout || NULL == m_pIcon || NULL == m_pText || NULL == m_pTimer) { m_bEnable = false; } else { m_bEnable = true; m_pLayout->addWidget(m_pIcon); m_pLayout->addWidget(m_pText); m_pLayout->setContentsMargins(0,0,0,0); m_pLayout->setStretchFactor(m_pIcon,1); m_pLayout->setStretchFactor(m_pText,10); m_pLayout->setSpacing(0); m_pIcon->setVisible(false); m_pText->setVisible(false); connect(m_pTimer,SIGNAL(timeout()),this,SLOT(onTimeOut())); }}QTitleBar::~QTitleBar(){ QHelper::deleteP(m_pLayout); QHelper::deleteP(m_pIcon); QHelper::deleteP(m_pText); QHelper::deleteP(m_pTimer);}void QTitleBar::setSize(QSize size){ this->setFixedSize(size);}void QTitleBar::setIcon(QString sFile, QSize iconSize){ if(!isEnable()) return ; QPixmap pixmap(sFile); m_pIcon->setPixmap(pixmap.scaled(iconSize)); if(NULL != this->parentWidget()) { this->parentWidget()->setWindowIcon(QIcon(sFile)); } m_pIcon->setVisible(true);}void QTitleBar::setBackground(QString sFile){ this->setAutoFillBackground(true); QPalette palette(this->palette()); QPixmap pixmap(sFile); palette.setBrush(QPalette::Window,QBrush(pixmap)); this->setPalette(palette);}void QTitleBar::setText(QString sText){ if(!isEnable()) return ; m_pText->setText(sText); m_pText->setVisible(true);}void QTitleBar::setTitle(QString sTitle){ if(NULL != this->parentWidget()) { this->parentWidget()->setWindowTitle(sTitle); }}void QTitleBar::mousePressEvent(QMouseEvent *event){ if(NULL == event || !isEnable()) return ; switch(event->button()) { case Qt::LeftButton: { m_bMousePressed = true; //响应拖动 m_oMousePressPos = event->globalPos();//使用全局坐标 m_oMouseMovePos = event->globalPos();//原始坐标,判断鼠标是否有移动 //响应长按 if(!m_pTimer->isActive()) { m_uTimerCnt = 0; m_pTimer->start(1000); } } break; case Qt::RightButton: break; default: break; } return QWidget::mousePressEvent(event);}void QTitleBar::mouseReleaseEvent(QMouseEvent *event){ if(NULL == event || !isEnable()) return ; switch(event->button()) { case Qt::LeftButton: { m_bMousePressed = false; //响应长按 if(m_pTimer->isActive()) { m_uTimerCnt = 0; m_pTimer->stop(); } } break; case Qt::RightButton: break; default: break; } return QWidget::mouseReleaseEvent(event);}void QTitleBar::mouseMoveEvent(QMouseEvent *event){ if(NULL == event || !m_bMousePressed || !isEnable()) return ; //响应拖动 //move event产生的button是nobutton,所以不用判断button类型 QPoint point = event->globalPos();//新位置 int iX = point.x()-m_oMouseMovePos.x();//x位移 int iY = point.y()-m_oMouseMovePos.y();//y位移 m_oMouseMovePos = event->globalPos();//更新鼠标当前位置 if(NULL != this->parentWidget()) { QPoint win = this->parentWidget()->pos(); int x = win.x()+iX;//窗口x应该位移 int y = win.y()+iY;//窗口y应该位移 this->parentWidget()->move(x,y); } //响应长按 if(m_pTimer->isActive()) { m_uTimerCnt = 0; m_pTimer->stop(); } return QWidget::mouseMoveEvent(event);}void QTitleBar::enterEvent(QEvent *event){ if(NULL == event || !isEnable()) return ; this->setCursor(Qt::OpenHandCursor);}void QTitleBar::leaveEvent(QEvent *event){ if(NULL == event || !isEnable()) return ; this->setCursor(Qt::ArrowCursor);}void QTitleBar::onTimeOut(){ //响应鼠标长按1秒事件 if(!m_bMousePressed || (++m_uTimerCnt) < m_uTimerCntSet || !isEnable()) return ; m_uTimerCnt = 0; m_pTimer->stop(); //当外部窗口响应长按信号时若使用模态对话框,则会阻断mouseReleaseEvent,所以此处直接false m_bMousePressed = false; emit longPressSignal(m_oMouseMovePos);}bool QTitleBar::isEnable(){ return m_bEnable;}
1 0
- QT4.7自定义标题栏简单实现
- Android简单自定义标题栏
- 简单的自定义标题栏
- ...Qt实现--自定义标题栏
- android实现自定义标题栏
- 自定义 ToolBar 实现标题栏
- qml实现自定义标题栏按钮
- 自定义Toolbar实现简洁标题栏
- android实现自定义顶部标题栏
- 菜鸟初学android体验之——实现自定义简单标题栏的两种简单方法
- androi实现自定义标题栏、自定义光标
- 实现自定义顶部标题栏(自定义控件)
- 基于百度地图 Android SDKv1.3.5实现简单的定位功能,自定义标题栏
- 基于百度地图 Android SDKv1.3.5实现简单的定位功能,自定义标题栏
- 自定义View之简单顶部标题栏
- Android自定义标题栏的实现,及屏蔽系统标题栏
- Android高版本自定义标题栏实现
- 安卓非自定义标题栏实现“返回”按钮
- 获取系统/sdcard存储空间路径无效的处理
- 同步/异步 与 阻塞/非阻塞的区别
- 线程基础:线程(3)——JAVA中的基本线程操作(中)
- [学习笔记]设计模式[4]-{单件模式}
- 数据库(第一范式,第二范式,第三范式)
- QT4.7自定义标题栏简单实现
- 全文索引
- MySQL开启记录执行过的SQL语句方法
- .NET 调用存储过程详解
- 手链样式
- [学习笔记]设计模式[5]-{命令模式}
- ios 七种手势详解
- 算法笔记_029-约瑟夫斯问题(Java)
- opencv之构建图和最大流/最小割的gcgraph.h源码解读