Proplema with layout: Widget does not resize with the window

0

I have a Dialog with a unique Wigdet that occupies the entire window. In this case it is a QTextEdit and is created with Qt Designer .

This is the file in Python :

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.gridLayout = QtGui.QGridLayout(Dialog)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.textEdit = QtGui.QTextEdit(Dialog)
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))

However, when executing the code that you throw from Dialog the Widget is not resized.

import sys

from qtdesigner.dialog_reacciones_rsa import *

class Window(QtGui.QMainWindow):

    def __init__(self, parent=None):

        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        self.ui.textEdit.setText('Hola\nAdios')


if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    myapp = Window()
    myapp.show()
    sys.exit(app.exec_())

Edited on 06.21.2018

I modify the code of Dialog according to FJSevilla's comment:

...
class Window(QtGui.QDialog):

        def __init__(self, parent=None):

            QtGui.QDialog.__init__(self, parent)
...

    
asked by Pedro Biel 20.06.2018 в 14:33
source

1 answer

0

If you need to use QMainWindow

Sorry, I use PyQt5:

import sys
from PyQt5.QtGui     import *
from PyQt5.QtCore    import *
from PyQt5.QtWidgets import *

#from qtdesigner.dialog_reacciones_rsa import *
# Tengo 'dialog_reacciones_rsa.py' en el directorio actual
from dialog_reacciones_rsa import Ui_Dialog

class Window(QMainWindow, Ui_Dialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setupUi(self)   

        self.central_widget = QWidget()                 # definir el widget central
        self.setCentralWidget(self.central_widget)      # instalar QMainWindow.centralWidget
        self.centralWidget().setLayout(self.gridLayout) # agregar el diseño al widget central        

        self.textEdit.setHtml(""" 
                Hola<br><b>Adios</b><br><br>

                <p style="color: red; font: 15px;">Atención: por favor mira 'dialog_reacciones_rsa.py':</p>
                <span style="color: blue; font: 12px;">
                <br>...<br>
                #self.gridLayout = QtWidgets.QGridLayout(Dialog)<br>
                self.gridLayout = QtWidgets.QGridLayout()<br>
                ...</span>
                """)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    myapp = Window()
    myapp.show()
    sys.exit(app.exec_())

dialog_reacciones_rsa.py

#from PyQt4 import QtCore, QtGui
from PyQt5 import QtCore, QtGui, QtWidgets
#                                ^^^^^^^^^

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtWidgets.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtWidgets.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtWidgets.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)

        # !!!!!!!!!!!!!!                         vvvvvv
        #self.gridLayout = QtWidgets.QGridLayout(Dialog)
        self.gridLayout = QtWidgets.QGridLayout()     


        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.textEdit = QtWidgets.QTextEdit(Dialog)
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))

    
answered by 21.06.2018 в 14:07