Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PyQt6 Selecting multiple items from a QComboBox
Forum Updated to NodeBB v4.3 + New Features

PyQt6 Selecting multiple items from a QComboBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
qcomboboxselection mode
3 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mpnix
    wrote on 27 Sept 2024, 04:00 last edited by
    #1

    I have a QComboBox populated from a database table. I want the user to be able to select one or more items to search other tables in the database. I considered the following to be logical but it's not permissible:
    self.tagComboBox.setSelectionMode(QComboBox.InsertPolicy.QAbstractItemView.MultiSelection)

    My code to establish the QComboBox and window:

    from PyQt6.QtCore import Qt
    from PyQt6.QtGui import QPixmap
    from PyQt6.QtWidgets import (
        QApplication,
        QCheckBox,
        QComboBox,
        QDial,
        QDoubleSpinBox,
        QLabel,
        QLineEdit,
        QListWidget,
        QMainWindow,
        QSlider,
        QAbstractItemView,
        QSpinBox
    )
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
    
            self.setWindowTitle("SQL Lite tags to combobox")
    
            self.tagComboBox = QComboBox()
            self.tagComboBox.setInsertPolicy(QComboBox.InsertPolicy.InsertAlphabetically)
    #       self.tagComboBox.setSelectionMode(QComboBox.InsertPolicy.QAbstractItemView.MultiSelection)
            self.tagComboBox.setEditable(True)
            self.tagComboBox.currentIndexChanged.connect(self.index_changed) # Sends a signal with the new index value
            self.tagComboBox.currentTextChanged.connect(self.text_changed)   # Sends a signal with the new text
    
            self.setCentralWidget(self.tagComboBox)
    
    P 1 Reply Last reply 27 Sept 2024, 04:09
    0
    • M mpnix
      27 Sept 2024, 04:00

      I have a QComboBox populated from a database table. I want the user to be able to select one or more items to search other tables in the database. I considered the following to be logical but it's not permissible:
      self.tagComboBox.setSelectionMode(QComboBox.InsertPolicy.QAbstractItemView.MultiSelection)

      My code to establish the QComboBox and window:

      from PyQt6.QtCore import Qt
      from PyQt6.QtGui import QPixmap
      from PyQt6.QtWidgets import (
          QApplication,
          QCheckBox,
          QComboBox,
          QDial,
          QDoubleSpinBox,
          QLabel,
          QLineEdit,
          QListWidget,
          QMainWindow,
          QSlider,
          QAbstractItemView,
          QSpinBox
      )
      
      class MainWindow(QMainWindow):
          def __init__(self):
              super().__init__()
      
              self.setWindowTitle("SQL Lite tags to combobox")
      
              self.tagComboBox = QComboBox()
              self.tagComboBox.setInsertPolicy(QComboBox.InsertPolicy.InsertAlphabetically)
      #       self.tagComboBox.setSelectionMode(QComboBox.InsertPolicy.QAbstractItemView.MultiSelection)
              self.tagComboBox.setEditable(True)
              self.tagComboBox.currentIndexChanged.connect(self.index_changed) # Sends a signal with the new index value
              self.tagComboBox.currentTextChanged.connect(self.text_changed)   # Sends a signal with the new text
      
              self.setCentralWidget(self.tagComboBox)
      
      P Offline
      P Offline
      Pl45m4
      wrote on 27 Sept 2024, 04:09 last edited by Pl45m4
      #2

      @mpnix

      Quick google search:

      • https://gis.stackexchange.com/questions/350148/qcombobox-multiple-selection-pyqt5

      Or you translate this code to Python:

      • https://stackoverflow.com/questions/6505627/how-to-make-qcombobox-as-multiselect-in-qt

      Anyway, why do you want to use a QComboBox as it's not intended to be used this way?!
      Maybe a QListWidget / QListView is more fitting...
      (there you have proper selection model support and it's more intuitive than a dropdown, where you have to click multiple times and close it manually again)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      M 1 Reply Last reply 27 Sept 2024, 06:38
      2
      • P Pl45m4
        27 Sept 2024, 04:09

        @mpnix

        Quick google search:

        • https://gis.stackexchange.com/questions/350148/qcombobox-multiple-selection-pyqt5

        Or you translate this code to Python:

        • https://stackoverflow.com/questions/6505627/how-to-make-qcombobox-as-multiselect-in-qt

        Anyway, why do you want to use a QComboBox as it's not intended to be used this way?!
        Maybe a QListWidget / QListView is more fitting...
        (there you have proper selection model support and it's more intuitive than a dropdown, where you have to click multiple times and close it manually again)

        M Offline
        M Offline
        mpnix
        wrote on 27 Sept 2024, 06:38 last edited by
        #3

        @Pl45m4 Thank you. My Google-foo (or perhaps it's Bing-fooey!), lacks your power!

        And, as to your second question: Because I'm still finding my way around Qt. I didn't really like the ComboBox behaviour, I'll have a look at the alternatives you suggested.

        1 Reply Last reply
        0

        1/3

        27 Sept 2024, 04:00

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved