0

I am new to Qt Designer and PyCharm and when I want to make a small program for practice, I have encountered a layout problem.

I use Qt Designer to design the layout and use PyUIC as an external tool to convert my login.ui into login.py.

I use Form Layout as my layout and put two pairs of labels and lineEdits.

Before running my program, I press the preview button to check my layout first, every thing looks as expected:
Preview in Qt Designer

But when I run my program in PyCharm, it looks weird and I don't know why:
Run in PyCharm

I have two .py files: login.py and mainn.py

login.py:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(473, 436)
        self.formLayout = QtWidgets.QFormLayout(Form)
        self.formLayout.setObjectName("formLayout")
        self.label = QtWidgets.QLabel(Form)
        self.label.setObjectName("label")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, 
        self.label)
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setObjectName("lineEdit")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, 
        self.lineEdit)
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, 
        self.label_2)
        self.lineEdit_2 = QtWidgets.QLineEdit(Form)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, 
        self.lineEdit_2)

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "Label 1"))
        self.label_2.setText(_translate("Form", "Label 2"))

mainn.py:

import sys
import login
from PyQt5.QtWidgets import QApplication, QMainWindow

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = login.Ui_Form()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

By the my I am running on a High-DPI laptop, I'm not sure if this is one of the reason. Any ideas? Thanks!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Tony Chen
  • 207
  • 2
  • 13
  • According to the template you use Qt Designer you must use the appropriate widget, in your case the first image the top widget is a Widget so you must use QWidget then change QMainWindow to QWidget – eyllanesc Dec 04 '18 at 19:08
  • Thanks! That's the problem. – Tony Chen Dec 04 '18 at 23:39

1 Answers1

0

If you run mainn.py viaCMD You would see an error message:

QLayout: Attempting to add QLayout" "to QMainWindow" Form ", which already has a layout

Try it:

import sys
import login
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget

if __name__ == '__main__':
    app = QApplication(sys.argv)
#    MainWindow = QMainWindow()         # ---
    MainWindow = QWidget()              # +++
    ui = login.Ui_Form()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
S. Nick
  • 12,879
  • 8
  • 25
  • 33