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. QPainter in QGraphicsView::drawBackground() does not paint anything?
Forum Updated to NodeBB v4.3 + New Features

QPainter in QGraphicsView::drawBackground() does not paint anything?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.2k 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.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by Niagarer
    #1

    Hey,
    I have a QGraphicsView class and implemented the funtion drawBackground:

    void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
    {
        Q_UNUSED(rect);
    
    
        QRectF sceneRect = this->sceneRect();
    
        QLinearGradient sceneGradient(sceneRect.topLeft(), sceneRect.bottomRight());
        sceneGradient.setColorAt(0, Qt::white);
        sceneGradient.setColorAt(1, Qt::black);
        painter->fillRect(sceneRect, sceneGradient);
        painter->setBrush(Qt::NoBrush);
        painter->drawRect(sceneRect);
    }
    

    Unfortunately something is missing, but what?
    (I am sure, the answer will be just a single line or something like that, I just took it from one of the examples and so far can't find the missing part)
    Thanks for answers

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Do you have a scene set? If not QGraphicsView will call the default paintEvent of the scroll view and your method won't be called.

      NiagarerN 1 Reply Last reply
      0
      • Chris KawaC Chris Kawa

        Do you have a scene set? If not QGraphicsView will call the default paintEvent of the scroll view and your method won't be called.

        NiagarerN Offline
        NiagarerN Offline
        Niagarer
        wrote on last edited by
        #3

        @Chris-Kawa
        Yes, in the constructor:

            QGraphicsScene *scene = new QGraphicsScene(this);
            scene->setItemIndexMethod(QGraphicsScene::NoIndex);
            scene->setSceneRect(-200, -200, 400, 400);
            setScene(scene);
            setCacheMode( CacheBackground );
            setViewportUpdateMode(BoundingRectViewportUpdate);
            setRenderHint((QPainter::Antialiasing));
            setTransformationAnchor(AnchorUnderMouse);
            scale(qreal(10-0), qreal(10.0));
        
        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          Your sample works for me:
          graphicsview

          The problem must be elsewhere in your code.

          NiagarerN 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            Your sample works for me:
            graphicsview

            The problem must be elsewhere in your code.

            NiagarerN Offline
            NiagarerN Offline
            Niagarer
            wrote on last edited by
            #5

            @Chris-Kawa
            Found it!
            As I saw now, I also implemented the paintEvent.
            So, just had to delete it.
            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