如何在Qt中使用事件过滤器避免多重继承?

发布于 2025-01-09 20:43:59 字数 1157 浏览 1 评论 0原文

在我的项目中,我使用 QGraphicsView 和 QGraphicsScene 绘制各种形状,如矩形、折线、文本。效果很好。但它存在多重继承的问题。我从多个类派生了 SchematicDesign 类。

这是它的 .h 文件:

class SchematicDesign : virtual public QGraphicsRectItem, virtual public QGraphicsPathItem, virtual public QGraphicsView
public:
    explicit SchematicDesign();

    explicit SchematicDesign(qreal x, qreal y, qreal width, qreal height,QGraphicsItem *parent = nullptr)
        : QGraphicsRectItem(x, y, width, height, parent)
        {}

    explicit SchematicDesign(QPainterPath pPath);
signals:

public:
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    QGraphicsRectItem* createRect(qreal x, qreal y, qreal width, qreal height);
    QGraphicsPathItem* drawPolyline(QPolygonF poly);
    QGraphicsPathItem* addText(QGraphicsRectItem *rect, double d1, double d2, QString s);
};

所以我被告知,删除多重继承。为此,他们建议,

使用事件过滤器,它会调用事件(只有 1 个事件) 在这种情况下,通过调整各种标志,您可以使用以下命令绘制形状 油漆

是我没有完全理解这个想法。我读到了有关事件过滤器的内容,但问题仍然是我不打算使用 QGraphicsRectItem 或 QGraphicsPathItem 来绘制矩形或多段线吗?
那个绘画方法是QPainter?
谁能帮助我理解这一点?
一个小例子会很有帮助

In my project, I was drawing various shapes like rectangle, polyline, text using QGraphicsView and QGraphicsScene. It works fine. But it has a problem of multiple inheritance. I have derived my SchematicDesign class from multiple classes.

Here is it's .h file :

class SchematicDesign : virtual public QGraphicsRectItem, virtual public QGraphicsPathItem, virtual public QGraphicsView
public:
    explicit SchematicDesign();

    explicit SchematicDesign(qreal x, qreal y, qreal width, qreal height,QGraphicsItem *parent = nullptr)
        : QGraphicsRectItem(x, y, width, height, parent)
        {}

    explicit SchematicDesign(QPainterPath pPath);
signals:

public:
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    QGraphicsRectItem* createRect(qreal x, qreal y, qreal width, qreal height);
    QGraphicsPathItem* drawPolyline(QPolygonF poly);
    QGraphicsPathItem* addText(QGraphicsRectItem *rect, double d1, double d2, QString s);
};

So I was told that, remove multiple inheritance. For that they suggested,

Use Event filter, it will call event ( only 1 event will be there )
and in that event, by adjusting various flags, you can draw shapes using
paint

Now the problem is I did not get this idea completely. I read about Event filter, but question remains that Am I not going to use QGraphicsRectItem or QGraphicsPathItem to draw rectangular or polyline ?
That paint method is QPainter ?
Can anyone help me to understand this ?
A small example will be very helpful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文