Qt: How to force a hidden widget to calculate its layout?

前端 未结 6 717
北海茫月
北海茫月 2020-12-09 17:01

What I am trying to do is render a qwidget onto a different window (manually using a QPainter)

I have a QWidget (w) with a layout and a bunch of child controls. w i

相关标签:
6条回答
  • 2020-12-09 17:36

    Try with the QWidget::sizeHint() method, which is supposed to return the size of the widget once laid out.

    0 讨论(0)
  • 2020-12-09 17:36

    When going through QWidget::grab() I noticed this part:

    if (r.width() < 0 || r.height() < 0) {
        // For grabbing widgets that haven't been shown yet,
        // we trigger the layouting mechanism to determine the widget's size.
        r = d->prepareToRender(QRegion(), renderFlags).boundingRect();
        r.setTopLeft(rectangle.topLeft());
    }
    

    QWidget::grab() has been introduced in Qt 5.0, and this test function with a QDialog containing a layout seems to work on Qt 5.5.1

    int layoutTest_2(QApplication& a)
    {
        CLayoutTestDlg dlg; // initial dlg size can also be set in the constructor
            // https://stackoverflow.com/questions/21635427
    
        QPixmap pixmap = dlg.grab(); // must be called with default/negative-size QRect
        bool savedOK = pixmap.save("E:/temp/dlg_img.png");
            // saving is not necessary, but by now the layout should be done
    
        dlg.show();
        return a.exec();
    }
    
    0 讨论(0)
  • 2020-12-09 17:40

    This worked for me when using the sizeHint() plus translating the painter, however I do this inside the paint() method.

    void ParentWidget::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
      painter->save();
      painter->translate(option.rect.topLeft());
      w->render(painter);
      painter->restore();
    }
    

    In this case, option.rect.topLeft() gives me the correct placement. You should try a more sensible coordinate instead of w->mapToGlobal(QPoint(0,0).

    0 讨论(0)
  • 2020-12-09 17:45

    The layout calculation of a widget can be forced by calling invalidate() followed by activate() on its layout, even if the widget is hidden. This also causes the widget's size() and sizeHint() functions to return correct and updated values, even if show() has not yet been called on that widget.

    It is however necessary to care about all child widgets and layouts recursively, as a layout recalculation request doesn't automatically propagate to the childs.

    The following code shows how to do this.

    /**
     * Forces the given widget to update, even if it's hidden.
     */
    void forceUpdate(QWidget *widget) {
        // Update all child widgets.
        for (int i = 0; i < widget->children().size(); i++) {
            QObject *child = widget->children()[i];
            if (child->isWidgetType()) {
                forceUpdate((QWidget *)child);
            }
        }
    
        // Invalidate the layout of the widget.
        if (widget->layout()) {
            invalidateLayout(widget->layout());
        }
    }
    
    /**
     * Helper function for forceUpdate(). Not self-sufficient!
     */
    void invalidateLayout(QLayout *layout) {
        // Recompute the given layout and all its child layouts.
        for (int i = 0; i < layout->count(); i++) {
            QLayoutItem *item = layout->itemAt(i);
            if (item->layout()) {
                invalidateLayout(item->layout());
            } else {
                item->invalidate();
            }
        }
        layout->invalidate();
        layout->activate();
    }
    
    0 讨论(0)
  • 2020-12-09 17:52

    I had some succes in a similar problem by first calling w->layout()->update() before w->layout()->activate(). That seems to force the activate() to actually do something rather than think it is fine because the window isn't being shown anyway.

    0 讨论(0)
  • 2020-12-09 17:56

    Forcing a layout calculation on a widget without showing it on the screen:

    widget->setAttribute(Qt::WA_DontShowOnScreen);
    widget->show();
    

    The show() call will force the layout calculation, and Qt::WA_DontShowOnScreen ensures that the widget is not explicitly shown.

    0 讨论(0)
提交回复
热议问题