Qt隐藏边框后添加最大化最小化关闭按钮

#include"InitButtonTopRight.h"
#include<QToolButton>  
#include<QPixmap>  
#include<QStyle>  
#include<QApplication>
#include<QDebug>
#include<QHBoxLayout>


InitButtonTopRight::InitButtonTopRight(QWidget* widget)
    :QWidget(widget)
{
    Qt::WindowFlags flags = Qt::Window;
    flags |= Qt::WindowMinimizeButtonHint;
    flags |= Qt::WindowMaximizeButtonHint;
    flags |= Qt::WindowCloseButtonHint;
    setWindowFlags(flags);
    this->setWindowFlags(Qt::FramelessWindowHint);
    label = new QLabel();
    label->setText("WindowTittle");
    int wide = width();//获取界面的宽度
    int heigh = height();
    //构建最小化、最大化、关闭按钮 
    //qDebug() << "Window width is:" << wide;
    QToolButton* position = new QToolButton(this);
    minButton = new QToolButton(this);
    closeButton = new QToolButton(this);
    maxButton =  new QToolButton(this);
    m_isMaxflags = false;
    //获取最小化、关闭按钮图标  
    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);
    //设置最小化、关闭按钮在界面的位置  
    /*position->setGeometry(0, 1, 20, 20);
    minButton->setGeometry(wide-40, 1, 200, 20);
    maxButton->setGeometry(wide-20, 1, 20, 20);
    closeButton->setGeometry(wide, 1, 20, 20);*/
    QHBoxLayout* layout = new QHBoxLayout();
    layout->insertWidget(0, label);
    layout->insertStretch(1);
    layout->insertWidget(2, minButton);
    layout->insertWidget(3, maxButton);
    layout->insertWidget(4, closeButton);
    layout->setSpacing(0);
    QVBoxLayout* main_layout = new QVBoxLayout();
    main_layout->insertLayout(0,layout);
    main_layout->insertStretch(1);
    setLayout(main_layout);


    //设置鼠标移至按钮上的提示信息  
    minButton->setToolTip(QString::fromLocal8Bit("最小化"));
    closeButton->setToolTip(QString::fromLocal8Bit("关闭"));
    maxButton->setToolTip(QString::fromLocal8Bit("最大化"));
    //设置最小化、关闭按钮的样式
    position->setStyleSheet("background-color:transparent;");
    minButton->setStyleSheet("background-color:transparent;");
    closeButton->setStyleSheet("background-color:transparent;");
    maxButton->setStyleSheet("background-color:transparent;");
    this->resize(600,500);
    connect(closeButton, SIGNAL(clicked()), this, SLOT(windowClose()));
    connect(minButton, SIGNAL(clicked()), this, SLOT(windowMin()));
    connect(maxButton, SIGNAL(clicked()), this, SLOT(windowMax()));
}


void InitButtonTopRight::windowClose()
{
    this->close();
}


void InitButtonTopRight::windowMin()
{
    setWindowState(Qt::WindowMinimized);
}


void InitButtonTopRight::windowMax()
{
    if (!m_isMaxflags)
    {
        setWindowState(Qt::WindowMaximized);
        m_isMaxflags = true;
        return;
    }
    setWindowState(Qt::WindowNoState);
    m_isMaxflags = false;
} 微笑
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值