代码之家  ›  专栏  ›  技术社区  ›  Luke Woodward

PyQt:让小部件在QDialog中自动调整大小

  •  10
  • Luke Woodward  · 技术社区  · 17 年前

    当对话框本身调整大小时,我很难让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()
    
    4 回复  |  直到 17 年前
        1
  •  4
  •   Judge Maygarden    17 年前

    QMainWindow对中央小部件具有QDialog所没有的特殊行为。要实现所需的行为,您需要创建 layout ,将文本区域添加到布局中,并将布局指定给对话框。

        2
  •  2
  •   Luke Woodward    17 年前

    只是想补充一点——我试图从一个应用程序中生成一个子窗口,这是一个 QDialog ,包含单个 QTextEdit 作为一个孩子/内容-我想要 QTextEdit 每当 QDialog 窗口大小更改。这似乎对我奏效了 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_()
    
        3
  •  2
  •   sdaau    13 年前

    我之前曾考虑过使用QLayout,但运气不佳。我试图做类似的事情

    dialog.setLayout(some_layout)
    

    但我无法让这种方法发挥作用,所以我放弃了。

    我的错误是,当我应该将对话框传递给布局时,我却试图将布局传递给对话框。

    添加线条

    layout = QVBoxLayout(self)
    layout.add(everything)
    

    到最后 TestDialog.__init__ 修复问题。

    感谢Monjardin促使我重新考虑布局。

        4
  •  1
  •   hollahhoo    15 年前

    查看 Python QT Automatic Widget Resizer 它应该工作得很好。