Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem how to change the boundary colors of the rectangale
Forum Updated to NodeBB v4.3 + New Features

QGraphicsItem how to change the boundary colors of the rectangale

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 4 Posters 12.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

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

    1 Reply Last reply
    1
    • Q Offline
      Q Offline
      Qt Enthusiast
      wrote on last edited by
      #3

      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

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        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

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          Qt Enthusiast
          wrote on last edited by Qt Enthusiast
          #5

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

          mrjjM 1 Reply Last reply
          0
          • Q Qt Enthusiast

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #6

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

            1 Reply Last reply
            1
            • Q Offline
              Q Offline
              Qt Enthusiast
              wrote on last edited by
              #7

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

              mrjjM 1 Reply Last reply
              0
              • Q Qt Enthusiast

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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @Qt-Enthusiast
                sorry didnt see any sample code.
                Its covered by QPainter and QPens docs.

                1 Reply Last reply
                1
                • Q Offline
                  Q Offline
                  Qt Enthusiast
                  wrote on last edited by
                  #9

                  how to get pointer of Qpainter and QPen
                  objects in QGraphicsitem

                  mrjjM 1 Reply Last reply
                  0
                  • Q Qt Enthusiast

                    how to get pointer of Qpainter and QPen
                    objects in QGraphicsitem

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @Qt-Enthusiast
                    In paintevent you have painter.
                    it has both.

                    1 Reply Last reply
                    1
                    • Q Offline
                      Q Offline
                      Qt Enthusiast
                      wrote on last edited by Qt Enthusiast
                      #11

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

                      mrjjM 1 Reply Last reply
                      0
                      • Q Qt Enthusiast

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

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @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 :)

                        1 Reply Last reply
                        1
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by A Former User
                          #13

                          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!

                          C 1 Reply Last reply
                          2
                          • Q Offline
                            Q Offline
                            Qt Enthusiast
                            wrote on last edited by
                            #14

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

                            1 Reply Last reply
                            0
                            • ? A Former User

                              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!

                              C Offline
                              C Offline
                              CorD SaC
                              wrote on last edited by
                              #15

                              @Wieland said in QGraphicsItem how to change the boundary colors of the rectangale:

                              ere 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),

                              Could you please explain why this error occur. ```
                              error: 'myitem' does not name a type
                              myitem *myitem = new myitem;

                              mrjjM 1 Reply Last reply
                              0
                              • C CorD SaC

                                @Wieland said in QGraphicsItem how to change the boundary colors of the rectangale:

                                ere 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),

                                Could you please explain why this error occur. ```
                                error: 'myitem' does not name a type
                                myitem *myitem = new myitem;

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @CorD-SaC

                                The myitem.h is a custom file

                                if you do not have it, it means that
                                it dont know what myitem is for a class.

                                • 'myitem' does not name a type
                                  =
                                  What the heck is "myitem" , compiler says
                                C 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @CorD-SaC

                                  The myitem.h is a custom file

                                  if you do not have it, it means that
                                  it dont know what myitem is for a class.

                                  • 'myitem' does not name a type
                                    =
                                    What the heck is "myitem" , compiler says
                                  C Offline
                                  C Offline
                                  CorD SaC
                                  wrote on last edited by
                                  #17

                                  @mrjj

                                  problem is solved,I change the code, myitem *mi = new myitem(); instead of
                                  //myitem *myitem = new myitem(); I don't know what was the reason,however after change the code like this,problem solved.

                                  mrjjM 1 Reply Last reply
                                  0
                                  • C CorD SaC

                                    @mrjj

                                    problem is solved,I change the code, myitem *mi = new myitem(); instead of
                                    //myitem *myitem = new myitem(); I don't know what was the reason,however after change the code like this,problem solved.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #18

                                    Oh the type (of the class) was used as variable also

                                    like
                                    int *int = new int()

                                    that is not allowed :)

                                    C 1 Reply Last reply
                                    1
                                    • mrjjM mrjj

                                      Oh the type (of the class) was used as variable also

                                      like
                                      int *int = new int()

                                      that is not allowed :)

                                      C Offline
                                      C Offline
                                      CorD SaC
                                      wrote on last edited by
                                      #19

                                      @mrjj

                                      Ooops,thanks :)

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved