Qt 扁平化MainWindow以及自定义标题栏

为了构建出自己认为优雅的界面,需要将Qt自带标题栏啥的去掉。
首先使用Qt Designer创建一个MainWindow的类。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_actionMinimize_triggered();     //最小化窗口
    void on_actionClose_triggered();        //关闭窗口
    void on_actionMaximize_triggered();     //最大化窗口

protected:
    //改写QWidget的函数,自己重新实现拖动操作
    void mouseMoveEvent ( QMouseEvent * event );
    void mousePressEvent ( QMouseEvent * event );
    void mouseReleaseEvent(QMouseEvent * event);

private:
    Ui::MainWindow *ui;

    //自己重新实现拖动操作
    QPoint mousePosition;
    bool isMousePressed;

};

#endif // MAINWINDOW_H

mainwindow.cpp

#include
#include 

#include "mainwindow.h"
#include "ui_mainwindow.h"

using namespace std;

//标题栏的长度
const static int pos_min_x = 0;
const static int pos_max_x = 800 - 120;  //为最小化和关闭按钮留空间
const static int pos_min_y = 0;
const static int pos_max_y = 20;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);//去掉窗口标题栏

    int width = this->width();//获取界面的宽度
    QToolButton *minButton = new QToolButton(this);   //最小按钮
    QToolButton *helpButton = new QToolButton(this);  //帮助按钮
    QToolButton *closeButton= new QToolButton(this);  //关闭按钮
    QToolButton *maxButton = new QToolButton(this);   //最大按钮
    QToolButton *menuButton = new QToolButton(this);  //菜单按钮
    QToolButton *normalButton = new QToolButton(this);
    QToolButton *shadeButton = new QToolButton(this);
    QToolButton *unshadeButton = new QToolButton(this);

    connect(minButton, SIGNAL(clicked()), this, SLOT(on_actionMinimize_triggered()));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(on_actionClose_triggered()));
    connect(maxButton, SIGNAL(clicked()), this, SLOT(on_actionMaximize_triggered()));

    //获取最小化、关闭按钮图标
    QPixmap minPix  = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
    QPixmap helpPix = style()->standardPixmap(QStyle::SP_TitleBarContextHelpButton);
    QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
    QPixmap maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);
    QPixmap shadePix = style()->standardPixmap(QStyle::SP_TitleBarShadeButton);
    QPixmap unshadePix = style()->standardPixmap(QStyle::SP_TitleBarUnshadeButton);
    QPixmap normalPix = style()->standardPixmap(QStyle::SP_TitleBarNormalButton);
    QPixmap menuPix = style()->standardPixmap(QStyle::SP_TitleBarMenuButton);

    //设置最小化、关闭按钮图标
    minButton->setIcon(minPix);
    closeButton->setIcon(closePix);
    helpButton->setIcon(helpPix);
    maxButton->setIcon(maxPix);
    shadeButton->setIcon(shadePix);
    unshadeButton->setIcon(unshadePix);
    normalButton->setIcon(normalPix);
    menuButton->setIcon(menuPix);

    //设置最小化、关闭按钮在界面的位置
    normalButton->setGeometry(width-160, 0, 20, 20);
    menuButton->setGeometry(width-140, 0, 20, 20);
    minButton->setGeometry(width-120,0,20,20);
    closeButton->setGeometry(width-100,0,20,20);
    helpButton->setGeometry(width-80,0,20,20);
    maxButton->setGeometry(width-60, 0, 20, 20);
    shadeButton->setGeometry(width-40,0, 20, 20);
    unshadeButton->setGeometry(width-20, 0, 20, 20);

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

    //设置最小化、关闭等按钮的样式
    minButton->setStyleSheet("background-color:transparent;");
    closeButton->setStyleSheet("background-color:transparent;");
    normalButton->setStyleSheet("background-color:transparent;");
    menuButton->setStyleSheet("background-color:transparent;");
    helpButton->setStyleSheet("background-color:transparent;");
    maxButton->setStyleSheet("background-color:transparent;");
    shadeButton->setStyleSheet("background-color:transparent;");
    unshadeButton->setStyleSheet("background-color:transparent;");
}

MainWindow::~MainWindow()
{
    delete ui;
}


//自己实现的窗口拖动操作
void MainWindow::mousePressEvent(QMouseEvent *event)
{
        //当鼠标单击窗体准备拖动时,初始化鼠标在窗体中的相对位置
        mousePosition = event->pos();
        //只对标题栏范围内的鼠标事件进行处理
        if (mousePosition.x()<=pos_min_x)
            return;
        if ( mousePosition.x()>=pos_max_x)
            return;
        if (mousePosition.y()<=pos_min_y )
            return;
        if (mousePosition.y()>=pos_max_y)
            return;
        isMousePressed = true;
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if ( isMousePressed==true )
    {
        QPoint movePot = event->globalPos() - mousePosition;
        //move是移动的位置是相对于全局而言(即屏幕)
        move(movePot);
    }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    isMousePressed=false;
}

void MainWindow::on_actionMinimize_triggered()
{
    //系统自定义的最小化窗口函数
    showMinimized();

}
void MainWindow::on_actionClose_triggered()
{
    //系统自定义的窗口关闭函数
    close();
}

void MainWindow::on_actionMaximize_triggered()
{
    //最大化 showMaximized()
    showMaximized();
}

效果展示

Qt 扁平化MainWindow以及自定义标题栏_第1张图片

Note: 本文这里并没有实现随窗体大小动态改变而使标题栏上按钮的相对位置发生改变,感谢大家的帮助。

Reference:
http://blog.sina.com.cn/s/blog_a6fb6cc90101auay.html
http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html

你可能感兴趣的:(Qt)