I am trying to get a list of ports with QComboBox when it is clicked, and resize it so I can see the full port name, but I am having some issues: I tried to connect the combobox on "clicked", but it does not work, so I try to go with mousepressedevent, but when I run the script it goes to the function straight away, even if I don't press on it. I also want to make the combobox small again if the user clicks anywhere outside of the combobox or it's drop-down list.
Here is my code: This part is the GUI code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(780, 590)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.portList = QtWidgets.QComboBox(self.centralwidget)
self.portList.setGeometry(QtCore.QRect(50, 50, 50, 25))
self.portList.setObjectName("portList")
MainWindow.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
And this is the part with the actual code:
from PyQt5 import QtWidgets, QtCore, QtGui
from test1 import Ui_MainWindow
import serial.tools.list_ports
import sys
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.portList.mousePressEvent=self.findPort()
# self.ui.portList.clicked.connect(self.findPort) <---- this does not work
def findPort(self):
comPorts = list(serial.tools.list_ports.comports()) # get a list of all devices connected through serial port
self.i=0
for counter in comPorts:
strPort=str(counter)
self.ui.portList.addItem("")
self.ui.portList.setItemText(self.i,strPort)
self.i=self.i+1
if self.i==0:
self.ui.portList.addItem("")
self.ui.portList.setItemText(0,"No Ports!")
else:
self.ui.portList.resize(500,25)
def clickedOutside(self): ####<---- not sure how to connect to this func (when clicked outside of the list)
self.ui.portList.resize(50,25)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
If anyone has an idea it would be very much appreciated.