Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promote PyQt Widget

Tags:

python

pyqt

I'm using PyQt and trying to promote a widget in QtDesigner. I'm able to get it working if I specify the full module path, in the "Header file" field, to the file that contains my widget sub-class.

Is there a better way to promote a widget in QtDesigner to a PyQt widget without having to specify the full module path?

Here's an example to hopefully illustrate what I'm talking about:

/PythonModuleRoot/Ui/MainUi.py

from PyQt4 import QtCore, QtGui, uic
class MainUi(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.ui = uic.loadUi(os.path.join(os.path.dirname(__file__), 'MainUi.ui'), self)

/PythonModuleRoot/Ui/CustomWidget.py

from PyQt4 import QtCore, QtGui, uic
class CustomWidget(QtGui.QWidget):
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)

/PythonModuleRoot/Ui/MainUi.ui

In MainUi.ui I promote a widget and set the Header file field to: "PythonModuleRoot.Ui.CustomWidget".

like image 456
keegan3d Avatar asked Jan 28 '11 20:01

keegan3d


People also ask

What is stacked widget in PyQt5?

QStackedWidget can be used to create a user interface similar to the one provided by QTabWidget . It is a convenience layout widget built on top of the QStackedLayout class. Like QStackedLayout , QStackedWidget can be constructed and populated with a number of child widgets (“pages”):

Can I use Qt Designer with PySide2?

PySide2 Tutorial — Creating applications with Qt Designer The good news is that Qt comes with a graphical editor — Qt Designer — which contains a drag-and-drop UI editor. Using Qt Designer you can define your UIs visually and then simply hook up the application logic later.

What is QVBoxLayout?

The QVBoxLayout class lines up widgets vertically. This class is used to construct vertical box layout objects. See QBoxLayout for details.


1 Answers

I got it figured out, my actual code is slightly different then the simplified example I gave. My actual code is more like this:

/PythonModuleRoot/Ui/MainUi.py

/PythonModuleRoot/Ui/MainUi.ui

/PythonModuleRoot/Ui/Widgets/CustomWidget.py

So I just needed to change the contents of Header file to be how MainUi would import CustomWidget, so: "Widgets.CustomWidget". This article pointed me in the right direction: http://www.mail-archive.com/[email protected]/msg17893.html

like image 82
keegan3d Avatar answered Sep 20 '22 13:09

keegan3d