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. [SOLVED]Why QGraphicsScene does not add the QGraphicsRect?
Qt 6.11 is out! See what's new in the release blog

[SOLVED]Why QGraphicsScene does not add the QGraphicsRect?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.9k Views 1 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.
  • H Offline
    H Offline
    heatblazer
    wrote on last edited by
    #1

    Hello, and happy tomorrow new year! I have a simple code, I must draw a rect in QGraphicScene but it`s not displayed. Here is the code:
    @
    myScene = new QGraphicsScene;
    myView = new QGraphicsView(myScene);
    myView->setMaximumWidth(width);
    myView->setMaximumHeight(height);
    myScene->setBackgroundBrush(QBrush(Qt::red));
    myScene->setSceneRect(QRectF(0,0,width-(width/40),
    height-(height/40)) );
    screenItem = new ActionRectItem(100, 100, 100, 100,
    "Screen", 1, NULL,
    myScene,
    new Callbacks(dummy3,
    dummy1,
    dummy2,
    doer));

    ((QGraphicsRectItem*)screenItem)->setBrush(QBrush(Qt::green));
    myScene->addItem((QGraphicsRectItem*)screenItem);
    myView->show();
    

    @
    Where ActionRectItem is subclass of QGraphicsRect item. Ive tried with a pure QGraphicsRectItem, its not added aswell.
    [EDIT] Ill add the QGraphicsRectItem is not displayed, not the scene. [EDIT] It appeared that Ive forgot to implement my custom overriden paint() from QGraphicsRectItem:
    @
    void ActionRectItem::paint(QPainter painter, const QStyleOptionGraphicsItem option, QWidget widget)
    {
    functors->onPaint(painter, option, widget, (ActionRectItem
    )this);
    }
    @
    And when I implement:
    @
    static inline void customOnPaint(QPainter
    qp,
    const QStyleOptionGraphicsItem
    qsi,
    QWidget* wd, QGraphicsItem* ari) {

    QBrush Brush(Qt::green);
    ((QGraphicsRectItem*)ari)->setBrush(QBrush(Qt::red));
    ((QGraphicsRectItem*)ari)->setRect(
                ((QGraphicsRectItem*)ari)->boundingRect()
                );
    qp->fillRect(((QGraphicsRectItem*)ari)->boundingRect()
            , Brush);
    

    }
    @

    Its working now, it was my mistake. Sorry.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      I think there are some wrong size calculations.
      The example below shows green square on red view.
      @
      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);

      QGraphicsScene* myScene = new QGraphicsScene(this);
      QGraphicsView* myView = new QGraphicsView(myScene, ui->centralWidget);
      myView->setScene(myScene);
      int width = this->width();
      int height = this->height();
      myView->setMaximumWidth(width);
      myView->setMaximumHeight(height);
      myScene->setBackgroundBrush(QBrush(Qt::red));
      myScene->setSceneRect(QRectF(0, 0, width, height));
      QGraphicsRectItem* screenItem = new QGraphicsRectItem(100, 100, 100, 100);
      

      // screenItem = new ActionRectItem(100, 100, 100, 100,
      // "Screen", 1, NULL,
      // myScene,
      // new Callbacks(dummy3,
      // dummy1,
      // dummy2,
      // doer));

      ((QGraphicsRectItem*)screenItem)->setBrush(QBrush(Qt::green));
      myScene->addItem((QGraphicsRectItem*)screenItem);
      

      // myView->show();
      }
      @

      1 Reply Last reply
      0
      • H Offline
        H Offline
        heatblazer
        wrote on last edited by
        #3

        You are right. But why is that... Kind of messy it got here :S

        1 Reply Last reply
        0
        • H Offline
          H Offline
          heatblazer
          wrote on last edited by
          #4

          OK, so I have QGraphicsItem, with overriden methods, paint, advance and onMousePressEvent. So, how I add the QGraphicsRectItem to be below the invisible QGraphicsItem, who is overriden, or how do I pass these functions to QGraphicsRectItem?

          1 Reply Last reply
          0
          • H Offline
            H Offline
            heatblazer
            wrote on last edited by
            #5

            Ive solved it :( Appeared to be entirely my mistake, since I was working with it and Ive forgot an important override.

            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