当对话框本身调整大小时,我很难让 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()