【Qt】自定义标题栏添加最小化、最大化(还原)和关闭按钮

创建界面时系统会自带一个标题栏,如果我们需要设计一个好看的界面,这个标题栏就会显得很突兀,所以我们需要自定义标题栏,隐藏原来的标题栏。

自定义标题栏,这些图标我们都可以自己设置,位置可以调整

具体步骤如下:

1.隐藏系统自带的标题栏

this->setWindowFlags(Qt::FramelessWindowHint);//隐藏边框

去掉标题栏会存在几个问题:

①界面会因为失去边框丢失调整大小的能力
②去除边框后,界面内容将无法移动

③原本自带的最小化、最大化、关闭按钮随着标题栏一起被隐藏了,所以接下来的步骤我们进行添加最小化、最大化、关闭按钮

2.创建最小化、最大化、关闭按钮

3.获取并设置按钮图标

4.设置按钮在界面的位置

 5.设置提示信息,样式和信号槽

具体代码

    //窗口标题栏设置
    this->setWindowFlags(Qt::FramelessWindowHint);//隐藏边框
    //构建最小化、最大化、关闭按钮
    minButton = new QToolButton(this);
    closeButton= new QToolButton(this);
    maxButton= new QToolButton(this);

    //获取最小化、关闭按钮图标
    QPixmap minPix  = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
    QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
    maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);//最大化需要在事件中进行判断更换图标所以设置成数据成员

    //设置最小化、关闭按钮图标
    minButton->setIcon(minPix);
    closeButton->setIcon(closePix);
    maxButton->setIcon(maxPix);

    //设置最小化、关闭按钮在界面的位置
    int wide = width();//获取界面的宽度
    minButton->setGeometry(wide-65,5,20,20);
    closeButton->setGeometry(wide-25,5,20,20);
    maxButton->setGeometry(wide-45,5,20,20);

    //设置鼠标移至按钮上的提示信息
    minButton->setToolTip(tr("最小化"));
    closeButton->setToolTip(tr("关闭"));
    maxButton->setToolTip(tr("最大化"));

    //设置最小化、关闭按钮的样式
    minButton->setStyleSheet("background-color:transparent;");
    closeButton->setStyleSheet("background-color:transparent;");
    maxButton->setStyleSheet("background-color:transparent;");
    //每个按钮对应的信号槽
    connect(closeButton, SIGNAL(clicked()), this, SLOT(windowclosed()));
    connect(minButton, SIGNAL(clicked()), this, SLOT(windowmin()));
    connect(maxButton, SIGNAL(clicked()), this, SLOT(winmax()));

 槽函数

public slots:
    void windowclosed();//最小化 关闭
    void windowmin();
    void winmax();



//槽函数实现
void Logwin::windowclosed()
{
    this->close();
}
void Logwin::windowmin()
{
    this->showMinimized();//最小化
}
void Logwin::winmax()
{
    if (this->isMaximized())
    {
        this->showNormal();//还原事件
        maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);
        maxButton->setIcon(maxPix);
        maxButton->setToolTip(tr("最大化"));
    }
    else
    {
        this->showMaximized();//最大化事件
        restore = style()->standardPixmap(QStyle::SP_TitleBarNormalButton);
        maxButton->setIcon(restore);
        maxButton->setToolTip(tr("最大化还原"));
    }
}

实现的效果

未放大时,鼠标移到最大化按钮上

 放大时,鼠标移到上面,图标不同,内容也不同

 

QStyle::StandardPixmap枚举

获取的图标信息,如果觉得系统给的不好看,可以自己添加

 参考QStyle(4):StandardPixmap和StyleHint枚举_hyongilfmmm的博客-CSDN博客

感谢观看!!!!

以上就是全部内容,如果对您有帮助,欢迎点赞评论,或者发现有哪里写错的,欢迎指正!

  • 10
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

logani

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值