-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamplewindow2.cpp
More file actions
79 lines (68 loc) · 2.53 KB
/
samplewindow2.cpp
File metadata and controls
79 lines (68 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <QVBoxLayout>
#include <QApplication>
#include <QStyle>
#include <QLineEdit>
#include <QComboBox>
#include <QRadioButton>
#include <QProgressBar>
#include "samplewindow2.h"
CustomTitleBar::CustomTitleBar(QWidget *parent)
: FramelessWindowTitleBar{parent}
{
m_tabBar = new QTabBar(this);
m_minimizeButton = new QPushButton("最小化", this);
m_maximizeButton = new QPushButton("最大化", this);
m_closeButton = new QPushButton("关闭", this);
/*
* BUG:FramelessWindow通过nativeEvent实现鼠标悬浮在最大化按钮触发snap layout的功能
* 时,发送QEvent::MouseMove事件,但是目标收到的是QEvent::HoverMove事件,无法触发
* QPushButton的状态变化。暂时通过这个代码处理事件改变QPushButton的状态。QPushButton
* 也可以是普通的QWidget
*/
m_maximizeButton->installEventFilter(this);
QHBoxLayout *rootLayout = new QHBoxLayout(this);
rootLayout->setContentsMargins(0, 0, 0, 0);
rootLayout->addWidget(m_tabBar);
rootLayout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Preferred));
rootLayout->addWidget(m_minimizeButton);
rootLayout->addWidget(m_maximizeButton);
rootLayout->addWidget(m_closeButton);
QObject::connect(m_minimizeButton, &QPushButton::clicked, this, [this](){
window()->showMinimized();
});
QObject::connect(m_closeButton, &QPushButton::clicked, this, [this](){
window()->close();
});
QObject::connect(m_tabBar, &QTabBar::currentChanged, this, &CustomTitleBar::currentTabChange);
}
void CustomTitleBar::onWindowStateChanged(Qt::WindowState state){
switch(state){
case Qt::WindowState::WindowNoState:
m_maximizeButton->setText("最大化");
break;
case Qt::WindowState::WindowMaximized:
m_maximizeButton->setText("还原");
break;
default:
break;
}
}
QWidget *CustomTitleBar::maximizeButton() const{
return m_maximizeButton;
}
QTabBar *CustomTitleBar::tabBar() const{
return m_tabBar;
}
SampleWindow2::SampleWindow2()
: FramelessWindow{}
{
CustomTitleBar *titleBar = new CustomTitleBar(this);
QLabel *body = new QLabel(this);
setTitleBarAndBodyWidget(titleBar, body);
QObject::connect(titleBar, &CustomTitleBar::currentTabChange, this, [this, body](int index){
body->setText(QString("当前:%1").arg(QString::number(index)));
});
for(int i = 0; i < 10; ++i){
titleBar->tabBar()->addTab(QString("第%1个").arg(QString::number(i)));
}
}