Reputation: 159
In my QGraphicsView
, I display a map. I need to display a horizontal ruler and a vertical ruler on the top edge and left edge respectfully.
The map needs to scrolled but the above rulers should be displayed at their fixed positions, but change their scale values.
I tried to implement this using drawForeground
method. Due to the maps large size I only paint the visible area. So I need to update()
every time scrolling is done. But this result sometimes flickers.
I feel it would be best to have separate layer like approach.
What is the best way to approach the problem?
Upvotes: 1
Views: 1441
Reputation: 1688
The correct way to implement a ruler on the top and left is to derive from QGraphicsView, and then call in the constructor:
// add two rulers on top and left. setViewportMargins(20, 20, 0, 0); // add grid layout QGridLayout* gridLayout = new QGridLayout(); gridLayout->setSpacing(0); gridLayout->setMargin(0); // create rulers hRuler = new Ruler(Qt::Horizontal); vRuler = new Ruler(Qt::Vertical); // add items to grid layout QWidget* corner = new QWidget(); corner->setBackgroundRole(QPalette::Window); corner->setFixedSize(20, 20); gridLayout->addWidget(corner, 0, 0); gridLayout->addWidget(hRuler, 0, 1); gridLayout->addWidget(vRuler, 1, 0); gridLayout->addWidget(viewport(), 1, 1); // finally set layout setLayout(gridLayout);
This solution was initially presented here, and it works very well. The result looks like this.
Upvotes: 2
Reputation: 159
I just changed ViewportUpdateMode of the graphics view to FullViewportUpdate to get away from flicker.
You can use SmartViewportUpdate for somewhat good results also.
The downside is, during animations, this takes more process power.
Upvotes: 0