答案 0 :(得分:0)
不幸的是,您提供了一个演示该问题的示例。
您可以使用sizeAdjustPolicy
和sizePolicy
属性来实现您的想法。
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Main(QtWidgets.QWidget):
def __init__(self):
super(Main, self).__init__()
sheets = [str(i) for i in ("item1",
"item2 item2",
"item3_item3_item3",
"The combobox will always adjust to the contents")]
self.combo = QtWidgets.QComboBox()
self.combo.addItems(sheets)
self.lineEdit = QtWidgets.QLineEdit("Here type a new value for the current setItemText")
self.combo.setSizeAdjustPolicy(self.combo.AdjustToContents)
self.combo.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
QtWidgets.QSizePolicy.Fixed)
self.shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return),
self.combo,
activated=self.onActivated)
layout = QtWidgets.QHBoxLayout()
layout.addWidget(QtWidgets.QLabel("ComboBox:"))
layout.addWidget(self.combo)
layout.addWidget(self.lineEdit)
self.setLayout(layout)
def onActivated(self):
index = self.combo.currentIndex()
self.combo.setEditable(True)
self.combo.setItemText(index, self.lineEdit.text())
self.combo.setEditable(False)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main = Main()
main.resize(600, 200)
main.show()
sys.exit(app.exec_())