简体   繁体   中英

QT Draw Rectangle on QGraphicsitem

My work Environment: Qt 5.8 MSVC2015 64bit, QT GraphicsView, QGraphicsObject, Windows 7 64 bit.

In my application I have added multiple QGraphicsitem, into a single graphic scene & into a single graphic view.

But I need to draw empty green color rectangle on top of QGraphicsitem image, as per mouse position. 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 Output:

QRubBandBand代码输出

Expected Output:

预期产量

@m7913d, tons of thanks for your value added suggestion. I have Override QRubberBand class & set QRubberBand class setGeometry, setPen & it's color.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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