QComboBox long item text breaks layout
-
Hi folks,
I am a little bit stuck with this. I have a input form that contains a lot of input fields (QTextEdit, QLinEdit, QComboBox) each with a description label in a QFormLayout. The layout is in a QScrollArea.
As long as the text of the items in any QComboBox is not too long, all looks fine, the QScrollArea grows vertically as it should be.
But if the text is long (lets say, very long), it grows horizontally too.Is there a way to
- have linebreaks inside a QComboBox?
- avoid that the QScrollArea grows in horizontal direction?
- or limit the width of the QComboBox itself so it displays elided text, but the popup should display the full text?
Thanks for your inputs :)
-
One idea that should work (assuming it is acceptable to you) is to have the text in the combo box shorted to some maximum length but keep the complete text stored as item data.
For example,
QComboBox *cb = new QComboBox(this); QString text("Some really really long text that I want to display in the combo box"); QString display_text = Text_Shorten_Function(text); // "Some really ... combo box" cb->addItem(display_text,text); // second parameter is for 'user data' // get displayed text QString displayed_text = cb->currentText(); // get actual text and not what is displayed QString real_text = cb->itemData(cb->currentIndex(),Qt::UserRole);
Another option would be to set a maximum width of the QComboBox. This is sure to work but I find when you do stuff like this you end up with other problems. Preferably this is left as the last option when you exhaust all others.
Note: You can set the tool tip text to show the full string if you think there might be duplicates or problems identifying the contents. If you need to use options like 'findText()' use 'findData()' instead.
-