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. Possible to have permanent, unselectable first item in QComboBox?
Forum Updated to NodeBB v4.3 + New Features

Possible to have permanent, unselectable first item in QComboBox?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 3.1k Views
  • 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.
  • Q Offline
    Q Offline
    Qt_User72653
    wrote on last edited by
    #1

    A look over the documentation came up with nothing, but, I'm looking for a way to basically have a 'title' for the a QComboBox that will always show, even when another value is selected. I'm essentially using it as a drop-down list of buttons so as soon as an item is clicked on it will send a signal, so the first index should never change.

    I could implement this with event filtering and so on but if there's a simpler way, that'd be great.

    J.HilkJ jsulmJ 2 Replies Last reply
    0
    • Q Qt_User72653

      A look over the documentation came up with nothing, but, I'm looking for a way to basically have a 'title' for the a QComboBox that will always show, even when another value is selected. I'm essentially using it as a drop-down list of buttons so as soon as an item is clicked on it will send a signal, so the first index should never change.

      I could implement this with event filtering and so on but if there's a simpler way, that'd be great.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Qt_User72653
      There are a couple of ways to do that, I'll list those who come to my mind:

      • Use a QPushButton and give it a menu (QPushButton::setMenu(QMenu* menu)) of QActions instead
      • Set the ComboBox as editable, make the lineedit part read only and use setText
      • Subclass QComboBox and overwrite the PaintEvent
      • Subclass QComboBox and change the behaviour any other way than with the paintevent

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      Q 1 Reply Last reply
      3
      • Q Qt_User72653

        A look over the documentation came up with nothing, but, I'm looking for a way to basically have a 'title' for the a QComboBox that will always show, even when another value is selected. I'm essentially using it as a drop-down list of buttons so as soon as an item is clicked on it will send a signal, so the first index should never change.

        I could implement this with event filtering and so on but if there's a simpler way, that'd be great.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Qt_User72653 Why do you want a "title" inside the combobox?
        Isn't it quite confusing for the user if he/she selects a value, but instead of that value this "title" is shown?
        I as user would think that it is simply not working.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        Q 1 Reply Last reply
        2
        • J.HilkJ J.Hilk

          @Qt_User72653
          There are a couple of ways to do that, I'll list those who come to my mind:

          • Use a QPushButton and give it a menu (QPushButton::setMenu(QMenu* menu)) of QActions instead
          • Set the ComboBox as editable, make the lineedit part read only and use setText
          • Subclass QComboBox and overwrite the PaintEvent
          • Subclass QComboBox and change the behaviour any other way than with the paintevent
          Q Offline
          Q Offline
          Qt_User72653
          wrote on last edited by
          #4

          @J.Hilk Thanks, I'll try these out.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            I'll add: Use a model (like QStandardItemModel) via QComboBox::setModel and make sure the flag() method of the model returns Qt::NoItemFlags for the first item

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            JonBJ 1 Reply Last reply
            3
            • jsulmJ jsulm

              @Qt_User72653 Why do you want a "title" inside the combobox?
              Isn't it quite confusing for the user if he/she selects a value, but instead of that value this "title" is shown?
              I as user would think that it is simply not working.

              Q Offline
              Q Offline
              Qt_User72653
              wrote on last edited by
              #6

              @jsulm It's not really the selected or clicked value that's important, because clicking on one of the items is going to open up a new window. It's basically a drop-down box of buttons, with the item displayed in the combobox being the header for the list of buttons.

              Actually, now that I've reviewed it, a QPushButton with a menu is exactly what I need.

              1 Reply Last reply
              0
              • VRoninV VRonin

                I'll add: Use a model (like QStandardItemModel) via QComboBox::setModel and make sure the flag() method of the model returns Qt::NoItemFlags for the first item

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @VRonin said in Possible to have permanent, unselectable first item in QComboBox?:

                I'll add: Use a model (like QStandardItemModel) via QComboBox::setModel and make sure the flag() method of the model returns Qt::NoItemFlags for the first item

                Often a combobox starts with a first item like "Pick one of the following". Presumably this would be just the right/easy way to implement that?

                1 Reply Last reply
                0

                • Login

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