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

How to set QComboBox?



  • 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() ).



  • @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?



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



  • @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.



  • @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 ).



  • @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.



  • @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...



  • 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);
    }
    

Log in to reply