Looking for guidance on PyQt5, and implementing paintEvent and drawing shapes via functions, for lightgun GUI project
-
Hello,
I am working on a lightgun project, and I'm communicating with a raspberry pico via a python program on my linux desktop
I built a GUI in qt designer, and then used pyuic5 to export a .py file that will run as a single file. I started coding in the output file of pyuic5, which I understand you are not supposed to do, but I am going to roll with it for now.
Currently my GUI is able to send and receive data from my Pico.
One of the pieces of info I can receive from the Pico, is a 12 value, comma separated byte array. In my code, i take that full value, strip out any formatting information, and put it into a python list, and convert them to Int. This is working properly as well
These 12 values are the x and y coordinates of the centers of 6 circles that I want to draw (px1,py1,px2,py2, etc etc), as well as 4 lines to connect the centers of the circles.
What I want to happen is that when I press my 'test camera' button, the function checks to see if serial data is available to read, and if so, it queries the pico every 0.2 seconds, and then updates the list, and then redraws the 6 circles and 4 lines, and it does this for 5 seconds, so basically 25 queries, 25 drawings
I realize I have to use a paintEvent function, but I am a bit lost as to how to actually implement that. I am a bit of a noob when it comes to Python and how to properly work with it, so its possible that my janky organization of stuff is causing me extra grief.
I'd appreciate it if someone could take a look at my code, and the function test_cam, and maybe provide some suggestions or food for thought on how I could implement paintEvent. I have included a commented out version of each of the drawEllipse and drawLine functions I wanted to use, but I haven't implemented any paintEvent function within my code yet (well I have, but I deleted it as I couldn't get it working and was worried I might screw something up )
I put my full script into a paste bin, as it is quite long. i can also paste it here if that is better.
-
S SGaist moved this topic from General and Desktop on
-
Hi and welcome to devnet,
There are two main issues:
- you are using a right loop which blocks Qt's event loop
- you are trying to paint your widget outside the paintEvent method
For the former, you can use a QTimer for example to trigger the read at regular interval.
For the latter, you should store as member variables and use them in the paintEvent method to draw whatever you need. An alternative would be to draw on a QPixmap in the read method and then draw that pixmap in the paintEvent.
-
Thank you, I will review what you said and see what I can do.
For reference, I realize that my initial pastebin code would not work for anyone attempting to test it, because it was checking for a serial connection. I removed everything else such that it is just a single push button now called test camera, which calls a function called test_cam2, and that function has a loop with a python list that has random integers being put into it, such that I can visual see the changes when I finally get things working
-
Just in case, Qt has QSerialPort which works asynchronously for interacting with serial ports.