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

    Hi
    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
    {

    private:
    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

    Hi
    Where you draw it. just change the pen.
    http://doc.qt.io/qt-5/qpainter.html#pen
    QPen mypen(White)
    painter.setPen(mypen)

    please see the docs
    http://doc.qt.io/qt-5/qpen.html



  • 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 {

    private
    }

    void GQPhyItem::updateConfig()
    {
    QColor color = QColor(Qt::red);
    brush.setColor(color);
    brush.setStyle(color);
    }


  • Qt Champions 2016

    @Qt-Enthusiast

    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

    @Qt-Enthusiast
    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

    @Qt-Enthusiast
    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

    @Qt-Enthusiast

    • 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.

    mainwindow.cpp

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

    myitem.h

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

    myitem.cpp

    #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)
    {
        Q_UNUSED(option)
        Q_UNUSED(widget)
        QPen pen( m_color ? QColor("lime") : QColor("purple") );
        pen.setWidth(m_penWidth);
        painter->setPen(pen);
        painter->drawRect(-20, -20, 40, 40);
    }
    
    void MyItem::toggleColor()
    {
        m_color = !m_color;
        update();
    }
    

    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.