Qt软件开发文档6---dockWidget自定义标题栏

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/me_badman/article/details/69384768

Qt 自带的 QDockWidget 的标题栏只有两个按钮, 浮动按钮和 关闭按钮。
往往满足不了需求,只适合需要我们自定义一个标题栏。
如下 标题栏实现了自定义的最小化,最大化,关闭按钮 ,接收到dock传来的标题信息
这里写图片描述
这里写图片描述
直接上代码:
1.声明一个MyDockTitleBar类
MyDockTitleBar.h

#ifndef MYDOCKTITLEBAR_H
#define MYDOCKTITLEBAR_H

#include <QSize>
#include <QDockWidget>
#include <QMouseEvent>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QStyle>

class MyDockTitleBar : public QWidget
{
    Q_OBJECT
public:
    MyDockTitleBar(QString dockName,QWidget *parent = 0);

    QSize sizeHint() const { return minimumSizeHint(); }
    QSize minimumSizeHint() const;

    int width;
    QString name;//receive parent,s name
protected:
    void paintEvent(QPaintEvent *event);//paint titleBar
    void mousePressEvent(QMouseEvent *event); //mouse press

private:
    QPixmap minPix, closePix, floatPix;

};

#endif // MYDOCKTITLEBAR_H

MyDockTitleBar.cpp

#include "myDockTitleBar.h"
MyDockTitleBar::MyDockTitleBar(QString dockName,QWidget *parent)
: QWidget(parent)
{
    //pixMap
    minPix = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
    closePix  = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
    floatPix = style()->standardPixmap(QStyle::SP_TitleBarNormalButton);
    name = dockName;
}

//set TitleBar minimumSize
QSize MyDockTitleBar::minimumSizeHint() const
{
    return QSize(156, 24);
}

//绘制titleBar
void MyDockTitleBar::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QRect rect = this->rect();
    width = rect.width();
    //drawPixmap
    painter.setPen(QPen(QColor(185, 185, 185), 2, Qt::SolidLine));
    painter.setBrush(QBrush(QColor(218, 218, 218), Qt::SolidPattern));
    painter.drawRect(rect.left(), rect.top(), rect.width(), rect.height());
    painter.drawPixmap(rect.topRight() - QPoint(closePix.width() + 10, -6), closePix);
    painter.drawPixmap(rect.topRight() - QPoint(floatPix.width() + 10 + closePix.width() + 10, -6), floatPix);
    painter.drawPixmap(rect.topRight() - QPoint(floatPix.width() + 10 + minPix.width() + 10
                                                + closePix.width() + 10, -3), minPix);
    //drawText
    painter.setPen(QPen(Qt::black, 1, Qt::SolidLine));
    painter.setBrush(Qt::NoBrush);
    painter.drawText(rect.left() + 10, rect.top()-2, rect.width(), rect.height(), Qt::AlignVCenter, name);

    //draw   Blank area
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(QColor(240, 240, 240), Qt::SolidPattern));//设置画刷形式
    painter.drawRect(rect.left(), rect.top() + 19, rect.width(), 5);

}

void MyDockTitleBar::mousePressEvent(QMouseEvent *event)
{
    QPoint pos = event->pos();

    QRect rect = this->rect();

    QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
    Q_ASSERT(dw != 0);

    //in this case,pix width = 10,pix spacing = 10
    int right = rect.right() - pos.x();
    QWidget *widget = dw->widget();
    if (right >= 10 && right <= 20)
    {
        event->accept();
        dw->close();
    }
    else if (right >= 30 && right <= 40){
        event->accept();
        if (widget->isVisible()){

        }
        else {
            widget->show();
            //dw setMinimumWidth initialize
            dw->setMinimumWidth(156);
        }
    }
    else if (right >= 50 && right <= 60){
        event->accept();
        if (widget->isVisible()){
            //click minPix ,dw setMinimumWidth
            dw->setMinimumWidth(width);
            widget->hide();
        }
        else {

        }
    }
}

在窗口中调用

    dock[0] = new QDockWidget(tr("TreeView"),this);
    dock[0]->setFeatures(QDockWidget::AllDockWidgetFeatures);//所有属性
    dock[0]->setAllowedAreas(Qt::LeftDockWidgetArea);//设置允许停靠的窗口位置
    dllName = "treeDll.dll";
    funcName = "showTree";
    MyDockTitleBar *titleBar0 = new MyDockTitleBar(tr("TreeView"), dock[0]);
    dock[0]->setTitleBarWidget(titleBar0);
    QWidget *tree = new QWidget(dock[0]);
    tree = setWidget(dllName,funcName);
    tree->setMinimumHeight(130);
    dock[0]->setWidget(tree);
    addDockWidget(Qt::LeftDockWidgetArea,dock[0]);

本文借鉴:https://www.oschina.net/code/snippet_1029551_20242

展开阅读全文

没有更多推荐了,返回首页