QWidget QApplication

I’m lost error follows:

QWidget: Must construct a QApplication before a QWidget

Process ended with exit code 3221226505.”

code follows;

except serial.SerialException as e:
msg = QtWidgets.QMessageBox()
msg.setWindowTitle(“Serial Error”)
msg.setText(f"Failed to open {serialPort}: {str(e)}\nCheck: 1) Port in Device Manager, 2) Close Arduino IDE Serial Monitor, 3) Run as admin")
msg.exec_()

In order to preserve formatting, please select any code or traceback that you post and then click the </> button.

There isn’t enough code there to determine what’s happening.

Is the code trying to open the serial port early, before the main GUI code has started, before QApplication has been called?

Got the formatting issue ..thanks. new to this process

I can’t answer the second question, would more code help?

timePoints = np.empty(0)  # Timestamps for gyro plots
startTime = QtCore.QTime.currentTime()  # Start time for elapsed time calculation# — Serial Connection Initialization —
try:
ser = serial.Serial(serialPort, baudRate, timeout=timeout)  # Open serial port
ser.reset_input_buffer()  # Clear input buffer to remove stale data
except serial.SerialException as e:
msg = QtWidgets.QMessageBox()
msg.setWindowTitle(“Serial Error”)
msg.setText(f"Failed to open {serialPort}: {str(e)}\nCheck: 1) Port in Device Manager, 2) Close Arduino IDE Serial Monitor, 3) Run as admin")
msg.exec_()
exit(1)# — Generate Arduino Code —

Strategy: Generate Arduino code with calibration parameters for onboard processing.

Gyro offsets are in °/s to match Python processing.


There’s no indentation in that code. Indentation is important in Python!

You could try writing to a log file, something like:

log_path = os.path.splitext(__file__)[0] + ".log"
log_file = open(log_path, "w")

near the top.

Just after the except line, put:

    log_file.write(f"Serial error: {e}\n")

Where you create the app with QApplication put:

log_file.write("Creating application\n")

Then , when you’ve run the program, check whether the log file says that the program created the application before the serial error was reported.

Python has a logging module for doing logging properly, but I’d leave that until this problem has been dealt with first.

You MUST create a QApplication object first in any Qt program.

2 Likes

Mr Scott,
Many thanks for your attention to this. I am new to Python and somewhat acquainted with C. I am attempting to create an "aircraft like’ instrument panel addition for my car, attitude indicator, turn and slip, altituded, vertical speed, electromagnetic compass. This particular software is used, I hope, to calibrate a 9 axis gyro/compass/accelerometer. All of the instruments can be included in a horizontal rectangular electronic screen contained in a plastic frame mounted at the base of the windshield/dashboard out of/below necessary view angle but visible

I was (some considerable time past) a military pilot and grew attached to having that information available. I doubt it will be of any practical use but my grandson (15) is intrigued with the idea and is helping with the “concept”. He’s going to do the frame design and 3D printing. I’ll do the physical wiring / electronics.

More than you wanted to know.

In any event, many thanks for your time and talent. The “python forum” certainly is a civilized activity, I’m glad you’re in.

vontolkacz@gmail.com

This response was intended for mr. Scott and Mr Barnett
vontolkacz

I don’t think you posted the entirety of your code, since there are no import statements, but you need to have something like this this line somewhere near the top of your program:

app = QtWidgets.QApplication(sys.argv)

1 Like