Skip to content Skip to sidebar Skip to footer

Where Do I Write The Class For A Single Promoted Qwidget From Qt Designer

I read, test and understand a lot of examples for usage of QWidgets from Qt designer, which are promoted to PyQt5. Nevertheless I am not able to deal with a simple example for my o

Solution 1:

Before all your code has an error: Why do you need a class that inherits from QLabel and QPixmap? I don't see the point, a QLabel uses a QPixmap(composition), a QLabel is not a QPixmap(inherency).

For a widget to be promoted it must have the following rules (I did not find them in the docs):

  • The constructor of the widget must have a single parameter and must be the parent.

  • The class must not be in the same file where it is used since it can create a circular import.

  • The widget promotion form asks for 2 data:

    1. Promoted class name which must be the name of the class.
    2. Header file which must be the location of the file where the class is (in C++ by custom rule the name of the file is the same name of the class but with lowercase letters, but in python it does not comply).

    For example if "FooClass" is placed as Promoted class name and "a/b/c" or "a.b.c" is placed in Header file it will be translated to from a.b.c import Foo so verify that this is correct.

Example:

Considering the above, the simplest implementation is to create a .py where the class to be promoted is:

neulabel.py

from PyQt5 import QtCore, QtWidgets


classNeuLabel(QtWidgets.QLabel):
    def__init__(self, parent=None):
        super().__init__(parent)
        self.setAcceptDrops(True)

    defmousePressEvent(self, e):
        super().mousePressEvent(e)
        if e.button() == QtCore.Qt.LeftButton:
            print("press")

Then in the .ui you must fill in the following in the form, press the "Add" button and then the "Promote" button

enter image description here

Generating the following .ui:

<?xml version="1.0" encoding="UTF-8"?><uiversion="4.0"><class>MainWindow</class><widgetclass="QMainWindow"name="MainWindow"><propertyname="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><propertyname="windowTitle"><string>MainWindow</string></property><widgetclass="QWidget"name="centralwidget"><widgetclass="NeuLabel"name="label"><propertyname="geometry"><rect><x>120</x><y>160</y><width>251</width><height>191</height></rect></property><propertyname="text"><string>TextLabel</string></property></widget></widget><widgetclass="QMenuBar"name="menubar"><propertyname="geometry"><rect><x>0</x><y>0</y><width>800</width><height>26</height></rect></property></widget><widgetclass="QStatusBar"name="statusbar"/></widget><customwidgets><customwidget><class>NeuLabel</class><extends>QLabel</extends><header>neulabel</header></customwidget></customwidgets><resources/><connections/></ui>

Then used in the main file:

import os
import sys

from PyQt5 import QtWidgets, uic

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

uifile_1 = os.path.join(CURRENT_DIR, "testpromote.ui")
form_1, base_1 = uic.loadUiType(uifile_1)


classmyApp(base_1, form_1):
    def__init__(self):
        super(base_1, self).__init__()
        self.setupUi(self)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = myApp()
    ex.show()
    sys.exit(app.exec_())
├── main.py
├── neulabel.py
└── testpromote.ui

Post a Comment for "Where Do I Write The Class For A Single Promoted Qwidget From Qt Designer"