当对话框本身调整大小时,我很难让 QDialog 中的小部件自动调整大小。
在下面的程序中,如果您调整主窗口的大小,文本区域会自动调整大小。但是,当调整对话框大小时,对话框中的文本区域保持相同大小。
有没有办法让对话框中的文本区域自动调整大小?我试过在对话框本身和其中的两个小部件上使用 setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
,但这似乎没有效果。
如果相关的话,我在 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()
最佳答案
QMainWindow 具有 QDialog 所没有的中央小部件的特殊行为。要实现所需的行为,您需要创建一个 layout , 将文本区域添加到布局并将布局分配给对话框。
关于python - PyQt:让小部件在 QDialog 中自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/406939/