Python Forum
Attempting to use Qt Designer.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attempting to use Qt Designer.
#1
Hey all I'm trying to make a simple GUI using QT Designer and having some trouble. This is going to be a simple app to keep up with hardware and software inventory at work.

I'm not totally new to programming. Took C++, C# and Java classes back in the early 2000s when .NET was still new so it's definitely been a while.

I'm using PyCharm. I created the GUI in Qt Designer and saved the .ui file in my project directory. I then used my PyQt5 external tool to convert it to .py. It loads the code below and of course it won't run. I did attempt to customize the code myself and managed to get rid of all errors but I think I hacked it up so bad it still wouldn't load.

I'm a little confused since when I watch tutorials of people doing exactly what I did they don't have to change the coding at all. It runs as soon as they convert it from ui to py. Any advice or guidance would be great. I don't mind customizing the code but I feel like I shouldn't have to seeing that other people aren't having to.

[Image: forum3.jpg]

[Image: forum2.jpg]
Reply
#2
Ahhh getting closer...

Constructor

[Image: forum4.jpg]
Reply
#3
Missed importing PyQt5 QtCore. Still trying to figure out the retranslateUi function.


[Image: forum5.jpg]
Reply
#4
Please post code and error message, not a screen image. If your code is large, make a simpler example that has a similar problem. Often I find making the simpler example helps me solve my original problem.
Reply
#5
Totally didn't see the insert python button.

Line 46. Parameter "MainWindow" unfilled.

ex = Ui_MainWindow()

I'm going to look at the code some more tomorrow with fresh eyes. I'm not entirely sure why I need the top if statement. I was thinking I would place "object" as the parameter here but no go. I'll figure it out in the morning.

from PyQt5 import QtWidgets, QtCore
from PySide2.QtCore import *
from PySide2.QtWidgets import *
import sys


class Ui_MainWindow(object):
    def __init__(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
            MainWindow.resize(327, 196)
            MainWindow.setStyleSheet(u"background-color: rgb(255, 255, 255);")
            self.CentralWidget = QWidget(MainWindow)
            self.CentralWidget.setObjectName(u"centralized")
            self.label = QLabel(self.CentralWidget)
            self.label.setObjectName(u'label')
            self.label.setGeometry(QRect(10, 0, 151, 181))
            self.hardware_button = QPushButton(self.CentralWidget)
            self.hardware_button.setObjectName(u"hardware_button")
            self.hardware_button.setGeometry(QRect(190, 10, 121, 51))
            self.software_button = QPushButton(self.CentralWidget)
            self.software_button.setObjectName(u"software_button")
            self.software_button.setGeometry(QRect(190, 70, 121, 51))
            self.reporting_button = QPushButton(self.CentralWidget)
            self.reporting_button.setObjectName(u"reporting_button")
            self.reporting_button.setGeometry(QRect(190, 130, 121, 41))
            MainWindow.setCentralWidget(self.CentralWidget)
            self.statusbar = QStatusBar(MainWindow)
            self.statusbar.setObjectName(u"statusbar")
            MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(QCoreApplication.translate(MainWindow, "Hardware/Software Manager"))
        self.label.setText(QCoreApplication.translate(MainWindow, "Hardware/Software Manager", None))
        self.hardware_button.setText(QCoreApplication.translate(MainWindow, u"Hardware"))
        self.software_button.setText(QCoreApplication.translate(MainWindow, u"Software"))
        self.reporting_button.setText(QCoreApplication.translate(MainWindow, u"Reporting"))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = Ui_MainWindow() 
    w = QtWidgets.QMainWindow() 
    ex.__init__(w)
    w.show() 
    sys.exit(app.exec_())
Reply
#6
from PyQt5 import QtWidgets, QtCore
from PySide2.QtCore import *
from PySide2.QtWidgets import *
Why this?. Do you use PyQt5 or PySide2?
Reply
#7
without designer (but it would be better to use a layout, not setGeometry on Widgets)

from PyQt5.QtWidgets import (QMainWindow, QApplication, 
                              QPushButton, QWidget, 
                              QStatusBar, QLabel)
from PyQt5.QtCore import QRect
import sys
 
 
class Ui_MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__()
        
        self.setStyleSheet("QMainWindow {background-color: #eee;} \
                            QStatusBar {font-size: 8pt; color: #444;}")
        self.label = QLabel("Label", self)
        self.label.setGeometry(QRect(10, 0, 151, 181))
        self.hardware_button = QPushButton("Hardware", self)
        self.hardware_button.clicked.connect(self.hardware_button_clicked)
        self.hardware_button.setGeometry(QRect(190, 10, 121, 51))
        self.software_button = QPushButton("Software", self)
        self.software_button.setGeometry(QRect(190, 70, 121, 51))
        self.reporting_button = QPushButton("Reporting", self)
        self.reporting_button.setGeometry(QRect(190, 130, 121, 51))
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage("Welcome", -1)
        self.resize(327, 196)
        
    def hardware_button_clicked(self):
        print("hardware_button_clicked")

 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Ui_MainWindow() 
    window.show() 
    sys.exit(app.exec_())
Reply
#8
Appreciate it Axel.

I'm going to build off your code and customize it. I had a logo image for the label and when I exported the ui file out of Designer the first time it exported a rsc file with the logo image as well but it's not doing that anymore so I need to figure that out and import the logo image.

I was getting ahead of myself and need to slow down. I understand most of the code but I just need to play around with it and customize to get a better feel.

I'm bad about attempting to fly before even crawling at times. I was thinking I could just come in and make a working crud app within an hour or two with the extremely fuzzy programming knowledge I do have. I was wrong.


(Feb-13-2021, 10:23 AM)Axel_Erfurt Wrote: without designer (but it would be better to use a layout, not setGeometry on Widgets)

from PyQt5.QtWidgets import (QMainWindow, QApplication, 
                              QPushButton, QWidget, 
                              QStatusBar, QLabel)
from PyQt5.QtCore import QRect
import sys
 
 
class Ui_MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__()
        
        self.setStyleSheet("QMainWindow {background-color: #eee;} \
                            QStatusBar {font-size: 8pt; color: #444;}")
        self.label = QLabel("Label", self)
        self.label.setGeometry(QRect(10, 0, 151, 181))
        self.hardware_button = QPushButton("Hardware", self)
        self.hardware_button.clicked.connect(self.hardware_button_clicked)
        self.hardware_button.setGeometry(QRect(190, 10, 121, 51))
        self.software_button = QPushButton("Software", self)
        self.software_button.setGeometry(QRect(190, 70, 121, 51))
        self.reporting_button = QPushButton("Reporting", self)
        self.reporting_button.setGeometry(QRect(190, 130, 121, 51))
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage("Welcome", -1)
        self.resize(327, 196)
        
    def hardware_button_clicked(self):
        print("hardware_button_clicked")

 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Ui_MainWindow() 
    window.show() 
    sys.exit(app.exec_())
Reply
#9
save the logo image in the script folder

replace logo.png with the name of your logo (line 18)

from PyQt5.QtWidgets import (QMainWindow, QApplication, 
                              QPushButton, QWidget, 
                              QStatusBar, QLabel)
from PyQt5.QtCore import QRect
from PyQt5.QtGui import QPixmap
import sys
  
  
class Ui_MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__()
         
        self.setStyleSheet("QMainWindow {background-color: #eee;} \
                            QStatusBar {font-size: 8pt; color: #444;}")
                            
        # label image
        self.label = QLabel(self, scaledContents=True)
        pixmap = QPixmap('logo.png')
        self.label.setPixmap(pixmap)
        
        self.label.setGeometry(QRect(10, 0, 151, 181))
        self.hardware_button = QPushButton("Hardware", self)
        self.hardware_button.clicked.connect(self.hardware_button_clicked)
        self.hardware_button.setGeometry(QRect(190, 10, 121, 51))
        self.software_button = QPushButton("Software", self)
        self.software_button.setGeometry(QRect(190, 70, 121, 51))
        self.reporting_button = QPushButton("Reporting", self)
        self.reporting_button.setGeometry(QRect(190, 130, 121, 51))
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage("Welcome", -1)
        self.resize(327, 196)
         
    def hardware_button_clicked(self):
        print("hardware_button_clicked")
 
  
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Ui_MainWindow() 
    window.show() 
    sys.exit(app.exec_())
Reply
#10
I actually thought it through and decided to use tab widgets to navigate between the hardware and software manager sections.

In order to do that I have to import QWidget and QTabWidget it looks like. If I am understanding this correctly the QTabWidget needs to be placed on top of the QWidget. Then everything else like labels and buttons have to be placed on the QTabWidget. This is what I have so far, am I going in the right direction?

I want to make sure if I understand lines 8-16 correctly. Please refer to notes in code. Thank you!

from PyQt5.QtWidgets import (QMainWindow, QApplication, QPushButton, QWidget, QStatusBar, QLabel, QTabWidget,
                             QCheckBox, QPlainTextEdit)
from PyQt5.QtCore import QRect
import sys


class mainWindowGUI(QMainWindow):
    def __init__(self):
        super(mainWindowGUI, self).__init__()  # constructs the main window of the app and names it "mainWindowGUI"
        self.setStyleSheet("QMainWindow {background-color: #eee;} \
                            QStatusBar {font-size: 8pt; color: #444;}")  # simply sets color and font settings
        self.main_widget = QWidget(self.mainWindowGUI)  # constructs a QWidget called "main_widget" and places it on "mainWindowGUI"
        self.main_widget.setObjectName("mainWidget")  # renames the QWidget "main_widget" to "mainWidget". Don't think this is necessary?
        self.tab_widget = QTabWidget(self.mainWidget)  # constructs a QTabWidget named "tab_widget" and places it on "mainWidget"
        self.tab_widget.setObjectName("tabWidget")  # renames the QTabWidget "tab_widget" to "tabWidget". Again may not be necessary?
        self.tab_widget.setGeometry(QRect(10, 10, 431, 241))  # sets the size of the QTabWidget "tabWidget"
        self.sn_hw_label = QLabel("SERIAL NUMBER", self)  # creates Serial Number Label for Hardware Tab
        self.sn_hw_label.setGeometry(QRect(20, 20, 131, 31))

    def searchEditHWfunction(self):
        print("Search/Edit Hardware Button Clicked")

    def clearFieldsHWfunction(self):
        print("Clear Fields Hardware Button Clicked")

    def enterNewHWfunction(self):
        print("Enter New Hardware Button Clicked")

    def searchEditSWfunction(self):
        print("Search/Edit Software Button Clicked")

    def clearFieldsSWfunction(self):
        print("Clear Fields Software Button Clicked")

    def enterNewSWfunction(self):
        print("Enter New Softwarw Button Clicked")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = mainWindowGUI()
    window.show()
    sys.exit(app.exec_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QT Designer Krissstian 4 1,704 May-26-2022, 09:45 AM
Last Post: Krissstian
  Qt Designer : help document viewer error. davediamond 2 1,587 Apr-14-2022, 10:38 AM
Last Post: davediamond
  GUI programming PyQt 5 + Qt Designer flash77 9 2,727 Mar-19-2022, 10:31 AM
Last Post: flash77
  [PyQt] QT5 Designer Drawing greenhorn1 1 2,554 Dec-30-2021, 05:12 PM
Last Post: deanhystad
  Installed Designer - Helpf Files for "assistant" are missing Valmont 0 2,001 Mar-22-2021, 11:09 AM
Last Post: Valmont
  Using a GUI Designer vs. hard coding 357mag 9 6,056 Feb-21-2021, 06:43 PM
Last Post: kkaur
  [PyGUI] help code python QT Designer yan_mhb 0 1,916 Aug-12-2020, 09:32 AM
Last Post: yan_mhb
  [Kivy] Kivy Designer Module Error SARAVANAN_M 0 3,863 Nov-20-2019, 09:57 AM
Last Post: SARAVANAN_M
  [PyQt] Send data between windows Pyqt5 and Qt Designer kkonrad002 8 10,159 Sep-05-2019, 02:25 PM
Last Post: Denni
  [PyQt] Qt Designer - Making a Font with a border jimmyvegas29 4 7,536 Feb-19-2019, 11:08 PM
Last Post: jimmyvegas29

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020