QGraphicsItem how to change the boundary colors of the rectangale

  • I have a QGraphicsItem as a rectangle , I want to change the boundary color of the rectangle and Please note the fill color of rectangle should remain same

    What is correct way to do it

  • Qt Champions 2016

    If you are painting the rectangle yourself then
    the QPen and its color should be what you are after.

  • I have following class

    class myItem::public QGraphicsItem

    QRectF rect;
    QBrush brush;

    Can you write the sample code , for the same

    How can I use brush to just color the boundary color and keep the fill color as same

  • Qt Champions 2016

    Where you draw it. just change the pen.
    QPen mypen(White)

    please see the docs

  • Once it is drawn , I want to change the boundary on already drawn rectangle on a button click

    For example I want to change the boundary when on a button click and I have object of QGraphicitem , if you could guide me

    Currently I have slot was following but clears the fill color of the QGraphictsitem

    class myItem :: QGraphictsitem {


    void GQPhyItem::updateConfig()
    QColor color = QColor(Qt::red);

  • Qt Champions 2016


    Well first u need to be able to change it. and get the effect you want.

    When that works, you need to make a slot in the item so you can
    ask it to change.

    Then you need to hook up the button so it triggers this slots to change the pen.

  • if you could point me to sample code that will be helpful

  • Qt Champions 2016

    sorry didnt see any sample code.
    Its covered by QPainter and QPens docs.

  • how to get pointer of Qpainter and QPen
    objects in QGraphicsitem

  • Qt Champions 2016

    In paintevent you have painter.
    it has both.

  • so you mean after the slot
    can I need to call i assume you have myItem::paintEvent ? is called automatcially after the slot

  • Qt Champions 2016


    • can I need to call i assume you have myItem::paintEvent ? is called automatcially after the slot

    Not sure what u say here :)

  • Hi! Here is a minimal example. Note that we need to derive our custom item not from QGraphicsItem but from QGraphicsObject so that we can use signals and slots with it.


    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QGraphicsScene>
    #include "myitem.h"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        QGraphicsScene *scene = new QGraphicsScene;
        MyItem *myItem = new MyItem();
        scene->addItem( myItem );
        // ui->pushButton_2 is the button to toggle the color of your rectangle
        connect(ui->pushButton_2, &QPushButton::clicked, myItem, &MyItem::toggleColor);


    #ifndef MYITEM_H
    #define MYITEM_H
    #include <QGraphicsObject> 
    // it uses signals & slots, so inheriting from QGraphicsItem wouldn't be enough
    class MyItem : public QGraphicsObject
        MyItem(QGraphicsItem *parent = Q_NULLPTR);
        QRectF boundingRect() const override;
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    public slots:
        void toggleColor();
        const qreal m_penWidth = 10;
        bool m_color = false; // color of the rectangle; true = lime, false = purple
    #endif // MYITEM_H


    #include "myitem.h"
    #include <QPen>
    #include <QPainter>
    MyItem::MyItem(QGraphicsItem *parent)
        : QGraphicsObject(parent)
    QRectF MyItem::boundingRect() const
        return QRectF(-20 - m_penWidth / 2, -20 - m_penWidth / 2, 40 + m_penWidth, 40 + m_penWidth);
    void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
        QPen pen( m_color ? QColor("lime") : QColor("purple") );
        painter->drawRect(-20, -20, 40, 40);
    void MyItem::toggleColor()
        m_color = !m_color;

    Hope it helps!

  • One more question , how to get filled color of colored QGraphicsitem filled rectangle

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.