如何在Qt中使用事件过滤器避免多重继承?
在我的项目中,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论