Python Forum
Attempting to use Qt Designer.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attempting to use Qt Designer.
#11
I think you got an error message on

self.main_widget = QWidget(self.mainWindowGUI)  # constructs a QWidget called "main_widget" and places it on "mainWindowGUI"
Output:
AttributeError: 'mainWindowGUI' object has no attribute 'mainWindowGUI'
mainWindowGUI is the class name, so use self


next error is on

self.tab_widget = QTabWidget(self.mainWidget)
Output:
AttributeError: 'mainWindowGUI' object has no attribute 'mainWidget'
self.mainWidget is wrong you defined it as self.main_widget

should be

self.tab_widget = QTabWidget(self.main_widget) 
But correcting this does not give you Tabs.


Simple Tabs Example

import sys
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, 
                            QTabWidget,QVBoxLayout, QLabel, QTextEdit)

class Tabs(QMainWindow):

    def __init__(self):
        super().__init__()

        self.setGeometry(100, 100, 500, 400)
        self.setWindowTitle('PyQt5 Tabs ')
        
        self.table_widget = TabsWidget(self)
        self.setCentralWidget(self.table_widget)
        
    
class TabsWidget(QWidget):
    
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout()
        
        # Initialize tab screen
        self.tabs = QTabWidget()
        self.tab1 = QTextEdit("Tab 1")
        self.tab2 = QLabel("Tab 2")
        self.tab3 = QLabel("Tab 3")
        
        # Add tabs
        self.tabs.addTab(self.tab1,"Tab 1")
        self.tabs.addTab(self.tab2,"Tab 2")
        self.tabs.addTab(self.tab3,"Tab 3")

        # Add tabs to widget
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Tabs()
    win.show()
    sys.exit(app.exec_())
Reply
#12
Thanks Axel, I'm going to redo my code and follow this template.

I'm wondering whats the point of using Qt Designer if it doesn't generate working code once converted to py? I assume I must not be doing something right but at this point it's better to just type it out myself and learn it quicker.
Reply
#13
Here is what I have written so far. There will be 3 tabs within the tab frame(tab frame may not be the best word to use here but it's the way I'm currently visualizing it in my head). I have only built the first tab so far, the "Hardware" tab. The Hardware tab will have 10 objects. 3 labels, 3 textboxes, 3 buttons and 1 checkbox.

Problem 1. Somehow the main window size is fixed to a certain size and I'm not exactly sure how. Line 14 & 15 I have the size set(I thought) but I can change these numbers and it doesn't change the size of the main window.

Problem 2. Same deal for the "tab frame". Line 34 is where again I thought I could set the size but if I change these numbers it doesn't change the tab frame size within the main window.

Problem 3. The first two problems would need to be fixed first of course but I'm not sure exactly what method I need to use to set the placement of the 10 objects in the "Hardware" tab. Right now when I run it I get the tabs but no objects anywhere.

[Image: tabissues.jpg]

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


class mainWindowGUI(QMainWindow):  # construct/initialize the main window of application
    def __init__(self):
        super().__init__()
        self.title = 'Hardware/Software Manager'
        self.left = 0
        self.top = 0
        self.width = 400
        self.height = 290
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.tab_widget = tabWidget(self)
        self.setCentralWidget(self.tab_widget)

        self.show()


class tabWidget(QWidget):  # Construct/Initialize the tab widget frame
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout(self)
        self.tabFrame = QTabWidget()
        # initialize the individual tabs
        self.hardwareTab = QWidget()
        self.softwareTab = QWidget()
        self.reportingTab = QWidget()
        self.tabFrame.resize(350, 250)
        # add the individual tabs to the tab form and title them
        self.tabFrame.addTab(self.hardwareTab, "Hardware")
        self.tabFrame.addTab(self.softwareTab, "Software")
        self.tabFrame.addTab(self.reportingTab, "Reporting")
        # start building objects in the first tab "Hardware"
        self.hardwareTab.layout = QVBoxLayout(self)
        self.hwSnLabel = QLabel("SERIAL NUMBER")
        self.hwModelLabel = QLabel("MODEL")
        self.hwUserLabel = QLabel("USER")
        self.hwSnTextBox = QPlainTextEdit()
        self.hwModelTextBox = QPlainTextEdit()
        self.hwUserTextBox = QPlainTextEdit()
        self.hwSeButton = QPushButton("SEARCH/EDIT")
        self.hwCfButton = QPushButton("CLEAR FIELDS")
        self.hwEnButton = QPushButton("ENTER NEW HARDWARE")
        self.hwCheckBox = QCheckBox("ACTIVE")
        # add the label objects to "Hardware" tab
        self.hardwareTab.layout.addWidget(self.hwSnLabel)
        self.hardwareTab.layout.addWidget(self.hwModelLabel)
        self.hardwareTab.layout.addWidget(self.hwUserLabel)
        # add the textbox objects to "Hardware" tab
        self.hardwareTab.layout.addWidget(self.hwSnTextBox)
        self.hardwareTab.layout.addWidget(self.hwModelTextBox)
        self.hardwareTab.layout.addWidget(self.hwUserTextBox)
        # add the button objects to "Hardware" tab
        self.hardwareTab.layout.addWidget(self.hwSeButton)
        self.hardwareTab.layout.addWidget(self.hwCfButton)
        self.hardwareTab.layout.addWidget(self.hwEnButton)
        # add the tab frame to main window
        self.layout.addWidget(self.tabFrame)
        self.setLayout(self.layout)

    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 Software Button Clicked")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = mainWindowGUI()
    sys.exit(app.exec_())
Reply
#14
You are doing it backward. Make the tab pages like each is its own application, then add the "pages" to the tab control.
"""Learning how QTabWidget works"""
import sys
import PySide2.QtWidgets as widgets

class MyMainWindow(widgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Hardware/Software Manager')

        self.tabs = widgets.QTabWidget()
        self.tabs.addTab(HardwarePage(), "Hardware")
        self.tabs.addTab(SoftwarePage(), "Software")
        self.tabs.addTab(ReportingPage(), "Reporting")
        self.setCentralWidget(self.tabs)
 
class HardwarePage(widgets.QWidget):
    """Harsare items page"""
    def __init__(self):
        super().__init__()

        self.SnLabel = widgets.QLabel("SERIAL NUMBER")
        self.SnTextBox = widgets.QLineEdit()
        self.ModelLabel = widgets.QLabel("MODEL")
        self.ModelTextBox = widgets.QLineEdit()
        self.UserLabel = widgets.QLabel("USER")
        self.UserTextBox = widgets.QLineEdit()
        self.SeButton = widgets.QPushButton("SEARCH/EDIT")
        self.CfButton = widgets.QPushButton("CLEAR FIELDS")
        self.EnButton = widgets.QPushButton("ENTER NEW HARDWARE")
        self.CheckBox = widgets.QCheckBox("ACTIVE")

        layout = widgets.QVBoxLayout(self)
        layout.addWidget(self.SnLabel)
        layout.addWidget(self.SnTextBox)
        layout.addWidget(self.ModelLabel)
        layout.addWidget(self.ModelTextBox)
        layout.addWidget(self.UserLabel)
        layout.addWidget(self.UserTextBox)
        layout.addWidget(self.SeButton)
        layout.addWidget(self.CfButton)
        layout.addWidget(self.EnButton)
        layout.addWidget(self.CheckBox)

class SoftwarePage(widgets.QWidget):
    """Software items page"""
    def __init__(self):
        super().__init__()

class ReportingPage(widgets.QWidget):
    """No identity page"""
    def __init__(self):
        super().__init__()

if __name__ == "__main__":
    APP = widgets.QApplication(sys.argv)
    WINDOW = MyMainWindow()
    WINDOW.show()
    sys.exit(APP.exec_())
Reply
#15
The reasons your controls don't show up is you didn't put them in the hardwareTab. The error is here:
self.hardwareTab.layout = QVBoxLayout(self)
You create a QVBoxLayout for self (the tabWidget object) and assign this as the layout attribute. "layout" is an attribute of QWidget, and it is a function that returns the layout object for a widget. You trashed that. The function is now unreachable.

This is not how you specify a layout. Either pass in the parent when you create the layout, or create the layout and use setLayout() to specify the parent. Either of these would work.
layout = QVBoxLayout(self.hardwareTab)
# or
self.hardwareTab.setLayout(QVBoxLayout())
And then you could use the layout() function to get the layout object.
self.hardwareTab.layout().addWidget(self.hwSnLabel)
I prefer less typing, so I do this:
layout = QVBoxLayout(self)
layout.addWidget(self.hwSnLabel)
layout.addWidget(self.hwSnTextBox)
Reply
#16
Thanks deanhystad. I noticed you're using pyside instead of pyqt5. I know they're pretty much the same but is there any particular reason you're use pyside instead of pyqt5?
Reply
#17
No. I picked one, you picked the other. That is all there is to that.
Reply
#18
I was just about to ask you why you did this:

import PySide2.QtWidgets as widgets
But looking at the code again the reasoning is that way you can pull whatever widget you need at anytime during coding from the QtWidgets library instead of typing it all out like I have been doing like this:

import sys
from PyQt5.QtWidgets import (QLabel, QPushButton, QPlainTextEdit, QApplication, QCheckBox, QMainWindow, QWidget, QVBoxLayout, QTabWidget)
Is this correct?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Installed Designer - Helpf Files for "assistant" are missing Valmont 0 307 Mar-22-2021, 11:09 AM
Last Post: Valmont
  Using a GUI Designer vs. hard coding 357mag 9 2,194 Feb-21-2021, 06:43 PM
Last Post: kkaur
  [PyGUI] help code python QT Designer yan_mhb 0 454 Aug-12-2020, 09:32 AM
Last Post: yan_mhb
  [Kivy] Kivy Designer Module Error SARAVANAN_M 0 1,886 Nov-20-2019, 09:57 AM
Last Post: SARAVANAN_M
  [PyQt] Send data between windows Pyqt5 and Qt Designer kkonrad002 8 4,364 Sep-05-2019, 02:25 PM
Last Post: Denni
  [PyQt] Qt Designer - Making a Font with a border jimmyvegas29 4 3,403 Feb-19-2019, 11:08 PM
Last Post: jimmyvegas29
  Installing PyQt5 Designer birdieman 6 47,659 Nov-01-2018, 10:51 AM
Last Post: mherrmann
  How to Integrate PyQt4 Custom Widget with Qt Designer Zukias 1 2,355 Aug-29-2018, 05:33 PM
Last Post: Zukias
  [PyQt] enter from py exported by pyqt5 designer to another py file royer14 0 1,240 Jun-29-2018, 01:45 AM
Last Post: royer14
  [PyQt] PyQT5: Open QFiledialog in a Dialog which was create in qt designer nieselfriem 4 15,256 Apr-30-2017, 07:40 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

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