Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Passing extra arguments to object that is generated by QtDesigner

Passing extra arguments to object that is generated by QtDesigner

Scheduled Pinned Locked Moved Unsolved Qt for Python
qtdesigner
2 Posts 2 Posters 895 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JokZiz1
    wrote on last edited by
    #1

    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:

    custom.py:

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      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);

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved