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. Question regarding overriding QGraphicsScene
QtWS25 Last Chance

Question regarding overriding QGraphicsScene

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.1k Views
  • 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.
  • J Offline
    J Offline
    jimbo2
    wrote on last edited by
    #1

    Hi there, I want to provide my own implementation deriving from QGraphicsScene for the purposes of providing multiple layers of depth for background, item and foreground objects and rendering within these categories in my own ordering. I appear to be missing something quite obvious (and probably rather embarrassing) in my initial stub implementation and was hoping perhaps someone could point me in the right direction. I've overrided the three drawing functions but the breakpoints in DrawBackground() and DrawItems() are not being hit. In the rest of the code I've set a background image and added a few QPixMap derived items and these are being displayed correctly. I'm calling repaint() on the view every 1/60s to trigger redraws.

    The code for the class is as follows:

    @class DisplayScene : public QGraphicsScene

    {

    Q_OBJECT
    

    public:

    explicit DisplayScene(QObject *parent = 0);
    

    protected:

    // from QGraphicsScene
    
    void drawItems(QPainter *painter, int numItems,
    
                   QGraphicsItem *items[],
    
                   const QStyleOptionGraphicsItem options[],
    
                   QWidget *widget = 0);
    
    void drawBackground(QPainter *painter, const QRectF &rect);
    
    void drawForeground(QPainter *painter, const QRectF &rect);
    

    };@

    And:

    @void DisplayScene::drawItems(QPainter *painter, int numItems,
    QGraphicsItem *items[],
    const QStyleOptionGraphicsItem options[],
    QWidget *widget)
    {
    QGraphicsScene::drawItems(painter, numItems, items, options, widget);
    }

    void DisplayScene::drawBackground(QPainter *painter, const QRectF &rect)
    {
    QGraphicsScene::drawBackground(painter, rect);
    }

    void DisplayScene::drawForeground(QPainter *painter, const QRectF &rect)
    {
    QGraphicsScene::drawForeground(painter, rect);
    }@

    Thanks for any help in pointing out what I'm missing.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      If you set a background brush, then the QGV framework calls the function QGraphicsView::drawBackground() instead of the QGraphicsScene::drawBackground(). Similarly for the foreground stuff.

      So either:

      • Dervice a custom class from QGraphicsView and override drawBackground() and drawForeground() there or
      • Don't set a custom background/foreground brush.

      Does that solve it for you?

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jimbo2
        wrote on last edited by
        #3

        Apologies for the late reply, that's completely solved my problem with the background image thank you!

        It appears the reason for drawItems() not being called is because I missed the bit about requiring the Indirect flag to be set since Qt 4.6. I'll perform all my rendering for items in the foreground processing then as I had no intention of using the scene's default implementation.

        Thank you for the help.

        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