[英]PyQt: getting widgets to resize automatically in a QDialog
I'm having difficulty getting widgets in a QDialog resized automatically when the dialog itself is resized. 调整对话框本身的大小时,我很难自动调整QDialog中的小部件的大小。
In the following program, the textarea resizes automatically if you resize the main window. 在以下程序中,如果您调整主窗口的大小,则文本区域会自动调整大小。 However, the textarea within the dialog stays the same size when the dialog is resized.
但是,调整对话框大小时,对话框中的文本区域保持不变。
Is there any way of making the textarea in the dialog resize automatically? 有什么方法可以使对话框中的文本区域自动调整大小? I've tried using
setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
on the dialog itself and the two widgets within, but that seems to have no effect. 我尝试在对话框本身和其中的两个小部件上使用
setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
,但这似乎没有效果。
I'm using Qt version 3.3.7 and PyQt version 3.5.5-29 on openSuSE 10.2, if that's relevant. 如果相关,我在openSuSE 10.2上使用Qt版本3.3.7和PyQt版本3.5.5-29。
import sys
from qt import *
# The numbers 1 to 1000 as a string.
NUMBERS = ("%d " * 1000) % (tuple(range(1,1001)))
# Add a textarea containing the numbers 1 to 1000 to the given
# QWidget.
def addTextArea(parent, size):
textbox = QTextEdit(parent)
textbox.setReadOnly(True)
textbox.setMinimumSize(QSize(size, size*0.75))
textbox.setText(NUMBERS)
class TestDialog(QDialog):
def __init__(self,parent=None):
QDialog.__init__(self,parent)
self.setCaption("Dialog")
everything = QVBox(self)
addTextArea(everything, 400)
everything.resize(everything.sizeHint())
class TestMainWindow(QMainWindow):
def __init__(self,parent=None):
QMainWindow.__init__(self,parent)
self.setCaption("Main Window")
everything = QVBox(self)
addTextArea(everything, 800)
button = QPushButton("Open dialog", everything)
self.connect(button, SIGNAL('clicked()'), self.openDialog)
self.setCentralWidget(everything)
self.resize(self.sizeHint())
self.dialog = TestDialog(self)
def openDialog(self):
self.dialog.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainwin = TestMainWindow(None)
app.setMainWidget(mainwin)
mainwin.show()
app.exec_loop()
Just to add a little note about this - I was trying to have a child window spawned from an application, which is a QDialog
, containing a single QTextEdit
as a child/content - and I wanted the QTextEdit
to resize automatically whenever the QDialog
window size changes. 只是要对此添加一点说明-我试图从应用程序中生成一个子窗口,该应用程序是一个
QDialog
,其中包含单个QTextEdit
作为子项/内容-并且我希望QTextEdit
在QDialog
窗口大小每次自动调整大小变化。 This seems to have done the trick for me with PyQt4
: 这似乎已经为我用
PyQt4
做到了:
def showTextWindow(self):
#QVBox, QHBox # don't exist in Qt4
dialog = QDialog(self)
#dialog.setGeometry(QRect(100, 100, 400, 200))
dialog.setWindowTitle("Title")
dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
textbox = QTextEdit(dialog)
textbox.setReadOnly(True)
textbox.setMinimumSize(QSize(400, 400*0.75))
textbox.setText("AHAAA!")
# this seems enough to have the QTextEdit
# autoresize to window size changes of dialog!
layout = QHBoxLayout(dialog)
layout.addWidget(textbox)
dialog.setLayout(layout)
dialog.exec_()
I had looked at using a QLayout before but had no luck. 我之前曾经看过使用QLayout,但是没有运气。 I was trying to do something like
我正在尝试做类似的事情
dialog.setLayout(some_layout)
but I couldn't get that approach to work so I gave up. 但是我无法采用这种方法,所以我放弃了。
My mistake was that I was trying to pass the layout to the dialog when I should have been passing the dialog to the layout. 我的错误是我本该将对话框传递给布局时尝试将布局传递给对话框。
Adding the lines 添加行
layout = QVBoxLayout(self)
layout.add(everything)
to the end of TestDialog.__init__
fixes the problem. 到
TestDialog.__init__
的末尾TestDialog.__init__
解决了该问题。
Thanks to Monjardin for prompting me to reconsider layouts. 感谢Monjardin提示我重新考虑布局。
查看Python QT Automatic Widget Resizer可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.