QT QDockWidget titleBar重绘

为了修改样式,便于后续皮肤的更换,重绘了QDockWidget 的titleBar
totalTitleBar.h

#ifndef TOTALTITLEBAR_H
#define TOTALTITLEBAR_H

#include 
#include 
#include 
#include 
#include 
#include 
#include "qdebug.h"
#include 



class TotalTitleBar : public
	QWidget
{
	Q_OBJECT
public:
	TotalTitleBar(QWidget *parent = 0);
	QColor backgroudColor = QColor(67, 67, 67, 100); // 标题栏默认背景颜色
	int fontSize = 10; // 默认标题栏文本字号
	QString titleBarText = ""; // 默认标题栏文本
	QColor fontColor = QColor(220, 220, 220);  // 默认标题栏文本颜色
	void setAttr(QColor _backgroudColor, int _fontSize, QString _titleBarText, QColor _fontColor);
	QSize sizeHint() const
	{
		return
			minimumSizeHint();
	}

	QSize minimumSizeHint() const;

protected:
	void paintEvent(QPaintEvent *event);
	void mousePressEvent(QMouseEvent *event);

	public slots:
	void updateMask();

private:
	QPixmap minPix, closePix, floatPix;

};





#endif // TOTALTITLEBAR_H

totalTitleBar…cpp

#include "totalTitleBar.h"

TotalTitleBar::TotalTitleBar(QWidget *parent)
	: QWidget(parent)

{
	//minPix = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
	//closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
	//floatPix = style()->standardPixmap(QStyle::SP_TitleBarNormalButton);

}



QSize TotalTitleBar::minimumSizeHint() const

{
	QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());

	Q_ASSERT(dw != 0);

	QSize result(100, 30);

	if
		(dw->features() & QDockWidget::DockWidgetVerticalTitleBar)
		result.transpose();

	return
		result;

}

void TotalTitleBar::paintEvent(QPaintEvent*)

{
	QPainter painter(this);
	QRect rect = this->rect();
	QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
	Q_ASSERT(dw != 0);

	if
		(dw->features() & QDockWidget::DockWidgetVerticalTitleBar) {
		QSize s = rect.size();
		s.transpose();
		rect.setSize(s);

		painter.translate(rect.left(), rect.top() + rect.width());
		painter.rotate(-90);
		painter.translate(-rect.left(), -rect.top());

	}
	painter.fillRect(rect.left(), rect.top(), rect.width(), rect.height(), backgroudColor); //QColor(67, 67, 67, 100)
	painter.drawPixmap(rect.topRight() - QPoint(closePix.width() + 10, -10), closePix);
	painter.drawPixmap(rect.topRight() - QPoint(minPix.width() + 10 + closePix.width() + 10, -7), minPix);
	painter.drawPixmap(rect.topRight() - QPoint(floatPix.width() + 10 + minPix.width() + 10 + closePix.width() + 10, -10), floatPix);

	QFont font1("微软雅黑", fontSize, false, false); //fontSize 10
	painter.setFont(font1);
	painter.setPen(fontColor); // fontColor QColor(220, 220, 220)
	painter.drawText(rect.left() + 5, 20, titleBarText); // titleBarText QStringLiteral("动画")
}

void TotalTitleBar::mousePressEvent(QMouseEvent *event)

{

	QPoint pos = event->pos();
	QRect rect = this->rect();
	QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
	Q_ASSERT(dw != 0);
	if
		(dw->features() & QDockWidget::DockWidgetVerticalTitleBar) {

		QPoint p = pos;
		pos.setX(rect.left() + rect.bottom() - p.y());
		pos.setY(rect.top() + p.x() - rect.left());



		QSize s = rect.size();
		s.transpose();
		rect.setSize(s);

	}
	const int buttonRight = 10;
	const int buttonWidth = 20;
	int right = rect.right() - pos.x();
	int button = (right - buttonRight) / buttonWidth;

	qDebug() << rect.right() << " --- " << pos.x() << " --- " << right << " --- " << button;

	switch
		(button) {

	case 0:
		event->accept();
		dw->hide();
		break;

	case 1:
		event->accept();
		dw->setFloating(!dw->isFloating());
		break;

	case 2: {
		event->accept();
		QDockWidget::DockWidgetFeatures features = dw->features();
		if
			(features & QDockWidget::DockWidgetVerticalTitleBar)
			features &= ~QDockWidget::DockWidgetVerticalTitleBar;
		else
			features |= QDockWidget::DockWidgetVerticalTitleBar;

		dw->setFeatures(features);
		break;

	}
	default:
		event->ignore();
		break;

	}

}

void TotalTitleBar::setAttr(QColor _backgroudColor = QColor(67, 67, 67, 100), int _fontSize = 10, QString _titleBarText = QStringLiteral(""), QColor _fontColor = QColor(220, 220, 220))
{
	backgroudColor = _backgroudColor;
	fontSize = _fontSize; // 默认标题栏文本字号
	titleBarText = _titleBarText; // 默认标题栏文本
	fontColor = _fontColor;  // 默认标题栏文本颜色
	update();
}

void TotalTitleBar::updateMask()
{
}

使用方法可直接new,使用默认值,或者new后调用setAttr来update()重绘

你可能感兴趣的:(QT,C++,QT,QDockWidget)