c++ - 如何让 QLayouts 正确展开?

标签 c++ qt layout widget qwebview

我的结构如下:

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

这有两个问题:1. 它忽略了容器的宽度,2. 它仍然没有得到正确的 QWebView 高度。

我该如何解决这个问题?

最佳答案

这是我的回答……原谅我,但它是用 PyQt 编写的。

我觉得您不应该过多考虑将包含小部件的大小调整为 QWebView 的内容,而只是将大小策略设置为扩展,关闭滚动条,并将大小推迟到此容器的任何布局被添加到。尝试手动调整它的大小是没有意义的。

from PyQt4 import QtCore, QtGui, QtWebKit

class WebWidget(QtGui.QWidget):

    def __init__(self):
        super(WebWidget, self).__init__()

        layout = QtGui.QHBoxLayout(self)

        title = QtGui.QLabel("FixedTitle:")
        # title.setText("A much larger fixed title")

        title.setSizePolicy(
            QtGui.QSizePolicy.Preferred, 
            QtGui.QSizePolicy.Fixed)

        layout.addWidget(title, 0, QtCore.Qt.AlignTop)

        v_layout = QtGui.QVBoxLayout()
        layout.addLayout(v_layout)

        expandingTitle = QtGui.QLabel("Expanding to Width Title")
        expandingTitle.setSizePolicy(
            QtGui.QSizePolicy.Expanding, 
            QtGui.QSizePolicy.Fixed)

        v_layout.addWidget(expandingTitle)

        text = QtGui.QTextEdit()

        view = QtWebKit.QWebView()
        view.setSizePolicy(
            QtGui.QSizePolicy.Expanding, 
            QtGui.QSizePolicy.Expanding)

        view.page().mainFrame().setScrollBarPolicy(
            QtCore.Qt.Vertical, 
            QtCore.Qt.ScrollBarAlwaysOff )

        view.page().mainFrame().setScrollBarPolicy(
            QtCore.Qt.Horizontal, 
            QtCore.Qt.ScrollBarAlwaysOff )

        v_layout.addWidget(view, 1)

        text.setPlainText("""
            this is a test which wraps to the next line\n\n\n
            bla bla bla\n\n\nthere are no vertical scroll bars here
            """)
        view.setHtml(text.toHtml())

        v_layout.addStretch()

        self.view = view
        view.page().mainFrame().contentsSizeChanged.connect(self.updateWebSize)

    def updateWebSize(self, size=None):
        if size is None:
            size = self.view.page().mainFrame().contentsSize()
        self.view.setFixedSize(size)

    def resizeEvent(self, event):
        super(WebWidget, self).resizeEvent(event)
        self.updateWebSize()


if __name__ == "__main__":

    app = QtGui.QApplication([])

    w = QtGui.QScrollArea()
    w.resize(800,600)

    web = WebWidget()
    w.setWidget(web)
    w.setWidgetResizable(True)

    w.show()

    app.exec_()
  • 左侧标题设置为首选宽度和固定高度,以便它获得所需的宽度但不会垂直增长。
  • 展开式标题采用展开式宽度策略和固定高度。
  • QWebView 获得双向扩展策略,并且它的滚动条被关闭。

举个例子。我刚刚创建了一个 QScrollArea 并将 WebWidget 设置到其中,这样您就可以看到父布局将允许 Web View 增长到它想要的大小,但将处理滚动条溢出。

关于c++ - 如何让 QLayouts 正确展开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12221345/

相关文章:

c++ - "std::decay_t<U> (until C++20) std::remove_cvref_t<U> (since C++20)"是什么意思?

c++ - 如何在特定偏移量处存储变量

c++ - 关于 QCoreApplication 和 QProcess

c++ - Qt connect() 用法

Android更改默认按钮背景颜色

c++ - 使用 cmake、clang 和 llvm 为 visual studio 构建工具链

c++ - Windows 函数 "NetUserChangePassword"不再在 Windows 10 下工作(在 Windows 7 下工作)

C++/Qt : Just a LNK2019 and I don't know why

python - 即使使用 setSpacing(0),小部件之间也有空白空间

flutter - 为什么我的 flutter 应用程序中的 FAB 大小很大?