I've been developing an application that takes coordinates from a serial port and creates a shape at said coordinates. The coords given from the serial port are in centimeters, so I convert them to pixels. (This conversion has been tested and works). However, my shapes seem to be confined to a box in the upper left-hand corner of my screen. Here you can see the result of multiple coordiantes tested. Shape Image. The circles are being cut off.
I've included my code below. Apologies if this is a newbie question, it's my first time using PYQT.
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QVBoxLayout
from PyQt6.QtCore import Qt, pyqtSignal, QObject, QThread
from PyQt6.QtGui import QPainter, QPen, QBrush, QColor, QPixmap
import time
from backend.IRserial import IRserial
class CircleWidget(QWidget):
def __init__(self, xCoord=0, yCoord=0):
super().__init__()
self.xCoord = xCoord
self.yCoord = yCoord
def paintEvent(self, Event):
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
# Set the pen (outline) and brush (fill) for the circle
pen = QPen()
pen.setColor(QColor("blue"))
pen.setWidth(2)
brush = QBrush()
brush.setColor(QColor("lightblue"))
brush.setStyle(Qt.BrushStyle.SolidPattern)
painter.setPen(pen)
painter.setBrush(brush)
# Draw a circle at position (100, 100) with a radius of 50
painter.drawEllipse(self.xCoord, self.yCoord, 50, 50)
class find_coords(QThread):
event_ready = pyqtSignal(int, int)
# This method runs in a separate thread
def run(self):
xMax = 27 #update this when you figure out the actual dimensions
yMax = 16.5
app = QApplication.instance()
# If the application is running
if (app is not None):
#--- DPI ---
screen = app.primaryScreen()
dpi = screen.logicalDotsPerInch()
# ------
sr = IRserial()
while 1:
l = sr.get_packet()
x = l[0]
y = l[1]
# ------ Convert x/y to top down coords ------
if (x < 0):
x = xMax - abs(x)
else:
x = xMax + x
if (y < 0):
y = yMax + x
else:
y = yMax - abs(y)
#------------------------------------------
# ---- Pixel representations of x and y ------
xPix = (x * dpi) / 2.54 # 1 inch = 2.54 cm
yPix = (y * dpi) / 2.54
#------------------------------------------
print(f"xPix: {xPix}")
print(f"yPix: {yPix}")
self.event_ready.emit(xPix, yPix)
def print_coords(x, y, window):
circle_widget = CircleWidget(x, y)
lay = window.layout()
lay.addWidget(circle_widget)
def main():
app = QApplication(sys.argv)
# --------- Create and scale window ---------
window = QMainWindow()
window.setWindowTitle("Scaled Window")
window.showMaximized() # Show the window maximized
# Get the screen size
screen = app.primaryScreen()
size = screen.availableGeometry()
# Set the window size to match the screen size
window.setGeometry(size)
#Removes window frame and title bar
window.setWindowFlags(Qt.WindowType.FramelessWindowHint)
#--------------------------------------------
image_widget = QWidget()
# Create a QVBoxLayout to center the image vertically
lay = QVBoxLayout(image_widget)
lay.setAlignment(Qt.AlignmentFlag.AlignCenter)
label = QLabel()
# Load an image from a file and set it to the label
pixmap = QPixmap("bullseye.jpg")
label.setPixmap(pixmap)
lay.addWidget(label)
window.setCentralWidget(image_widget)
#--------- Threading
find_coords_thread = find_coords()
find_coords_thread.start()
#-------------------
slot_function = lambda x, y: print_coords(x, y, window)
find_coords_thread.event_ready.connect(slot_function)
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()
I've tried changing the layout type and creating the layout in the print_coords function.