Colin Warn
Colin Warn

Reputation: 411

How to "Click" a QWidget Button in Code Python

Question is as stated. I'm working on code that sets a voltage to an Arduino out when the user clicks one of the QWidget buttons. I'm creating a test process which sets the voltages through a computer program, and I'd like to interface through this code that has already been previously written.

Is there something like a QWidget.button.click function in QWidget that allows me to "click" the button in code?

Example code of one of these buttons from the source code I'm interfacing with.

self.plainTextEditCh_7 = QPlainTextEdit()
        self.plainTextEditCh_7.setMaximumSize(QtCore.QSize(150, 30))
        self.plainTextEditCh_7.setObjectName("plainTextEditCh_7")
        grid.addWidget(self.plainTextEditCh_7, 7, 3, 1, 1)
        self.lineEditCh_7 = QLineEdit()
        self.lineEditCh_7.setObjectName("lineEditCh_7")
        grid.addWidget(self.lineEditCh_7, 7, 1, 1, 1)
        self.buttonCh_7 = QPushButton()
        self.buttonCh_7.setObjectName("buttonCh_7")
        grid.addWidget(self.buttonCh_7, 7, 2, 1, 1)
        self.labelCh7 = QLabel()
        self.labelCh7.setObjectName("labelCh7")
        grid.addWidget(self.labelCh7, 7, 0, 1, 1)
        self.plainTextEditCh_7.setReadOnly(True)
        self.buttonCh_7.clicked.connect(lambda: DAC_Communication.set_voltage(channel=7,
                                                                              line_edit=self.lineEditCh_7,
                                                                              plain_text=self.plainTextEditCh_7))

Upvotes: 0

Views: 101

Answers (1)

Colin Warn
Colin Warn

Reputation: 411

QWidget has a Class QAbstractButton that allows a coder to create "virtual clicks" in the code. https://doc.qt.io/qt-5/qabstractbutton.html#click

An example implementation:

form.buttonCh_3.click()

Upvotes: 1

Related Questions