QComboBox width would changed when it setstylesheet
-
When i do not setstylesheet to the widget the qcombobox looks ok.like this.
This is the designer picture.
The code:QtGuiApplication::QtGuiApplication(QWidget *parent) : QWidget(parent) { ui.setupUi(this); ui.comboBox->addItem("xxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"); ui.comboBox->setCurrentIndex(-1); }
But if I setstylesheet, it will change.
QtGuiApplication::QtGuiApplication(QWidget *parent) : QWidget(parent) { ui.setupUi(this); ui.comboBox->addItem("xxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"); ui.comboBox->setCurrentIndex(-1); setStyleSheet("QPushButton {background-color:black;}"); }
The qcombobox width change to fit the item text width.
The Qt version I used is 5.9.7.
Any help is apprecitate! -
Hi,
Try QComboBox::AdjustToMinimumContentsLengthWithIcon, because the default value is
AdjustToContentsOnFirstShow
, so the combo box stretching that way, is expected.You can do that using code, or through Qt Designer:
-
QComboBox has a SizeAdjustPolicy. But, I haven't found any value for this policy I would expect to help.
In this very specific case you might want to set the style sheet only on the push button:
ui.pushButton->setStyleSheet("background-color:black;");
. It doesn't help, though, if you plan on adjusting more things globally with a stylesheet. -
Hi,
Try QComboBox::AdjustToMinimumContentsLengthWithIcon, because the default value is
AdjustToContentsOnFirstShow
, so the combo box stretching that way, is expected.You can do that using code, or through Qt Designer:
-
@SimonSchroeder Hi, this example is used to simulate my actual usage scenario.
-
@Abderrahmene_Rayene Oh!!!!It works!!!Thank you so much!!!
-