gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:32:11 25 4
gpt4 key购买 nike

我的结构如下:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com