我是qt noob,我正在尝试构建一个需要显示以下内容的应用程序:
电子邮件列表(我在网格布局的第1列中显示) 使用QListWidget1左侧列表)
密码列表(我在QListWidget2中间列表的网格布局第2列中显示)
现在,我想显示一个按钮列表(列表的每个元素都有1个),它应该在QListWidget2中复制密码。
我应该在第3列中循环并为每一行添加一个按钮吗?或者有更好的方法吗?是否有一个对象可以通过布置已经与列表元素一致的按钮来帮助我,这样我就可以轻松地将它们与列表中每个条目的值连接起来(以后再复制它们)?
我的代码如下:
from PySide import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(311, 499)
self.gridLayoutWidget = QtGui.QWidget(Dialog)
self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 291, 371))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
# self.dockWidget = QtGui.QDockWidget(self.gridLayoutWidget)
# self.dockWidget.setObjectName("dockWidget")
# self.dockWidgetContents = QtGui.QWidget()
# self.dockWidgetContents.setObjectName("dockWidgetContents")
# self.dockWidget.setWidget(self.dockWidgetContents)
# self.gridLayout.addWidget(self.dockWidget, 0, 0, 1, 1)
self.leftList = QtGui.QListWidget(self.gridLayoutWidget)
self.gridLayout.addWidget(self.leftList, 0, 0, 1, 1)
self.middleList = QtGui.QListWidget(self.gridLayoutWidget)
self.gridLayout.addWidget(self.middleList, 0, 1, 1, 1)
self.rightList = QtGui.QListWidget(self.gridLayoutWidget)
self.gridLayout.addWidget(self.rightList, 0, 2, 1, 1)
self.progressBar = QtGui.QProgressBar(Dialog)
self.progressBar.setGeometry(QtCore.QRect(10, 410, 231, 23))
self.progressBar.setInputMethodHints(QtCore.Qt.ImhNone)
self.progressBar.setMaximum(30)
self.progressBar.setProperty("value", 30)
self.progressBar.setInvertedAppearance(False)
self.progressBar.setTextDirection(QtGui.QProgressBar.TopToBottom)
self.progressBar.setObjectName("progressBar")
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(240, 413, 51, 16))
self.label.setObjectName("label")
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(10, 440, 291, 51))
self.pushButton.setObjectName("pushButton")
self.line = QtGui.QFrame(Dialog)
self.line.setGeometry(QtCore.QRect(10, 390, 291, 20))
self.line.setFrameShape(QtGui.QFrame.HLine)
self.line.setFrameShadow(QtGui.QFrame.Sunken)
self.line.setObjectName("line")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.progressBar.setFormat(QtGui.QApplication.translate("Dialog", "%vs", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Dialog", "TimeOut", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Refresh", None, QtGui.QApplication.UnicodeUTF8))
答案 0 :(得分:1)
如果要为列表中的每个项目添加按钮,可以使用setItemWidget。
要使按钮向右对齐,请使用下面演示脚本中的布局:
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.list = QtGui.QListWidget(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.list)
def addListItem(self, text):
item = QtGui.QListWidgetItem(text)
self.list.addItem(item)
widget = QtGui.QWidget(self.list)
button = QtGui.QToolButton(widget)
layout = QtGui.QHBoxLayout(widget)
layout.setContentsMargins(0, 0, 0, 0)
layout.addStretch()
layout.addWidget(button)
self.list.setItemWidget(item, widget)
button.clicked[()].connect(
lambda: self.handleButtonClicked(item))
def handleButtonClicked(self, item):
print(item.text())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
for label in 'red blue green yellow purple'.split():
window.addListItem(label)
window.setGeometry(500, 300, 300, 200)
window.show()
sys.exit(app.exec_())