I’m trying to add the PowerBar
widget created in the “Custom Widgets” tutorial in a window made in Qt Creator, but it is not appearing.
I made a file named “power_bar.py” with the exact content from the tutorial page, and tested it following the tutorial.
Then, I created a new MainWindow
form in Qt Creator, following the “Embedding custom widgets from Qt Designer” tutorial, put there a widget, and promoted it to PowerBar
from include file power_bar
. The ui content can be seen here: <?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>MainWindow</ - Pastebin.com
I then compiled the ui to a file named main_window.py
, which can be seen here:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(364, 634)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.powerbarWidget = PowerBar(5)
self.powerbarWidget.setGeometry(QtCore.QRect(30, 40, 291, 541))
self.powerbarWidget.setObjectName("powerbarWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
from power_bar import PowerBar
I had to edit this file, in the line where it instantiates the PowerBar
, I put a number there because by default it instantiates passing self.centralWidget
as the parameter.
Then I made a file to show the MainWindow
, the content can be seen here:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qt import *
import sys
from main_window import Ui_MainWindow
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
When I run the code, it shows a window, but it doesn’t show the widget.
If I put a line after the setupUi like:
self.ui.powerbarWidget.show()
it shows the widget, but it creates another window.
How can I use this widget in a window, like did in the pyqtgraph example?