Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to hide item text and down-arrow of QComboBox ?



  • Here is the preview of QComboBox with icons ( resultant from given code).

    alt text

    Now how can i hide item text and arraow from QComboBox editable ? and make drop-down pop-up aligned center to current editable icons.

    alt text

    Output should look like this

    alt text

    from PySide2 import QtCore, QtGui, QtWidgets
    import sys
    
    class statsMainWindowUI(QtWidgets.QMainWindow):
        def __init__(self):
            QtWidgets.QMainWindow.__init__(self)
            self.setupUi(self)    	
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(503, 575)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
            self.verticalLayout.setObjectName("verticalLayout")
            self.statsComboBox = QtWidgets.QComboBox(self.centralwidget)
            font = QtGui.QFont()
            font.setFamily("SF Pro Display")
            font.setPointSize(16)
            font.setWeight(75)
            font.setBold(True)
            self.statsComboBox.setFont(font)
            self.statsComboBox.setIconSize(QtCore.QSize(70, 20))
            self.statsComboBox.setObjectName("statsComboBox")
            self.statsComboBox.addItem("")
            self.statsComboBox.addItem("")
            self.statsComboBox.addItem("")
            self.statsComboBox.addItem("")
            self.verticalLayout.addWidget(self.statsComboBox)
            MainWindow.setCentralWidget(self.centralwidget)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "MainWindow", None, -1))
            self.statsComboBox.setItemText(0, QtWidgets.QApplication.translate("MainWindow", "High", None, -1))
            self.statsComboBox.setItemText(1, QtWidgets.QApplication.translate("MainWindow", "Medium", None, -1))
            self.statsComboBox.setItemText(2, QtWidgets.QApplication.translate("MainWindow", "Normal", None, -1))
            self.statsComboBox.setItemText(3, QtWidgets.QApplication.translate("MainWindow", "Low", None, -1))
            self.statsComboBox.setItemIcon(0, QtGui.QIcon('E:/code/highStats.png'))
            self.statsComboBox.setItemIcon(1, QtGui.QIcon('E:/code/mediumStats.png'))
            self.statsComboBox.setItemIcon(2, QtGui.QIcon('E:/code/lowStats.png'))
            self.statsComboBox.setItemIcon(3, QtGui.QIcon(E:/code/normalStats.png'))
    
    
    app = QtWidgets.QApplication(sys.argv)
    statsUI = statsMainWindowUI()
    statsUI.show()
    sys.exit(app.exec_())
    

  • Banned

    @ruchitinfushion okay hopefully all you have to do is replace the reference to PyQt5 with PySide2 but not sure on that one. Regardless if you cannot get this to work straight out of the gate then at least it should point you in the right direction of how to implement what you are trying to implement -- it also gets rid of the unnecessary Designer junk that was there and extends the classification appropriately. Lastly I do not have your images so I simply commented that part out and used straight Text. So hopefully all you will have to do is implement those lines if I read the documentation correctly -- so just comment out the pure Text lines in favor of the Icon/Pixmap related stuff

    from sys import exit as sysExit
    
    from PyQt5.QtCore    import Qt, QSize, pyqtSlot
    from PyQt5.QtGui     import QFont, QIcon
    from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QComboBox, QVBoxLayout, QLabel
    
    class CentralPanel(QWidget):
        def __init__(self, parent):
            QWidget.__init__(self)
            
            self.MyParent = parent
    
          # Best just to load these once at start
            self.pmHigh = QPixmap('E:/code/highStats.png')
            self.pmMedm = QPixmap('E:/code/mediumStats.png')
            self.pmLows = QPixmap('E:/code/lowStats.png')
            self.pmNorm = QPixmap('E:/code/normalStats.png')
    
            qgFont = QFont()
            qgFont.setFamily("SF Pro Display")
            qgFont.setPointSize(16)
            qgFont.setWeight(75)
            qgFont.setBold(True)
    
            self.lblIconHldr = QLabel('Hidden Label')
            self.lblIconHldr.setFont(qgFont)
            self.lblIconHldr.setAlignment(Qt.AlignHCenter)
            self.lblIconHldr.hide()
    
            self.cbxStats = QComboBox()
            self.cbxStats.setFont(qgFont)
    #        self.cbxStats.setIconSize(QSize(70, 20))
    
            self.cbxStats.addItem('High')
            self.cbxStats.addItem('Medium')
            self.cbxStats.addItem('Low')
            self.cbxStats.addItem('Normal')
    #        self.cbxStats.addItem(QIcon('E:/code/highStats.png'),'High')
    #        self.cbxStats.addItem(QIcon('E:/code/mediumStats.png'), 'Medium')
    #        self.cbxStats.addItem(QIcon('E:/code/lowStats.png'), 'Low')
    #        self.cbxStats.addItem(QIcon(E:/code/normalStats.png'), 'Normal')
    
            self.cbxStats.currentIndexChanged.connect(self.SetIcon)
    
            vbxLayout = QVBoxLayout()
            vbxLayout.addWidget(self.lblIconHldr)
            vbxLayout.addStretch(1)   # The position of this affects how things are rendered
            vbxLayout.addWidget(self.cbxStats)
            
            self.setLayout(vbxLayout)
    
        @pyqtSlot(int)
        def SetIcon(self, Indx):
            if Indx == 0:
                self.lblIconHldr.setText('High')
    #            pmImage = self.pmHigh
            elif Indx == 1:
                self.lblIconHldr.setText('Medium')
    #            pmImage = self.pmMedm
            elif Indx == 2:
                self.lblIconHldr.setText('Low')
    #            pmImage = self.pmLows
            else: 
                self.lblIconHldr.setText('Normal')
    #            pmImage = self.pmNorm
    
    #        pmImage.scaled(70, 20, Qt.KeepAspectRatio)
    #        self.lblIconHldr.setPixmap(pmImage)
    
            self.cbxStats.hide()
            self.lblIconHldr.show()
    
    class statsMainWindow(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
    
            self.setWindowTitle('Main Window')
            self.resize(300, 275)
    
            self.CenterPane = CentralPanel(self)
            self.setCentralWidget(self.CenterPane)
    
    if __name__ == '__main__':
        MainThred = QApplication([])
    
        MainGUI = statsMainWindow()
        MainGUI.show()
    
        sysExit(MainThred.exec_())
    
      # If anyone wants more extensive free help I run an online lab-like classroom-like 
      # message server feel free and drop by you will not be able to post until I clear 
      # you as a student as this prevents spammers so if interested here is the invite
      # https://discord.gg/3D8huKC
    

Log in to reply