在QDodckWidget中的停靠小部件上绘制/绘制 [英] Paint/Draw on top of docked widgets in QDodckWidget

查看:117
本文介绍了在QDodckWidget中的停靠小部件上绘制/绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Qt中有一个继承QDockWidget的类。该类包含另一个小部件。
是否有可能在我的QDockWidget继承类中定义一个函数,该函数在包含的小部件之上绘制东西?就像绘画独立于包含的小部件,但要链接到继承的类。

I have a class in Qt that inherits QDockWidget. And that class contains another widget. Is there any possibility to define a function in my QDockWidget inherited class that draws stuff on top of the contained widget? Like the painting to be independent of the contained widget but to be linked to the inherited class.

谢谢

推荐答案

当然可以。事实上,这很简单。您需要在 QDockWidget 中放置一个位于其他所有内容之上的子窗口小部件。要这样做,它必须是您添加到dockwidget的最后一个子窗口小部件。该小部件不得绘制其背景,然后它可以绘制dockwidget的任何子节点。小部件的大小必须跟踪父小部件的大小。

Sure it's possible. It is fairly simple to do, in fact. You need to place a child widget that sits on top of everything else in your QDockWidget. To do it so, it must be the last child widget you add to your dockwidget. That widget must not to draw its background, and it can then draw over any children of the dockwidget. The widget's size must track the size of the parent widget.

下面是一个自包含的示例。

Below is a self-contained example.

// https://github.com/KubaO/stackoverflown/tree/master/questions/overlay-line-11034838
#include <QtGui>
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif

class Line : public QWidget {
protected:
   void paintEvent(QPaintEvent *) override {
        QPainter p(this);
        p.setRenderHint(QPainter::Antialiasing);
        p.drawLine(rect().topLeft(), rect().bottomRight());
    }
public:
    explicit Line(QWidget *parent = nullptr) : QWidget(parent) {
       setAttribute(Qt::WA_TransparentForMouseEvents);
    }
};

class Window : public QWidget {
    QHBoxLayout layout{this};
    QPushButton left{"Left"};
    QLabel right{"Right"};
    Line line{this};
protected:
    void resizeEvent(QResizeEvent *) override {
        line.resize(size());
    }
public:
    explicit Window(QWidget *parent = nullptr) : QWidget(parent) {
        layout.addWidget(&left);
        right.setFrameStyle(QFrame::Box | QFrame::Raised);
        layout.addWidget(&right);
        line.raise();
    }
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    Window w;
    w.show();
    return app.exec();
}

这篇关于在QDodckWidget中的停靠小部件上绘制/绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆