8

I would like to set the text of a QComboBox to some custom text (that is not in the QComboBox's list), without adding this text as an item of the QComboBox. This behaviour is achievable on an editable QComboBox with QComboBox::setEditText(const QString & text). On a non-editable QComboBox, however, this function does nothing.

Is it possible to programmatically set the display/edit text of a non-editable QComboBox to something that is not in its list? Or do I have to find another way (e.g. use a QPushButton with a popup menu)

EDIT: Consider an editable QComboBox with InsertPolicy QComboBox::NoInsert. If the user types in something and hits enter, the entered value will be used but not added to the list. What I want is this behaviour to change the 'current' text programmatically, but without allowing the user to type in some text himself. The user can choose something from the QComboBox, but some time later, I may want to override the 'current' text.

PrisonMonkeys
  • 1,199
  • 1
  • 10
  • 20
  • what are you trying to achieve, something like tooltip? In editable combobox it's used do add some unpredicted options i guess, but if not editable, why not add that tooltip text as item with zero index for example and after it add items, that are needed – Shf Apr 18 '13 at 11:23

4 Answers4

8

I had the same problem when I subclassed QComboBox to make a combo box of check boxes. I wrote a small function to programmatically change the text displayed in the combo box, but I didn't want to enable the user to edit that text. The solution was to set the combo box as editable:

 this->setEditable(true);

and the QComboBox::lineEdit() to read only. Refer to the function:

void CheckedComboBox::setText(QString text)
{
   QLineEdit *displayedText = this->lineEdit();
   displayedText->setText(text);
   displayedText->setReadOnly(true);
}
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Jays
  • 201
  • 1
  • 3
  • 8
  • 2
    Thanks, this works. It's just too bad that the UI changes a lot due to the `setEditable` (at least on Mac it changes). – PrisonMonkeys Feb 17 '15 at 15:57
2

Reimplement paintEvent : https://github.com/qt/qtbase/blob/28d1d19a526148845107b631612520a3524b402b/src/widgets/widgets/qcombobox.cpp#L2995

and add this line : opt.currentText = QString(tr("My Custom Text"));

Example :

QCustomCheckComboBoxFilter.h

...
protected:
    void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE;
...

QCustomCheckComboBoxFilter.cpp

...
void QCustomCheckComboBoxFilter::paintEvent(QPaintEvent *)
{
    QStylePainter painter(this);
    painter.setPen(palette().color(QPalette::Text));

    // draw the combobox frame, focusrect and selected etc.
    QStyleOptionComboBox opt;
    initStyleOption(&opt);
    opt.currentText = QString(tr("My Custom Text"));
    painter.drawComplexControl(QStyle::CC_ComboBox, opt);

    // draw the icon and text
    painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
}
...
Sebastien247
  • 423
  • 5
  • 16
1

I supposed that you want to have a combo box with "A", "B", "C" as actual data and "This is A" , "This is B" and "This is c" as what is displayed in QComboBox. Here is the code:

box.addItems(QStringList () << "This is A"<< "This is B"<< "This is C");
box.setItemData(0, "A");
box.setItemData(1, "B");
box.setItemData(2, "C");

You can get the actual data with this code :

QString actual = box.itemData(0).toString();//actual will be = "A";
qDebug()<<actual;//"A"

Note: You can almost set every data types that you want for a combo box Item. Even more, you can set more that just one additional data for each item with the third parameter of setItemData.

s4eed
  • 7,173
  • 9
  • 67
  • 104
  • 1
    That is not really what I want. What I want is a QComboBox with 3 options ("A", "B", "C") and the displayed text could be either one of those three options or something else ("Nothing from the list"). This should not be a fourth option in the list. And I want to change the actual text of this something else repeatedly. – PrisonMonkeys Apr 18 '13 at 11:42
1

I ended up using a QPushButton with a popup menu. I added the items I had in the list of my QComboBox as QActions to the menu. A menu can be set on a QPushButton with

QPushButton::setMenu(QMenu* menu)

. The text on the button can easily be set with

QPushButton::setText(const QString &)

and is unrelated to the text in the popup menu, which is what I wanted.

PrisonMonkeys
  • 1,199
  • 1
  • 10
  • 20