QWebView 布局不正确
即使我设置了 QWebView 的几何形状,它也会占据整个左侧屏幕。
更糟糕的是,如果我最大化窗口,两个小部件之间会有间隙
下面是我写的代码:
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.
Even worse if I maxmize the window, there is a gap between the two widgets
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在代码(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 thatQWebView::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 theQWebView
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.我认为您可以使用 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);