[英]How to use QComboBox to Auto position Content of QCombobox?
这是 QComboBox 和 LineEdit 之间的一点交互。 (我是 python 的新手,主要是我对 javascript 很满意)
我想当我 select 之一的 QComboBox 项目将其效果应用于 QLineEdit 内部的内容时,到目前为止我写了我能写的我不明白为什么没有任何效果
#!/usr/bin/python
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QComboBox, QHBoxLayout, QLineEdit,
QWidget)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout()
combo = QComboBox(self)
combo.addItem('Left')
combo.addItem('Center')
combo.addItem('Right')
combo.activated[str].connect(self.onActivated)
self.qle = QLineEdit(self)
hbox.addWidget(combo)
hbox.setSpacing(20)
hbox.addWidget(self.qle)
self.setLayout(hbox)
self.setWindowTitle('Text alignment')
self.show()
def onActivated(self, text):
if text === Left:
self.qle.setAlignment(Qt.AlignLeft)
elif text === Center:
self.qle.setAlignment(Qt.AlignCenter)
finally:
self.qle.setAlignment(Qt.AlignRight)
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
这是固定的(您的问题只是语法)
使用==
而不是===
和left
而不是left
因为它是一个string
#!/usr/bin/python
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QComboBox, QHBoxLayout, QLineEdit,
QWidget)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout()
combo = QComboBox(self)
combo.addItem('Left')
combo.addItem('Center')
combo.addItem('Right')
combo.activated[str].connect(self.onActivated)
self.qle = QLineEdit(self)
hbox.addWidget(combo)
hbox.setSpacing(20)
hbox.addWidget(self.qle)
self.setLayout(hbox)
self.setWindowTitle('Text alignment')
self.show()
def onActivated(self, text):
if text == 'Left':
self.qle.setAlignment(Qt.AlignLeft)
elif text == 'Center':
self.qle.setAlignment(Qt.AlignCenter)
else:
self.qle.setAlignment(Qt.AlignRight)
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.