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_OBJECT
public:
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;
}