My structure is as follows:
QWidget -QHBoxLayout -QLabel -QVBoxLayout -QLabel -QWebView
I want the HBoxLayout to fill the width however large the container may be but go no more or less. However, I want the QVBoxLayout to expand to accommodate the size of its contents in the vertical direction.
+-------------+------------------------------+ | FixedTitle: | Expanding to Width Title + | |------------------------------+ | | + | | this is a test which wraps to+ | | the next line + | | + | | + | | + | | bla bla bla + | | + | | + | | + | | there are no vertical scroll + | | bars here + +-------------+------------------------------+
In this example, FixedTitle's width is however big it needs to be, but does not resize ever. Expanding to Width Title fills up the remaining horizontal space.
So far, I have:
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QHBoxLayout *layout = new QHBoxLayout;
this->setLayout(layout);
layout->addWidget(new QLabel(QString("FixedTitle")), 0, Qt::AlignTop);
QVBoxLayout *v_layout = new QVBoxLayout;
v_layout->setSizeConstraint(QLayout::SetNoConstraint);
layout->addLayout(v_layout);
v_layout ->addWidget(new QLabel(QString("Expanding to Width Title")), 1, Qt::AlignTop | Qt::AlignLeft);
QWebView *view = new QWebView();
QTextEdit text;
text.setPlainText(QSString("\nthis is a test which wraps to the next line\n\n\nbla bla bla\n\n\nthere are no vertical scroll bars here"));
view->setHtml(text.toHtml());
int width = view->page()->mainFrame()->contentsSize().width();
int height = view->page()->mainFrame()->contentsSize().height();
view->page()->setViewportSize(QSize(width, height));
view->resize(width, height);
view->setFixedSize(width, height);
v_layout->addWidget(view);
There are two problems with this: 1. It ignores the width of the container and 2. It still doesnt get the height of the QWebView correct.
How do I fix this?
This is my take on an answer... and forgive me but its written in PyQt.
I feel that your shouldn't be thinking so much about resizing the containing widget to the contents of the QWebView, but rather just have the size policy set to expanding, turn off the scrollbars, and defer sizing to whatever layout this container is added to. It makes no sense to try and manually resize it.
And for an example. I just created a QScrollArea and set the WebWidget into it, so that you can see the parent layout will allow the Web view to grow as big as it wants to, but will handle overflow with scrollbars.