0

I've created a program in python that controls an instrument (a power source) using the pyvisa library. The program has a GUI I designed with qtcreator where the file format is a .ui which then I convert to a .py using pyuic6: basically I did the same of this video except I used pyuic6 instead of 5.

Everything worked and I kept adding functionalities till I decided, for security reasons, to catch the pressure of the "x" button that closes the program so I can tell to my power supply to switch-off and avoid dangerous situations.

I found examples to do that with the closeEvent method, like the answer of this question. The example works as is, but when I try to put it on my code seems the closeEvent method it isn't triggered when I actually close the windows.

here some code:

The file I launch "main.py"

import datetime
import math
import os
import queue
import subprocess
import sys
import threading
import time

import exitwindow
import infoloadwindow
from tkinter import filedialog

import matplotlib.pyplot as plt
import pandas as pd
import pyvisa as pv
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas

from PyQt6.QtCore import QObject, QThread, pyqtSignal
from qtpy import QtWidgets
from form_v32 import Ui_TESTALIM

--- some code ---

class MiaUi(Ui_TESTALIM):
    c_voltage, c_current, c_resistance, c_power = 0.0, 0.0, 0.0, 0.0

    def __init__(self, Widget):
        self.setupUi(Widget)
        self.tableWidget.verticalHeader().setVisible(False)
        self.horizontalLayout_20 = QtWidgets.QHBoxLayout(
            self.frame_grafico)
        self.horizontalLayout_20.setObjectName("horizontalLayout_20")
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.horizontalLayout_20.addWidget(
            self.canvas)
        self.check()

    def check(self):
        self.start_on.clicked.connect(self.lettura_cicli)
        self.aggiungi.clicked.connect(self.aggiungi_riga)
        self.carica_da_file.clicked.connect(self.load_from_csv)
        self.elimina.clicked.connect(self.cancella_riga)
        self.ovp_on.clicked.connect(self.ovp_acceso)
        self.ovp_off.clicked.connect(self.ovp_spento)
        self.ocp_on.clicked.connect(self.ocp_acceso)
        self.ocp_off.clicked.connect(self.ocp_spento)
        self.start_off.clicked.connect(self.spegni)
        self.salvataggi.clicked.connect(self.apricartella)
        self.collega_strumento.clicked.connect(self.collegamento)

    def closeEvent(self, event):
        print("I guess it worked")

--- other methods ---

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MyWidget = QtWidgets.QWidget()
    ui = MiaUi(MyWidget)
    MyWidget.show()
    MyWidget.setFixedSize(1110, 600)
    sys.exit(app.exec())

The gui file "form_v32.py"

from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_TESTALIM(object):
    def setupUi(self, TESTALIM):
        TESTALIM.setObjectName("TESTALIM")
        TESTALIM.resize(1110, 600)
        TESTALIM.setAutoFillBackground(False)
        TESTALIM.setStyleSheet("background-color: rgb(255,255,255);\n"
"border-color: rgb(0, 0, 0);")

--- other code ---

    def retranslateUi(self, TESTALIM):
        _translate = QtCore.QCoreApplication.translate
        TESTALIM.setWindowTitle(_translate("TESTALIM", "Widget"))
        self.savings.setText(_translate("TESTALIM", "save"))

-- other code and eof---

My feeling is that closeEvent isn't seen as a method of form_V32 but only as a method of the class MiaUI, and that's why it does not trigger anything, but I can't understand the cause. I have the doubt something changed related to this from Pyqt5 to Pyqt6, but I'm not sure. I have even the doubt I might miss some sort of import so I reported all of them.

Thanks in advance

Foxite
  • 1
  • You're not expected to edit those generated files, but properly follow the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt6/designer.html) instead. – musicamante Aug 24 '22 at 16:09
  • To clarify: even if you don't edit the pyuic file, subclassing the ui class alone is pointless and has the same result: that's *not* a Qt widget, is a basic python `object` so your attempt to override `closeEvent()` is useless: you're *not* overriding; that function is called on the QWidget instance, so you must subclass QWidget and override the function in that class. To properly subclass with the ui you need to use the multiple inheritance method as in the link above (`class MiaUi(QWidget, Ui_TESTALIM)`), call `self.setupUi(self)` in the `__init__` and just create an instance of `MiaUi`. – musicamante Aug 24 '22 at 22:28
  • I actually didn't edit the pyuic file, just reported the structure. The multiple inheritance method for sure is the problem, I actually modified the class as you suggested, so my class now is `class MiaUi(QWidget, Ui_TESTALIM):`, and in the `__init__` added `self.setupUi(self)` and, before than that `QWidget.__init__(self, widget)` as indicated [here](https://stackoverflow.com/questions/46544780/qtdesigner-changes-will-be-lost-after-redesign-user-interface) and kept the declaration as in the main.py example I posted but I'm still doing something wrong – Foxite Aug 26 '22 at 06:42
  • Did you properly update the `if __name__ == "__main__"` block? How? Did you follow the indications of the official PyQt docs linked above? Use a pastebin with your updated code. – musicamante Aug 26 '22 at 09:14
  • ok, found the mistake. In this [link](https://pastebin.com/B93b2h24) how I modified my code compared to the one in the question. I actually couldn't see where is explained how to modify the main in your link (I feel a bit stupid), but I solved looking [here](https://stackoverflow.com/questions/66819121/how-to-use-python-code-generated-via-qt-designer-directly-in-python). Many thanks, have a good one – Foxite Aug 26 '22 at 12:36

0 Answers0