Qt 自定義窗口的標題欄,重寫鼠標事件實現(xiàn),關閉隱藏,最大化/最小化,重寫窗口事件函數(shù),實現(xiàn)鼠標選中邊框拉大拉小,雙擊標題欄切換窗口最大化和最小化
1、main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
2、widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
// 重寫窗口事件函數(shù),實現(xiàn)鼠標選中邊框拉大拉小
#include <windows.h>
#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
protected:
// 重寫鼠標事件,實現(xiàn)(隱藏窗口,最大化/最小化窗口,關閉窗口)
void mousePressEvent(QMouseEvent *event) override; // 重寫鼠標按下事件
void mouseMoveEvent(QMouseEvent *event) override; // 重寫鼠標移動事件
void mouseReleaseEvent(QMouseEvent *event) override; // 重寫鼠標釋放事件
// 重寫窗口事件函數(shù),實現(xiàn)鼠標選中邊框拉大拉小
bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;
// 雙擊titleBar切換窗口最大化和最小化,重寫mouseDoubleClickEvent函數(shù),這個函數(shù)會在鼠標雙擊事件發(fā)生時被調(diào)用
void mouseDoubleClickEvent(QMouseEvent *event) override;
/* 1、將函數(shù)聲明為virtual的作用是允許這兩個函數(shù)在派生類中被重寫(override),
* 因為resizeEvent()和moveEvent()是QWidget類中的虛函數(shù),因此在派生類中也要將其聲明為virtual,
* 這樣就可以在派生類中重新定義這兩個函數(shù),以實現(xiàn)派生類中對窗口大小改變和窗口移動事件的處理
*/
virtual void resizeEvent(QResizeEvent *event);
virtual void moveEvent(QMoveEvent *event);
private:
QWidget *content; // 中央控件
QWidget *titleBar; // 自定義標題欄
bool isPressed; // 鼠標是否按下
QPoint startPos; // 鼠標按下時的位置
QPoint endPos; // 鼠標釋放時的位置
int edge; // 窗口邊緣寬度
// 聲明標題欄槽函數(shù)(隱藏窗口,最大化/最小化窗口,關閉窗口)
private slots:
void on_tbn_min_clicked();
void on_tbn_max_clicked();
void on_tbn_close_clicked();
};
#endif // WIDGET_H
3、widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent> // 1、包含QMouseEvent類的頭文件
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QDesktopWidget>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
isPressed = false;
edge = 5;
// 設置窗口無邊框和透明背景
setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
// 2、去除默認的標題欄和邊框
// Qt::FramelessWindowHint:這是一個窗口提示,表示窗口沒有邊框
// Qt::WindowMinimizeButtonHint:這是一個窗口提示,表示窗口有最小化按鈕
// Qt::WindowMaximizeButtonHint:這是一個窗口提示,表示窗口有最大化按鈕
// Qt::WindowCloseButtonHint:這是一個窗口提示,表示窗口有關閉按鈕
//this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
// 創(chuàng)建content容器,放到Widget窗口上
content = new QWidget(this);
// 設置 objectName 屬性,QSS 文件中使用 #objectName 來定位到該控件
content->setObjectName("content");
// 設置content的大小策略,就是當窗口大小變化時,content怎么調(diào)整自己的大小,第一個參數(shù)是水平方向上的策略,第二個參數(shù)是垂直方向上的策略
content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// 設置content的位置和大小,參數(shù)是x坐標、y坐標、寬度和高度
content->setGeometry(0, 0, width(), height());
content->setStyleSheet("background-color:red;");
// 在content上創(chuàng)建一個垂直布局,下面的2種方法都可以
// content->setLayout(contentVLayout);
QVBoxLayout *contentVLayout = new QVBoxLayout(content);
// 3、自定義標題欄(隱藏窗口,最大化/最小化窗口,關閉窗口)
// 創(chuàng)建titleBar容器,放到content容器里
// titleBar = new QWidget(content);
titleBar = new QWidget();
titleBar->setObjectName("titleBar");
titleBar->setStyleSheet("background-color:blue;");
// 把titleBar添加到contentVLayout布局中
contentVLayout->addWidget(titleBar);
// 設置對齊方式為頂部對齊,讓titleBar顯示在垂直布局中的最上面
contentVLayout->setAlignment(titleBar, Qt::AlignTop);
// 設置內(nèi)容邊距為0,讓它的子部件緊貼著它的邊緣
// 給contentVLayout這個垂直布局設置內(nèi)邊距為0,內(nèi)邊距是指布局和它的子部件之間的空隙
// setContentsMargins()方法4個參數(shù),分別表示左邊距,上邊距,右邊距和下邊距
contentVLayout->setContentsMargins(0, 0, 0, 0);
// 水平方向上可以自動擴展,在垂直方向上保持固定大小
titleBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
// 設置標題欄控件的位置和大小
titleBar->setGeometry(0, 0, width(), 60);
// 在titleBar上創(chuàng)建一個水平布局
QHBoxLayout *titleBarHLayout = new QHBoxLayout(titleBar);
// Qt::AlignLeft 水平方向靠左
// Qt::AlignRight 水平方向靠右
// Qt::AlignHCenter 水平方向居中
// Qt::AlignTop 垂直方向靠上
titleBarHLayout->setAlignment(Qt::AlignHCenter);
// 在titleBarHLayout上添加你想要的控件,比如圖標、標題、最小化按鈕、最大化按鈕、關閉按鈕等
QLabel *iconLabel = new QLabel(titleBar); // 創(chuàng)建圖標標簽
iconLabel->setPixmap(QPixmap(":/imagenns/icon.png")); // 設置圖標標簽的圖片
iconLabel->setScaledContents(true); // 設置圖標標簽的圖片自動縮放
iconLabel->setFixedSize(20, 20); // 設置圖標標簽的固定大小
QLabel *titleLabel = new QLabel(titleBar); // 創(chuàng)建標題標簽
titleLabel->setText("QWidget布局"); // 設置標題標簽的文本
titleLabel->setFont(QFont("Arial", 12, QFont::Bold)); // 設置標題標簽的字體
QPushButton *minButton = new QPushButton(titleBar); // 創(chuàng)建最小化按鈕
minButton->setText("-"); // 設置最小化按鈕的文本
minButton->setFixedSize(20, 20); // 設置最小化按鈕的固定大小
QPushButton *maxButton = new QPushButton(titleBar); // 創(chuàng)建最大化按鈕
maxButton->setText("+"); // 設置最大化按鈕的文本
maxButton->setFixedSize(20, 20); // 設置最大化按鈕的固定大小
QPushButton *closeButton = new QPushButton(titleBar); // 創(chuàng)建關閉按鈕
closeButton->setText("x"); // 設置關閉按鈕的文本
closeButton->setFixedSize(20, 20); // 設置關閉按鈕的固定大小
// 將這些控件添加到水平布局中,并設置合適的間距和彈簧
titleBarHLayout->addWidget(iconLabel); // 將圖標標簽添加到水平布局中
titleBarHLayout->addSpacing(10); // 添加10像素的間距
titleBarHLayout->addWidget(titleLabel); // 將標題標簽添加到水平布局中
titleBarHLayout->addStretch(); // 添加彈簧,使得后面的按鈕靠右對齊
titleBarHLayout->addWidget(minButton); // 將最小化按鈕添加到水平布局中
titleBarHLayout->addWidget(maxButton); // 將最大化按鈕添加到水平布局中
titleBarHLayout->addWidget(closeButton); // 將關閉按鈕添加到水平布局中
connect (minButton, SIGNAL (clicked ()), this, SLOT (on_tbn_min_clicked ())); // 連接最小化按鈕的信號和槽
connect (maxButton, SIGNAL (clicked ()), this, SLOT (on_tbn_max_clicked ())); // 連接最大化按鈕的信號和槽
connect (closeButton, SIGNAL (clicked ()), this, SLOT (on_tbn_close_clicked ())); // 連接關閉按鈕的信號和槽
}
Widget::~Widget()
{
delete ui;
}
// 最小化按鈕的槽函數(shù)
void Widget::on_tbn_min_clicked()
{
// 最小化窗口
showMinimized();
}
// 最大化按鈕的槽函數(shù)
void Widget::on_tbn_max_clicked()
{
if (isMaximized()) {
// 還原窗口
showNormal();
// 切換圖標
qobject_cast<QPushButton *>(sender())->setText("+"); // 使用qobject_cast轉換類型,并修改文本
} else {
// 最大化窗口
showMaximized();
qobject_cast<QPushButton *>(sender())->setText("-");
}
}
// 關閉按鈕的槽函數(shù)
void Widget::on_tbn_close_clicked()
{
close();
}
// 重寫鼠標事件,實現(xiàn)窗口的移動
void Widget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) // 如果是左鍵按下
{
isPressed = true; // 設置鼠標按下標志為真
startPos = event->globalPos(); // 記錄鼠標按下時的全局坐標
endPos = this->frameGeometry().topLeft(); // 記錄窗口左上角的坐標
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if (isPressed) // 如果鼠標按下
{
QPoint movePos = event->globalPos() - startPos; // 計算鼠標移動的偏移量
this->move(endPos + movePos); // 移動窗口到新的位置
}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) // 如果是左鍵釋放
{
isPressed = false; // 設置鼠標按下標志為假
int x = this->x(); // 獲取窗口當前的x坐標
int y = this->y(); // 獲取窗口當前的y坐標
// 判斷窗口是否超出屏幕邊緣,如果超出則調(diào)整位置
if (x < 0)
x = 0;
if (y < 0)
y = 0;
if (x + this->width() > QApplication::desktop()->width())
x = QApplication::desktop()->width() - this->width();
if (y + this->height() > QApplication::desktop()->height())
y = QApplication::desktop()->height() - this->height();
this->move(x, y); // 移動窗口到新的位置
}
}
// 鼠標選中窗口邊框拉大拉小
bool Widget::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
MSG* msg = (MSG*)message;
switch (msg->message)
{
case WM_NCHITTEST: // 處理鼠標在窗口邊緣的事件
{
int xPos = GET_X_LPARAM(msg->lParam) - this->frameGeometry().x(); // 獲取鼠標相對于窗口左上角的x坐標
int yPos = GET_Y_LPARAM(msg->lParam) - this->frameGeometry().y(); // 獲取鼠標相對于窗口左上角的y坐標
// 根據(jù)鼠標位置判斷返回值,改變鼠標樣式和窗口拉伸方向
if(xPos < edge && yPos < edge) // 左上角
*result = HTTOPLEFT;
else if(xPos < edge && yPos > height() - edge) // 左下角
*result = HTBOTTOMLEFT;
else if(xPos > width() - edge && yPos < edge) // 右上角
*result = HTTOPRIGHT;
else if(xPos > width() - edge && yPos > height() - edge)// 右下角
*result = HTBOTTOMRIGHT;
else if(xPos < edge) // 左邊
*result = HTLEFT;
else if(xPos > width() - edge) // 右邊
*result = HTRIGHT;
else if(yPos < edge) // 上邊
*result = HTTOP;
else if(yPos > height() - edge) // 下邊
*result = HTBOTTOM;
else // 其他部分不做處理,返回false,留給其他事件處理器處理
return false;
return true;
}
default:
break;
}
return QWidget::nativeEvent(eventType, message, result); // 將事件傳遞給父類處理
}
/* 如果要實現(xiàn)widget窗口跟隨鼠標拖動并自動跟隨窗口縮放,
* 需要在QWidget類中重寫resizeEvent()函數(shù)和moveEvent()函數(shù)
*/
// 在resizeEvent()函數(shù)中,重新設置widget窗口的大小和位置
void Widget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event); // 調(diào)用父類的resizeEvent()函數(shù)
if(this->content) // 判斷是否有widget被添加到Material窗口中
{
// 獲取Material窗口的大小
int w = this->width();
int h = this->height();
// 設置widget的大小為Material窗口的大小
this->content->setGeometry(0, 0, w, h);
}
}
// 在moveEvent()函數(shù)中,重新設置widget窗口的位置
void Widget::moveEvent(QMoveEvent *event)
{
QWidget::moveEvent(event);
if(this->content)
{
this->content->move(0, 0);
}
}
// 雙擊titleBar切換窗口最大化和最小化,重寫mouseDoubleClickEvent函數(shù),這個函數(shù)會在鼠標雙擊事件發(fā)生時被調(diào)用
void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
// 如果是左鍵雙擊
if (event->button() == Qt::LeftButton)
{
// 獲取父窗口的指針
QWidget *pWindow = this->window();
// 判斷當前是否是最大化狀態(tài)
if (pWindow->isMaximized())
{
// 如果是,恢復到正常大小
pWindow->showNormal();
}
else
{
// 如果不是,最大化窗口
pWindow->showMaximized();
}
}
}
4、效果展示文章來源:http://www.zghlxwxcb.cn/news/detail-577034.html
5、完成文章來源地址http://www.zghlxwxcb.cn/news/detail-577034.html
到了這里,關于Qt 自定義窗口的標題欄,重寫鼠標事件實現(xiàn),關閉隱藏,最大化/最小化,重寫窗口事件函數(shù),實現(xiàn)鼠標選中邊框拉大拉小,雙擊標題欄切換窗口最大化和最小化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!