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. How to set QComboBox?
Forum Updated to NodeBB v4.3 + New Features

How to set QComboBox?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 748 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by
    #1

    Hello,

    I have my class, which inherits from QComboBox. I set on it:

           setEditable(true);
           QLineEdit *displayedText = this->lineEdit();
           displayedText->setText(text);
           displayedText->setReadOnly(true);
    

    exa.png

    Now when I would like to click on this widget in a green area I can't ( the red area works ).

    My goal: when I click on green area I would like to expand the list like in a standard QComboBox ( with no setEditable() and no lineEdit() ).

    JonBJ 1 Reply Last reply
    0
    • T TomNow99

      Hello,

      I have my class, which inherits from QComboBox. I set on it:

             setEditable(true);
             QLineEdit *displayedText = this->lineEdit();
             displayedText->setText(text);
             displayedText->setReadOnly(true);
      

      exa.png

      Now when I would like to click on this widget in a green area I can't ( the red area works ).

      My goal: when I click on green area I would like to expand the list like in a standard QComboBox ( with no setEditable() and no lineEdit() ).

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

      @TomNow99
      Question: if your combo is editable but you want click on line edit to treat as combo dropdown, how does user click to edit the text?

      1 Reply Last reply
      1
      • T Offline
        T Offline
        TomNow99
        wrote on last edited by
        #3

        @JonB I set the lineEdit() to read only, so user can't click on lineEdit() to change a text.

        JonBJ 1 Reply Last reply
        0
        • T TomNow99

          @JonB I set the lineEdit() to read only, so user can't click on lineEdit() to change a text.

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

          @TomNow99
          I don't understand why you would want

          setEditable(true);
          displayedText->setReadOnly(true);
          

          Why is it editable if you don't want user to edit the text?

          Anyway, my only answer to your question would be to look at a signal for clicking on the QLineEdit. Like maybe https://doc.qt.io/qt-5/qlineedit.html#mousePressEvent. You may be able to do it on the combo viewport too, I don't know. And then act on that to do the dropdown, or whatever you want.

          1 Reply Last reply
          1
          • T Offline
            T Offline
            TomNow99
            wrote on last edited by
            #5

            @JonB So I create QComboBox with checkboxes.

            The texts in checkboxes will be:
            "1"
            "2"
            "3"
            "4"

            When user choose "3", "4", I would like to show in QComboBox in lineEdit() text "3 and 4".

            So I have to setEditable(true) because I would like to set my own text ( "3 and 4") and I have to set displayedText->setReadOnly(true); because I don't want to user change the text in lineEdit() ( for example click backspace ).

            JonBJ Pablo J. RoginaP 2 Replies Last reply
            0
            • T TomNow99

              @JonB So I create QComboBox with checkboxes.

              The texts in checkboxes will be:
              "1"
              "2"
              "3"
              "4"

              When user choose "3", "4", I would like to show in QComboBox in lineEdit() text "3 and 4".

              So I have to setEditable(true) because I would like to set my own text ( "3 and 4") and I have to set displayedText->setReadOnly(true); because I don't want to user change the text in lineEdit() ( for example click backspace ).

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

              @TomNow99
              A combobox is for choosing one item among a selection. If I understand right, you seem to be using it to allow multiple choices, which in turn are fired from checkboxes. If that's about right, a combo box is not the right choice of widget. For example, a QLisWidget does allow multiple selections.. And if you really wanted to do this, you might achieve it by dynamically adding choices to the combo's list, not via its line edit.

              Anyway, as i wrote earlier I think you have to recognise the user clicking/mouse-downing on the line edit and act on that.

              1 Reply Last reply
              2
              • T TomNow99

                @JonB So I create QComboBox with checkboxes.

                The texts in checkboxes will be:
                "1"
                "2"
                "3"
                "4"

                When user choose "3", "4", I would like to show in QComboBox in lineEdit() text "3 and 4".

                So I have to setEditable(true) because I would like to set my own text ( "3 and 4") and I have to set displayedText->setReadOnly(true); because I don't want to user change the text in lineEdit() ( for example click backspace ).

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #7

                @TomNow99 said in How to set QComboBox?:

                So I have to setEditable(true) because I would like to set my own text ( "3 and 4")

                If you want to add an option to the QComboBox just use insertItem(), right?.

                An editable combobox "can be edited by the user" which is quite the opposite of what you want...

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                3
                • B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #8

                  I feel that what you want is still a non-editable combo box, just neet to change its current text.
                  Since you already subclass your own, then instead of using editable / QLineEdit, how about reimplement the paintEvent while keeping it not editable?
                  The original source code of QComboBox::paintEvent is :

                  void QComboBox::paintEvent(QPaintEvent *)
                  {
                      QStylePainter painter(this);
                      painter.setPen(palette().color(QPalette::Text));
                  
                      // draw the combobox frame, focusrect and selected etc.
                      QStyleOptionComboBox opt;
                      initStyleOption(&opt);
                      painter.drawComplexControl(QStyle::CC_ComboBox, opt);
                  
                      // draw the icon and text
                      painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
                  }
                  

                  You can copy that and add one line:

                  void MyComboBox::paintEvent(QPaintEvent *)
                  {
                      QStylePainter painter(this);
                      painter.setPen(palette().color(QPalette::Text));
                  
                      // draw the combobox frame, focusrect and selected etc.
                      QStyleOptionComboBox opt;
                      initStyleOption(&opt);
                      painter.drawComplexControl(QStyle::CC_ComboBox, opt);
                  
                      // draw the icon and text
                      opt.currentText = "some text you want to show";
                      painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
                  }
                  
                  1 Reply Last reply
                  2

                  • Login

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