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. QGraphicsScene clear() problems
Qt 6.11 is out! See what's new in the release blog

QGraphicsScene clear() problems

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.1k 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.
  • R Offline
    R Offline
    Raaap
    wrote on last edited by
    #1

    I have this custom class which inherits from QGraphicsRectItem.

    [code]
    class CellGraphic : public QGraphicsRectItem
    {
    private:
    static QColor active, inactive, noConnection;

    Cell *cell;
    bool isBorderCell;
    

    // Various functions
    }[/code]

    I create a number of these on the heap and add them to a QGraphicsScene object. Every once in a while I need to dump them and create the scene from scratch, so I empty the scene by calling clear() and then create new ones which I add to the scene.

    This works as expected, but only until they've received a manual call to the update() function. Here is the paint function:
    [code]void CellGraphic::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget){

    if(cell == nullptr || isBorderCell)
        painter->fillRect(rect(), noConnection);
    else if(!cell->currentState)
        painter->fillRect(rect(), inactive);
    else
        painter->fillRect(rect(), active);
    
    painter->drawRect((rect()));
    

    }[/code]

    When I clear the scene after the object has been drawn with the last 'else' condition, the debugger immediately takes me to a heap breakpoint.

    If I'm in release mode the program works as expected, though I may be seeing a slight memory leak. It's a bit difficult to determine without more extensive testing.

    Anyway, being new at Qt and not very experienced in C++ I guess I'm just wondering what I'm doing wrong here. I was under the impression that the scene would handle the objects once added, and that clear() would delete them properly.

    edit
    Nevermind, just me being an idiot. I fixed a tricky invalid pointer problem elsewhere in the code that must have been corrupting the CellGraphic objects, because clearing the container works flawlessly now.

    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