Qt之自定义界面添加最小化、最大化和关闭按钮

#include "dialog.h"
#include<QToolButton>
#include<QPixmap>
#include<QStyle>
#include<QApplication>

Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
    int wide = width();//获取界面的宽度

    //构建最小化、最大化、关闭按钮
    QToolButton *minButton = new QToolButton(this);
    QToolButton *closeButton= new QToolButton(this);
    QToolButton *maxButton= new QToolButton(this);

    //获取最小化、关闭按钮图标
    QPixmap minPix  = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
    QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
    QPixmap maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);

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

    //设置最小化、关闭按钮在界面的位置
    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()));
}

Dialog::~Dialog() {}

void Dialog::windowclosed()
{
    //QApplication::exit();
    this->close();
}
void Dialog::windowmin()
{
    this->showMinimized();
}
void Dialog::winmax()
{
    this->showMaximized();
}

 

    原文作者:DayDay_Upppp
    原文地址: https://blog.csdn.net/DayDay_Upppp/article/details/86148781
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞