简体   繁体   English

QGraphicsitem上的QT绘制矩形

[英]QT Draw Rectangle on QGraphicsitem

My work Environment: Qt 5.8 MSVC2015 64bit, QT GraphicsView, QGraphicsObject, Windows 7 64 bit. 我的工作环境: Qt 5.8 MSVC2015 64位,QT GraphicsView,QGraphicsObject,Windows 7 64位。

In my application I have added multiple QGraphicsitem, into a single graphic scene & into a single graphic view. 在我的应用程序中,我已将多个QGraphicsitem添加到单个图形场景和单个图形视图中。

But I need to draw empty green color rectangle on top of QGraphicsitem image, as per mouse position. 但是我需要根据鼠标位置在QGraphicsitem图像上方绘制一个空的绿色矩形。 So I tried with below : 所以我尝试了以下:

QRubberBand* _rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
_rubberBand->setGeometry(QRect(MousePos.x() -2, MousePos.y() - 2, MousePos.x() + 2, MousePos.y() + 2).normalized());
_rubberBand->setAutoFillBackground(false);
QPalette pal;
pal.setBrush(QPalette::Highlight, QBrush(Qt::green));
_rubberBand->setPalette(pal);
_rubberBand->show();

Issue with QRubberBand, it changes size dynamically, I want draw small rectangle, not flickery RubberBand. QRubberBand的问题,它会动态更改大小,我想绘制一个小矩形,而不是闪烁的RubberBand。

QRubberBand Output: QRubberBand输出:

QRubBandBand代码输出

Expected Output: 预期产量:

预期产量

@m7913d, tons of thanks for your value added suggestion. @ m7913d,非常感谢您的增值建议。 I have Override QRubberBand class & set QRubberBand class setGeometry, setPen & it's color. 我有覆盖QRubberBand类和设置QRubberBand类setGeometry,setPen及其颜色。

Here is final solution code : 这是最终的解决方案代码:

class roiFrame : public QRubberBand
{
public:
    roiFrame(Shape s, QWidget * p = 0):QRubberBand(s, p){}
    ~roiFrame(){}
protected:
    void paintEvent(QPaintEvent *pe)
    {
        Q_UNUSED(pe);
        QStyleOptionRubberBand opt;
        QStylePainter painter(this);
        opt.initFrom(this);
        QRect rectangle(0,0,30, 15);
        QColor color(Qt::green);
        painter.setPen(color);
        painter.drawRect(rectangle);
    }
};

Caller code : 来电显示:

_rectFrame = new roiFrame(QRubberBand::Rectangle, this);
_rectFrame->setGeometry(QRect(thumbMousePos.x() -1, thumbMousePos.y() - 1, thumbMousePos.x() + 1, thumbMousePos.y() + 1).normalized());
_rectFrame->setAutoFillBackground(false);
_rectFrame->show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM