Passing extra arguments to object that is generated by QtDesigner
-
I have created custom class that inherits from QListWidget. I have promoted widget to my custom class through QtDesigner. Problem is, that I need to pass additional argument to that Widget, which I am able to do manually, but every time I rebuild my GUI, it will obviously get changed. I provide example below if I am not clear enough:
class LoggerList(QListWidget): def __init__(self, extra_argument, parent = None) -> None: super(LoggerList, self).__init__(parent) self.argument= extra_argument
I have GUI built on QtDesigner and this is what is generated:
main_window_ui.py
class Ui_MainWindow(object): def setupUi(self, MainWindow): if not MainWindow.objectName(): MainWindow.setObjectName(u"MainWindow") MainWindow.resize(1583, 883) font = QFont() font.setFamily(u"Segoe UI") MainWindow.setFont(font) self.centralwidget = QWidget(MainWindow) self.centralwidget.setObjectName(u"centralwidget") self.main_display = QLabel(self.centralwidget) self.main_display.setObjectName(u"main_display") self.main_display.setGeometry(QRect(10, 100, 581, 581)) self.main_display.setFocusPolicy(Qt.NoFocus) self.main_display.setStyleSheet(u"background-color: rgb(160, 160, 160);") self.main_display.setAlignment(Qt.AlignCenter) self.main_display.setIndent(0) self.listWidget = LoggerList(self.groupBox_4) self.listWidget.setObjectName(u"listWidget") self.listWidget.setGeometry(QRect(10, 20, 1021, 111))
What I want is (3rd last row):
self.listWidget = LoggerList(my_extra_argument, self.groupBox_4)
If I manually change main_window_ui.py file by adding the argument, everything works fine, however, whenever I generate main_window_ui.py file over again, my_extra_argument is lost, all I can do is add it over again manually every time i regenrate my UI file. Is there any way to let Designer know, that I want to pass another argument to my QListWidget?
-
Hi,
It's possible with dynamic properties, but as you want to pass an object, it's a little bit tricky:
In the designer, you create a property named "argument" ( + sign on the right panel) and set groupBox_4 as value for this property.
Then by code you can retreave that object:
QString name=ui->listWidget->property("argument").toString(); QGroupBox* box=findChild<QGroupBox*>(name); if(box) { // do what you want width the groupbox }
From inside LoggerList, you have to start the search from the window :
QGroupBox* box=window()->findChild<QGroupBox*>(name);