0

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.

Alon123
  • 164
  • 3
  • 15
  • Your question is a bit unclear. Do you want the combobox to resize itself to the *current* item text whenever it's clicked? – musicamante Feb 05 '20 at 17:32
  • Yes, but it is not my problem... my problem is that the function that is checking for ports+resizing the combobox is being called without me clicking on the combobox. – Alon123 Feb 05 '20 at 17:33
  • That's because of the line `self.ui.portList.mousePressEvent=self.findPort()`. I guess you meant `self.ui.portList.mousePressEvent=self.findPort` (without paranetheses), but that's not the way forward. You could try subclassing `QComboBox` and override `QComboBox.showPopup` and `QComboBox.hidePopup`. – Heike Feb 05 '20 at 17:39
  • without parentheses it doesn't run immediately, but when I click on it I get a message that says findport() takes 1 arg but 2 are given.... not sure why – Alon123 Feb 05 '20 at 17:42
  • @Alon123 Do not use `self.ui.portList.mousePressEvent=self.findPort`, a better option is to use an eventFilter or create a custom QComboBox where you override the showPopup or mousePressEvent method. I would also recommend using QSerialPort instead of pyserial since that is how you use the magic of the signals – eyllanesc Feb 05 '20 at 17:49
  • I really believe that you're having an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Besides the problem about using the function call instead of the *callable* you should: 1. remember that `mousePressEvent` has an `event` parameter, 2. you should call the base `mousePressEvent` implementation of QComboBox, 3. resizing of a combobox upon *any* mouse press event is a **bad** idea. – musicamante Feb 05 '20 at 17:50
  • eyllanesc could you point me to a guide or an extended explanation of how to do that please? since I am not sure what eventFilter is, or how to create a custom QComboBox – Alon123 Feb 05 '20 at 17:52

0 Answers0