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. Qt 5: QGraphicsItem update() mechanism
Qt 6.11 is out! See what's new in the release blog

Qt 5: QGraphicsItem update() mechanism

Scheduled Pinned Locked Moved General and Desktop
qgraphicsitem u
1 Posts 1 Posters 919 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    (Qt 5.4 on Win system)

    Hello Qt'ies!

    I have a QGraphicsScene, which can be zoomed and panned. In the picture attached there's a gray line which is a zoomed-in view of arc (ArcGraphItem is a QGraphicsItem-derived class).
    The two yellow lines beside are helper lines painted to demonstrate the clicking/picking area of the arc (implementation in shape() method).

    Example screenshot

    void 
    ArcGraphItem::paint(
    		QPainter						* painter,
    		const QStyleOptionGraphicsItem	* option,
    		QWidget							* widget) {
    		
    	
    	qDebug() << "repaint";
    
    	ArcGraphItem::scaleFactor = painter->transform().m11();
    
    	painter->save();
    	if (this->hovered) {
    		painter->setPen(AbstractGraphItem::hoverPen);
    	} else if (this->selected) {
    		painter->setPen(AbstractGraphItem::selectPen);
    	} else {
    		painter->setPen(AbstractGraphItem::unselectPen);
    	}
    
    	/* draw arc by start/end point incl. re-connection to end point because of glitchy 1/16 integer angles */
    	QPainterPath path(this->startPoint);
    	path.arcTo(this->rectangle, this->startAngle, this->spanAngle);
    	path.lineTo(this->endPoint);
    	painter->drawPath(path);
    	
    
      #if SHOW_PICK_AREA == 1
        /* yellow outline of the picking area */
    	painter->setPen(AbstractGraphItem::pickAreaPen);
    	painter->drawRect(this->boundingBox);
    	painter->drawPath(this->shape());
      #endif
    
    	painter->restore();
    }
    

    When clicking the arc, I call a selection method, which calls update() method of the AbstractGraphItem:

    void
    ArcGraphItem::setSelected(
    		bool							selected) {
    
    	if (this->selected != selected) {
    		this->selected = selected;
    
    		qDebug() << (selected ? "" : "not") << "select";
    		this->update();
    	}
    }
    

    The debug output clearly show that setSelected() is called and toggles the selection status, which is interpreted in paint() method. Unfortunately, the update() does not trigger a call to paint(), if only this single AbstractGraphItem is visible at the moment. If I a add another QGraphicsItem to the scene ("small point"), then the paint() method of the clicked arc is triggered.

    According to a thread on StackOverflow [http://stackoverflow.com/questions/22900106/qt-update-doesnt-work#] I also have to call the update method of the scene. Do I have to?

    Thank you for reading,
    Riff Raff

    Btw: shape() and boundingRect() methods are impl'd for ArcGraphItem.

    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