QComboBox adjust drop down width
Question:
Not sure why I cant get it to work, so maybe one of you will be able to see my mistake… :
combo_type = QComboBox()
combo_type.setMaximumWidth(50)
combo_type.addItems(["TEsst1111","TEsst11111111111111","TEsst1111111111111111111111111"])
combo_type.setStyleSheet('''*
QComboBox QAbstractItemView::item
{
min-width: 6000px;
}
''')
The idea is so that widget in UI has 50 width but when drop down and open I can read the list, sadly the stylesheet override don’t change the drop down width making it 50 and unreadable…
Thanks.
Answers:
Sorted… it was naming error. Correct answer posted below.
combo_type.setStyleSheet('''*
QComboBox QAbstractItemView
{
min-width: 150px;
}
''')
Using the QListView
:
combo_type = QComboBox()
combo_type.SizeAdjustPolicy(QComboBox.AdjustToContentsOnFirstShow)
view = QListView() # creat a ListView
view.setFixedWidth(200) # set the ListView with fixed Width
combo_type.setView(view) # provide the list view to Combobox object
combo_type.setMaximumWidth(500) # will be overwritten by style-sheet
combo_type.addItems(["TEsst1111","TEsst11111111111111","TEsst1111111111111111111111111"])
combo_type.setStyleSheet('''
QComboBox { max-width: 50px; min-height: 40px;}
QComboBox QAbstractItemView::item { min-height: 150px;}
QListView::item:selected { color: red; background-color: lightgray; min-width: 1000px;}"
''')
QComboBox already has a QListView inside it. See bellow:
combo_type = QComboBox()
view = combo_type.view()
view.setFixedWidth(200)
Just adjust "200" to your own value.
Not sure why I cant get it to work, so maybe one of you will be able to see my mistake… :
combo_type = QComboBox()
combo_type.setMaximumWidth(50)
combo_type.addItems(["TEsst1111","TEsst11111111111111","TEsst1111111111111111111111111"])
combo_type.setStyleSheet('''*
QComboBox QAbstractItemView::item
{
min-width: 6000px;
}
''')
The idea is so that widget in UI has 50 width but when drop down and open I can read the list, sadly the stylesheet override don’t change the drop down width making it 50 and unreadable…
Thanks.
Sorted… it was naming error. Correct answer posted below.
combo_type.setStyleSheet('''*
QComboBox QAbstractItemView
{
min-width: 150px;
}
''')
Using the QListView
:
combo_type = QComboBox()
combo_type.SizeAdjustPolicy(QComboBox.AdjustToContentsOnFirstShow)
view = QListView() # creat a ListView
view.setFixedWidth(200) # set the ListView with fixed Width
combo_type.setView(view) # provide the list view to Combobox object
combo_type.setMaximumWidth(500) # will be overwritten by style-sheet
combo_type.addItems(["TEsst1111","TEsst11111111111111","TEsst1111111111111111111111111"])
combo_type.setStyleSheet('''
QComboBox { max-width: 50px; min-height: 40px;}
QComboBox QAbstractItemView::item { min-height: 150px;}
QListView::item:selected { color: red; background-color: lightgray; min-width: 1000px;}"
''')
QComboBox already has a QListView inside it. See bellow:
combo_type = QComboBox()
view = combo_type.view()
view.setFixedWidth(200)
Just adjust "200" to your own value.