I have a Qcombobox, which I want to set a specific width, which is not depending on item width. I have looked around and find some tips and hints only for C++. I do not have any knowledge on that language!!
What I get by first run:
What I want to get by first run:
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.setLayout(QtWidgets.QVBoxLayout())
combo = QtWidgets.QComboBox(self)
self.layout().addWidget(combo)
combo.addItems(["item1", "item2", "item3"])
combo.activated[str].connect(self.onActivatedText)
@QtCore.pyqtSlot(str)
def onActivatedText(self, text):
print(text)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
An ugly solution is:
combo.addItems(["item1 ", "item2 ", "item3 "])
minimumContentsLength : int
This property holds the minimum number of characters that should fit into the combobox.
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setLayout(QtWidgets.QVBoxLayout())
combo = QtWidgets.QComboBox()
self.layout().addWidget(combo)
combo.addItems(["item1", "item2", "item3"])
combo.activated[str].connect(self.onActivatedText)
combo.setMinimumContentsLength(30) # +++
@QtCore.pyqtSlot(str)
def onActivatedText(self, text):
print(text)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())