2

I created a button in the QT6 creator and named it scanButton. The button shows up when I run the code. However, I'm running into an issue once I want to bind it to a function. I finally got a result but it shows a new button in the top left and it runs self.close, which is not supposed to be the case.

Image of code + App

.ui code:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Widget</class>
     <widget class="QWidget" name="Widget">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Widget</string>
      </property>
      <widget class="QPushButton" name="scanButton">
       <property name="geometry">
        <rect>
         <x>280</x>
         <y>210</y>
         <width>181</width>
         <height>141</height>
        </rect>
       </property>
       <property name="text">
        <string>Scan</string>
       </property>
      </widget>
     </widget>
     <resources/>
     <connections/>
    </ui>
   

.py code:

    import os
    from pathlib import Path
    import sys
    
    from PySide6.QtWidgets import QApplication, QWidget, QPushButton
    from PySide6.QtCore import QFile
    from PySide6.QtUiTools import QUiLoader
    
    
    class Widget(QWidget):
        def __init__(self):
            super(Widget, self).__init__()
            self.load_ui()
    
        def load_ui(self):
            loader = QUiLoader()
            path = os.fspath(Path(__file__).resolve().parent / "form.ui")
            ui_file = QFile(path)
            ui_file.open(QFile.ReadOnly)
            loader.load(ui_file, self)
            ui_file.close()
    
            scanButton = QPushButton(self)
            scanButton.clicked.connect(self.close)
    
    
    if __name__ == "__main__":
        app = QApplication([])
        widget = Widget()
        widget.show()
        sys.exit(app.exec_())
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
toad
  • 23
  • 5
  • Welcome back to Stack Overflow. "I finally got a result but it shows a new button in the top left" yes, because `scanButton = QPushButton(self)` creates a new button which has nothing to do with the one from the designer. "and it runs self.close" I don't understand what you mean by this. What is "it"? When does the "running" happen? How do you know it is "running self.close"? What should happen instead? – Karl Knechtel May 29 '22 at 03:13
  • The button in the middle should run self.close – toad May 29 '22 at 09:40

1 Answers1

4

First of all you need to load your ui file as you did:

self.ui = loader.load('form.ui', None)
self.ui.show()

Then you can call your button with the name like this:

self.ui.scanButton.clicked.connect(self.check)

Note that when you press the button it will go to check function.

Full code:

class main(QWindow):
    def __init__(self):
        super().__init__()
        loader = QUiLoader()
        self.ui = loader.load('form.ui', None)
        self.ui.show()

        self.ui.scanButton.clicked.connect(self.check)
    
    def check(self):
        print('You pressed the button!')
  • Why are you inheriting from QWindow? Considering that you're not using any of its feature and the *actual* window is shown as a separate object, there's no point in using that inheritance. – musicamante May 29 '22 at 07:04
  • 1
    Just to highlight the key points: `loader.load` returns a value that represents the loaded designer content, so *we should assign it as an attribute of `self`* to keep track of it later. We also need to `.show()` the content. To refer to the button (the actual question): the name given in the `.ui` file *will be an attribute name* on the object we got back from `loader.load`. – Karl Knechtel May 29 '22 at 16:02