How to display a list of QPushButton?

北城余情 提交于 2021-02-04 21:08:04

问题


I am a qt noob and I am trying to build an application that needs to display the following:

  • a list of e-mail (which I am displaying in column 1 of a grid layout with a QListWidget1 left list)

  • a list of passwords (which I display in column 2 of grid layout in a QListWidget2 middle list)

Now, I would like to display a list of button (1 for every element of the list) which is supposed to copy the password in the QListWidget2.

Should I just loop in column 3 and add a push button for every row? Or is there a better way of doing this? Is there an object that could help me by laying out the buttons already in line with the elements of my list, so that I can connect them easily with the value of every entry in the list (for copying them later)?

My code is the following:

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))

回答1:


If you want to add a button for each item in the list, you can use setItemWidget.

To get the buttons to align to the right, use a layout as in the demo script below:

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_())


来源:https://stackoverflow.com/questions/20282965/how-to-display-a-list-of-qpushbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!