Qt修改QMainWindow的标题栏样式

Qt修改QMainWindow的标题栏样式

  • 自定义标题栏样式

  • 效果图
    Qt修改QMainWindow的标题栏样式_第1张图片

  • 代码展示

class WidgetBase : public QWidget
{
	Q_OBJECT
public:
	explicit WidgetBase(QWidget *parent = 0);
};

class TitleStyle : public QWidget
{
    Q_OBJECT

public:
    TitleStyle(QWidget *parent = nullptr);
    ~TitleStyle();

	void SetTitleContent(const char* pContent);

public slots:
	void OnMin();
	void OnMax();
	void OnClose();

protected:
	virtual void mouseDoubleClickEvent(QMouseEvent *event);
	virtual void mousePressEvent(QMouseEvent *event);
	virtual bool eventFilter(QObject *obj, QEvent *event);

private:
	void InitSignalSlot();
	void InitSytleSheet();
private:
	Ui::TitleStyleClass ui;
};
#include "TitleStyle.h"
#include 
#include 

TitleStyle::TitleStyle(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

	InitSignalSlot();

	InitSytleSheet();
}

TitleStyle::~TitleStyle()
{}


void TitleStyle::SetTitleContent(const char* pContent)
{
	if (pContent)
	{
		ui.lblTitle->setText(QString::fromLocal8Bit(pContent));
	}
}

void TitleStyle::OnMin()
{
	QWidget *pWindow = this->window();
	if (pWindow->isWindow())
	{
		pWindow->showMinimized();
	}
}

void TitleStyle::OnMax()
{
	QWidget *pWindow = this->window();
	if (pWindow->isWindow())
	{
		pWindow->isMaximized() ? pWindow->showNormal() : pWindow->showMaximized();
	}
}

void TitleStyle::OnClose()
{
	QWidget *pWindow = this->window();
	if (pWindow->isWindow())
	{
		pWindow->close();
	}
}

void TitleStyle::mouseDoubleClickEvent(QMouseEvent *event)
{
	Q_UNUSED(event);
	emit ui.btnMax->clicked();
}

void TitleStyle::mousePressEvent(QMouseEvent *event)
{
	if (ReleaseCapture())
	{
		QWidget *pWindow = this->window();
		if (pWindow->isWindow())
		{
			SendMessage(HWND(pWindow->winId()), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
		}
	}
	event->ignore();
}

bool TitleStyle::eventFilter(QObject *obj, QEvent *event)
{
	switch (event->type())
	{
	case QEvent::WindowTitleChange:
	{
		QWidget *pWidget = qobject_cast(obj);
		if (pWidget)
		{
			ui.lblTitle->setText(pWidget->windowTitle());
			return true;
		}
	}
	case QEvent::WindowIconChange:
	{
		QWidget *pWidget = qobject_cast(obj);
		if (pWidget)
		{
			QIcon icon = pWidget->windowIcon();
			ui.lblIcon->setPixmap(icon.pixmap(ui.lblIcon->size()));
			return true;
		}
	}
	case QEvent::WindowStateChange:
	case QEvent::Resize:
	{
		return true;
	}
	default:
	{
		return false;
	}
	}
	return QWidget::eventFilter(obj, event);
}

void TitleStyle::InitSignalSlot()
{
	connect(ui.btnMin, SIGNAL(clicked()), this, SLOT(OnMin()));
	connect(ui.btnMax, SIGNAL(clicked()), this, SLOT(OnMax()));
	connect(ui.btnClose, SIGNAL(clicked()), this, SLOT(OnClose()));
}

void TitleStyle::InitSytleSheet()
{
	ui.btnMin->setIcon(QIcon(":/image/image/btnMin.png"));
	ui.btnMax->setIcon(QIcon(":/image/image/btnMax.png"));
	ui.btnClose->setIcon(QIcon(":/image/image/btnClose.png"));

	ui.lblIcon->setScaledContents(true);
	ui.lblIcon->setPixmap(QPixmap(":/image/image/lblIcon.png"));
	
	ui.lblTitle->setStyleSheet("color: #FF96DB");

	ui.widget->setStyleSheet("background-color: rgb(85, 170, 0);");
}

WidgetBase::WidgetBase(QWidget *parent /*= 0*/)
{

}
  • 主窗口调用
QtWidgetsApplication6::QtWidgetsApplication6(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	WidgetBase *pTopWindow = new WidgetBase();
	pTopWindow->setWindowFlags(Qt::FramelessWindowHint);

	this->setParent(pTopWindow);
	TitleStyle *pTitleWidget = new TitleStyle(pTopWindow);
	pTitleWidget->SetTitleContent("测试程序");
	pTopWindow->setSizePolicy(this->sizePolicy());
	pTopWindow->setMaximumSize(this->maximumSize());
	pTopWindow->setMaximumSize(this->maximumSize());

	this->installEventFilter(pTitleWidget);
	QGridLayout *pLayout = new QGridLayout();
	pLayout->addWidget(pTitleWidget);
	pLayout->addWidget(this);
	pLayout->setSpacing(0);
	pLayout->setContentsMargins(0, 0, 0, 0);
	pTopWindow->setLayout(pLayout);

	pTopWindow->show();
}
  • 需要自己用Qt Designer创建一个布局界面,如下图:
    Qt修改QMainWindow的标题栏样式_第2张图片
  • 标记自己学习

你可能感兴趣的:(编程,qt,ui,开发语言)