【Qt】自定义标题栏并实现最小化/最大化/关闭窗口功能

之前实现了自定义窗口的拖拽移动,见【Qt】自定义标题栏并实现鼠标拖拽移动 今天实现系统按钮:最小化、最大化、关闭窗口

1.先去掉窗口的边框

/* 标题栏样式 */  
this->setWindowFlags(Qt::FramelessWindowHint |  
                     Qt::WindowSystemMenuHint |  
                     Qt::WindowMinMaxButtonsHint);  

2.添加三个私有槽,与一个记录窗口当前状态的变量,并设置初值为 true

private slots:
    /* custom system btn */
    void onMin( bool );
    void onMaxOrNormal( bool );
    void onClose( bool );
private:
    bool maxOrNormal;//true表示当前窗口状态为normal,图标应显示为max

3.添加三个自定义的按钮,图标根据Ui需求确定,我使用系统图标,并连接信号槽

    /* custom system btn */
    ui->btn_close->setStyleSheet("border:none;");
    ui->btn_min->setStyleSheet("border:none;");
    ui->btn_max_nor->setStyleSheet("border:none;");//去掉边框
    QIcon icon;
    icon = style()->standardIcon( QStyle::SP_TitleBarMinButton );
    ui->btn_min->setIcon( icon );
    if( maxOrNormal ){// true represents Max
        icon = style()->standardIcon( QStyle::SP_TitleBarMaxButton );
        ui->btn_max_nor->setIcon( icon );
    }else{
        icon = style()->standardIcon( QStyle::SP_TitleBarNormalButton );
        ui->btn_max_nor->setIcon( icon );
    }
    icon = style()->standardIcon( QStyle::SP_TitleBarCloseButton );
    ui->btn_close->setIcon( icon );

    connect( ui->btn_min, SIGNAL(clicked(bool)), this, SLOT( onMin(bool)) );
    connect( ui->btn_max_nor, SIGNAL(clicked(bool)), this, SLOT( onMaxOrNormal(bool)) );
    connect( ui->btn_close, SIGNAL(clicked(bool)), this, SLOT( onClose(bool)) );

4.槽函数源代码:

void MainWindow::onMin(bool)
{
    if( windowState() != Qt::WindowMinimized ){
        setWindowState( Qt::WindowMinimized );
    }
}

void MainWindow::onMaxOrNormal(bool)
{
    QIcon icon;
    if( maxOrNormal ){
        icon = style()->standardIcon( QStyle::SP_TitleBarNormalButton );
        ui->btn_max_nor->setIcon( icon );
        setWindowState( Qt::WindowMaximized );
    }else {
        icon = style()->standardIcon( QStyle::SP_TitleBarMaxButton );
        ui->btn_max_nor->setIcon( icon );
        setWindowState( Qt::WindowNoState );
    }
    maxOrNormal = !maxOrNormal;
}

void MainWindow::onClose(bool)
{
    close();
}
5.注意事项

1)设置窗口最大化、最小化、原状态也可以使用如下方法

    showMaximized();
    showMinimized();
    showNormal();
2)窗口normal状态是Qt:WindowNoState, 关于Qt::WindowStates有如下几种:

Constant	Value	Description
Qt::WindowNoState	0x00000000	The window has no state set (in normal state).
Qt::WindowMinimized	0x00000001	The window is minimized (i.e. iconified).
Qt::WindowMaximized	0x00000002	The window is maximized with a frame around it.
Qt::WindowFullScreen	0x00000004	The window fills the entire screen without any frame around it.
Qt::WindowActive	0x00000008	The window is the active window, i.e. it has keyboard focus.








  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用Qt的QMainWindow类来自定义标题栏。 首先,需要将窗口标题栏隐藏起来,可以使用setWindowFlags函数来实现: ```cpp setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint); ``` 这里使用了`Qt::FramelessWindowHint`参数来隐藏窗口标题栏,`Qt::WindowSystemMenuHint`参数来显示窗口的系统菜单,`Qt::WindowMinMaxButtonsHint`参数来显示窗口最大化最小化按钮。 接下来,需要在窗口中添加自定义标题栏。可以使用QWidget来实现,将其作为QMainWindow的子控件添加到窗口中: ```cpp QWidget *titleBar = new QWidget(this); titleBar->setObjectName("TitleBar"); titleBar->setStyleSheet("QWidget#TitleBar{background-color: #F0F0F0;}"); titleBar->setFixedHeight(30); QHBoxLayout *layout = new QHBoxLayout(titleBar); layout->setMargin(0); layout->setSpacing(0); QLabel *titleLabel = new QLabel(titleBar); titleLabel->setText("My Title"); titleLabel->setStyleSheet("QLabel{font-size: 14px;color: #333333;}"); layout->addWidget(titleLabel); layout->addStretch(); QToolButton *minButton = new QToolButton(titleBar); minButton->setObjectName("MinButton"); minButton->setStyleSheet("QToolButton{border:none;background-color:transparent;}" "QToolButton:hover{background-color:#E0E0E0;}" "QToolButton:pressed{background-color:#D0D0D0;}"); minButton->setIcon(QIcon(":/titlebar/minimize.png")); minButton->setFixedSize(30, 30); layout->addWidget(minButton); QToolButton *maxButton = new QToolButton(titleBar); maxButton->setObjectName("MaxButton"); maxButton->setStyleSheet("QToolButton{border:none;background-color:transparent;}" "QToolButton:hover{background-color:#E0E0E0;}" "QToolButton:pressed{background-color:#D0D0D0;}"); maxButton->setIcon(QIcon(":/titlebar/maximize.png")); maxButton->setFixedSize(30, 30); layout->addWidget(maxButton); QToolButton *closeButton = new QToolButton(titleBar); closeButton->setObjectName("CloseButton"); closeButton->setStyleSheet("QToolButton{border:none;background-color:transparent;}" "QToolButton:hover{background-color:#E0E0E0;}" "QToolButton:pressed{background-color:#D0D0D0;}"); closeButton->setIcon(QIcon(":/titlebar/close.png")); closeButton->setFixedSize(30, 30); layout->addWidget(closeButton); setMenuWidget(titleBar); ``` 这里创建了一个名为"titleBar"的QWidget作为窗口标题栏,设置了其背景颜色和固定高度。然后创建了一个水平布局并添加了一个QLabel、一个最小化按钮一个最大化按钮一个关闭按钮。最后将标题栏设置为窗口的菜单控件,用于显示系统菜单。 需要注意的是,上面的代码中使用了三个图标文件,需要将它们添加到Qt项目中,并在代码中使用正确的路径。 ```qrc <RCC> <qresource prefix="/titlebar"> <file>minimize.png</file> <file>maximize.png</file> <file>close.png</file> </qresource> </RCC> ``` 最后,需要在窗口实现标题栏按钮功能。可以在按钮的clicked信号中实现最小化最大化关闭窗口功能: ```cpp connect(minButton, &QToolButton::clicked, this, &QMainWindow::showMinimized); connect(maxButton, &QToolButton::clicked, this, &QMainWindow::showMaximized); connect(closeButton, &QToolButton::clicked, this, &QMainWindow::close); ``` 这里使用了Qt的槽函数机制来实现按钮功能。 完整的示例代码可以参考下面的链接: https://github.com/qt/qtbase/blob/dev/examples/widgets/widgets/framelesswindow/main.cpp

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值