QWebView 布局不正确

发布于 2024-09-27 23:41:52 字数 937 浏览 2 评论 0原文

即使我设置了 QWebView 的几何形状,它也会占据整个左侧屏幕。

替代文本 更糟糕的是,如果我最大化窗口,两个小部件之间会有间隙

alt text 下面是我写的代码:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *

class twsearchbox(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        hbx = QHBoxLayout()        
        self.setLayout(hbx)
        self.resize(1024,800)
        self.setWindowTitle('layout test')

        tbx = QTextEdit()
        tbx.setGeometry(0,0, 300, 550)
        tbx.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
        hbx.addWidget(tbx, 0 , Qt.AlignLeft)

        wv = QWebView()
        wv.load(QUrl('twsearch_template.html'))
        wv.setGeometry(0,0,300,550)
        wv.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
        hbx.addWidget(wv, 0 , Qt.AlignLeft)

Even if I set the geometry for the QWebView it occupies the whole left over screen.

alt text
Even worse if I maxmize the window, there is a gap between the two widgets

alt text
Below is my code I wrote:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *

class twsearchbox(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        hbx = QHBoxLayout()        
        self.setLayout(hbx)
        self.resize(1024,800)
        self.setWindowTitle('layout test')

        tbx = QTextEdit()
        tbx.setGeometry(0,0, 300, 550)
        tbx.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
        hbx.addWidget(tbx, 0 , Qt.AlignLeft)

        wv = QWebView()
        wv.load(QUrl('twsearch_template.html'))
        wv.setGeometry(0,0,300,550)
        wv.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
        hbx.addWidget(wv, 0 , Qt.AlignLeft)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半步萧音过轻尘 2024-10-04 23:41:53

我在代码(C++)中对此进行了研究,发现它有点令人费解,直到最后我查看了 QWebView 的源代码。似乎 QWebView::sizeHint() 被硬编码为 800 x 600 (Qt 4.6)。

这对我来说似乎有点奇怪,但我不确定是否可以报告错误。如果有人可以发表评论来确认或否认它的正确性,那就太好了:)

在任何情况下,您应该能够通过根据需要设置最小/最大高度/宽度来覆盖此行为。

另外,为了解释您看到的其他行为,即使 QWebView 是您指定的宽度 (300),您也会看到一个间隙,因为布局试图将水平空间分成相等的大小。由于 QWebView 默认宽度为 800,布局会从左框窃取空间以满足右框所需的大小。如果您有足够宽的屏幕,当您水平拉伸窗口时,您最终会看到两半的大小相等。

I investigated this in code (in c++) and found it a bit puzzling until finally I took a look at the source code for QWebView. It seems that QWebView::sizeHint() is hard coded to 800 x 600 (Qt 4.6).

This seems a bit odd to me but I'm not sure enough to report a bug for it. If somebody can post a comment to confirm or deny the correctness of it, that would be good :)

In any case, you should be able to override this behavior by setting min/max height/width as needed.

Also, to explain the other behavior you see, even if the QWebView was the width you specified (300), you would see a gap as the layout tries to break the horizontal space into equal sizes. With the QWebView defaulting to 800 wide, the layout is stealing space from the left box to satisfy the size needed for the right box. If you had a wide enough screen, you would eventually see both halves equalize in size as you stretched the window horizontally.

葬花如无物 2024-10-04 23:41:53

我认为您可以使用 http://doc.qt.nokia 解决问题。 com/latest/qboxlayout.html#setStretch

并将 QWebView sizepolicy 设置为展开

webView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

I think you can solve the problem by using http://doc.qt.nokia.com/latest/qboxlayout.html#setStretch

and setting QWebView sizepolicy to expanding

webView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文