pyqt : qdialog에서 자동으로 크기를 조정할 위젯 얻기
문제
대화 상자 자체가 크기를 조정할 때 QDialog 크기로 조정하는 위젯을 얻는 데 어려움이 있습니다.
다음 프로그램에서는 기본 창을 크기를 조정하면 Textarea가 자동으로 크기를 조정합니다. 그러나 대화 상자 내에서 대화 상자가 크기를 조정할 때 동일한 크기가 유지됩니다.
대화 상자에서 TextRea를 자동으로 만드는 방법이 있습니까? 사용해 보았습니다 setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
대화 자체와 두 개의 위젯에서는 영향을 미치지 않는 것 같습니다.
관련이 있다면 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가하지 않는 중앙 위젯에 대한 특별한 행동을 가지고 있습니다. 원하는 행동을 달성하려면 형세, 텍스트 영역을 레이아웃에 추가하고 대화 상자에 레이아웃을 할당하십시오.
다른 팁
나는 전에 Qlayout을 사용하는 것을 보았지만 운이 없었습니다. 나는 같은 일을하려고했다
dialog.setLayout(some_layout)
그러나 나는 그 접근 방식을 일할 수 없어서 포기했다.
내 실수는 대화 상자를 레이아웃에 전달했을 때 대화 상자에 레이아웃을 전달하려고하는 것이 었습니다.
라인 추가
layout = QVBoxLayout(self)
layout.add(everything)
끝까지 TestDialog.__init__
문제를 해결합니다.
레이아웃을 재고하도록 저지른 Monjardin에게 감사드립니다.
이것에 대해 약간의 메모를 추가하기 위해 - 나는 신청서에서 아이 창을 낳으려고 노력했다. 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_()
체크 아웃 파이썬 QT 자동 위젯 리저 저기 잘 작동한다고 가정합니다.