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
Qt 6.11 is out! See what's new in the release blog

QGraphicsItem how to change the boundary colors of the rectangale

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 4 Posters 13.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.
  • 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