如何使QLayouts正确扩展? [英] How to get the QLayouts to expand properly?

查看:104
本文介绍了如何使QLayouts正确扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的结构如下:

QWidget
    -QHBoxLayout
         -QLabel
         -QVBoxLayout
              -QLabel
              -QWebView

无论容器有多大,我都希望HBoxLayout填充宽度,但不要多多少少.但是,我希望QVBoxLayout扩展以适应垂直方向上其内容的大小.

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                    +
+-------------+------------------------------+

在此示例中,FixedTitle的宽度需要变大,但是永远不会调整大小.扩展到宽度标题"将填充剩余的水平空间.

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.

到目前为止,我有:


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的高度.

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.

我该如何解决?

推荐答案

这是我的回答...并原谅我,但它是用PyQt编写的.

This is my take on an answer... and forgive me but its written in PyQt.

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

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.

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视图可以按其期望的大小增长,但是可以使用滚动条来处理溢出.

    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.

    这篇关于如何使QLayouts正确扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆