[solved] QGraphicsView in a QTabWidget



  • I've got multiple QGraphicsView items (in a custom class for a plot) on different tabs of a QTabWidget. When resizing, the QGraphicsView runs fitInView. However, this only works when the QGraphicsView is on the currently active tab. When a different tab is active, the view is not adjusted.

    I've noticed that the resize event only occurs when the tab containing the QGraphicsView is activated, but fitInView doesn't work if the resize occurs this way. It only works when I resize the window (such that the QGraphicsView is able to resize) with the QGraphicsView on the active tab.

    Any suggestions as to how I can make this behave?



  • I don't exactly see what's the problem in receiving one resizeEvent instead of "many" (i.e. when the tab containing the QGraphicsView gets activated). Can you elaborate on that?



  • How to best explain this? I have 5 tabs. Tab 1 has a QGraphicsView object, Tab 3 has 2 QGraphicsView objects. I have each of these objects set to use fitInView whenever there is a resizeEvent received by the object.

    @
    void Plot2D::resizeEvent(QResizeEvent *event) {
    Q_UNUSED(event);
    myView->fitInView(myScene->sceneRect(),Qt::KeepAspectRatio);
    }
    @

    Here's an example of the behavior I'm seeing. I start the program on Tab 0. If I resize the window while Tab 0 is active, there are no resizeEvents triggered from any of the plots. If I switch to Tab 1, the plot on Tab 1 receives a resize event, but does not correct the view. If I resize while Tab 1 is active, the view is corrected. The same thing occurs for the plots on Tab 3.

    The desired behavior is for the view to be corrected whenever a resize event happens on the window. I am content with that occurring either immediately on the window resize or when the tab is activated. But it only happens when the tab containing the plot is already active.



  • Ok, I've found a solution. What I do is whenever the tab is changed, I force a resize event at that time to the plot.

    @
    QSize tempSize=tab1->plot->size();
    tab1->plot->resizeEvent(new QResizeEvent(tempSize,tempSize));
    @

    Not exactly how I was hoping to do it, but it solves the problem.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.