19
votes

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 is hidden. Until w is shown, there is no layout calculations happening, which is expected.

When I call w->render(painter, w->mapToGlobal(QPoint(0,0)), I get a bunch of controls all overlapping each other.
w->layout()->activate();w->layout()->update() doesn't seem to do anything.

Is there a way to force the layout to happen without showing w?

6

6 Answers

21
votes

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.

12
votes

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();
}
1
votes

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

1
votes

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.

1
votes

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://stackguides.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
votes

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).