【问题标题】:Save & Load QListItems when I'm running my program.运行程序时保存并加载 QListItems。
【发布时间】:2014-09-28 17:57:37
【问题描述】:

我是 Python 和一般编码的新手,这实际上是我迄今为止的第一个项目。 我在 PYQT 中创建了一个 GUI 并将其转换为 Py。 以下代码在下图中创建页面;

self.ListView_InsertCoin = QtGui.QListWidget(self.page_EditMarket)
    self.ListView_InsertCoin.setGeometry(300,250,220,150)
    self.ListView_InsertCoin.setStyleSheet(_fromUtf8("background-color: white"))
    self.ListView_InsertCoin.clicked.connect(self.Action_SelectCoinFromList)
    self.ListView_InsertCoin.sortItems(QtCore.Qt.AscendingOrder)

    self.Pushbutton_AddCoin = QtGui.QPushButton(self.page_EditMarket)
    font = QtGui.QFont()
    font.setPointSize(7)
    font.setBold(True)
    font.setWeight(75)
    self.Pushbutton_AddCoin.setFont(font)
    self.Pushbutton_AddCoin.setGeometry(420,190,100,25)
    self.Pushbutton_AddCoin.setStyleSheet('Background-color: white')
    self.Pushbutton_AddCoin.setText('Add Coin')
    self.Pushbutton_AddCoin.setEnabled(False)
    self.Pushbutton_AddCoin.clicked.connect(self.Action_PushButton_AddCoin)

    self.Lineedit_InsertCoin = QtGui.QLineEdit(self.page_EditMarket)
    self.Lineedit_InsertCoin.setGeometry(300,190,110,25)
    self.Lineedit_InsertCoin.setStyleSheet(_fromUtf8("background-color: white"))
    self.Lineedit_InsertCoin.setAlignment(QtCore.Qt.AlignHCenter)
    self.Lineedit_InsertCoin.setPlaceholderText('Insert Symbol')
    self.Lineedit_InsertCoin.textEdited.connect(self.Action_LineEdit_TextEdit)


    self.Pushbutton_DeleteCoin = QtGui.QPushButton(self.page_EditMarket)
    font = QtGui.QFont()
    font.setPointSize(7)
    font.setBold(True)
    font.setWeight(75)
    self.Pushbutton_DeleteCoin.setFont(font)
    self.Pushbutton_DeleteCoin.setGeometry(420,220,100,25)
    self.Pushbutton_DeleteCoin.setStyleSheet('Background-color: white')
    self.Pushbutton_DeleteCoin.setText('Delete Coin')
    self.Pushbutton_DeleteCoin.setEnabled(False)
    self.Pushbutton_DeleteCoin.clicked.connect(self.Action_PushButton_DeleteCoin)

    self.Label_DeleteCoin = QtGui.QLabel(self.page_EditMarket)
    self.Label_DeleteCoin.setText('Select to Delete')
    self.Label_DeleteCoin.setGeometry(300,220,110,25)
    self.Label_DeleteCoin.setStyleSheet('background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:1 rgba(255, 255, 255, 0)); \n Color: White')


    self.Pushbutton_SaveList = QtGui.QPushButton(self.page_EditMarket)
    font = QtGui.QFont()
    font.setPointSize(7)
    font.setBold(True)
    font.setWeight(75)
    self.Pushbutton_SaveList.setFont(font)
    self.Pushbutton_SaveList.setGeometry(300,410,220,30)
    self.Pushbutton_SaveList.setStyleSheet(_fromUtf8("background-color: qlineargradient(spread:repeat, x1:0.506, y1:1, x2:0.499591, y2:0, stop:0.00568182 rgba(124, 143, 153, 255), stop:0.238636 rgba(103, 138, 158, 255), stop:0.931818 rgba(133, 131, 153, 232), stop:1 rgba(99, 111, 131, 255));\n"
"color: rgb(255, 255, 255);"))
        self.Pushbutton_SaveList.setText('Save my List')
        self.Pushbutton_SaveList.setIcon(QtGui.QIcon('./Icons/Icon_ExitProgram.png'))
        self.Pushbutton_SaveList.clicked.connect(self.Action_PushButton_SaveMyList)
        self.Pushbutton_SaveList.setEnabled(False)

def Action_SelectCoinFromList(self):
        self.Pushbutton_DeleteCoin.setStyleSheet(_fromUtf8("background-color: qlineargradient(spread:repeat, x1:0.506, y1:1, x2:0.499591, y2:0, stop:0.00568182 rgba(124, 143, 153, 255), stop:0.238636 rgba(103, 138, 158, 255), stop:0.931818 rgba(133, 131, 153, 232), stop:1 rgba(99, 111, 131, 255));\n"
"color: rgb(255, 255, 255);"))
        self.Pushbutton_DeleteCoin.setEnabled(True)
        self.Lineedit_InsertCoin.clear()
        self.Pushbutton_AddCoin.setEnabled(False)
        self.Pushbutton_AddCoin.setStyleSheet("Background-color: white")

    def Action_LineEdit_TextEdit(self):
        self.Pushbutton_AddCoin.setStyleSheet(_fromUtf8("background-color: qlineargradient(spread:repeat, x1:0.506, y1:1, x2:0.499591, y2:0, stop:0.00568182 rgba(124, 143, 153, 255), stop:0.238636 rgba(103, 138, 158, 255), stop:0.931818 rgba(133, 131, 153, 232), stop:1 rgba(99, 111, 131, 255));\n"
"color: rgb(255, 255, 255);"))
        self.Pushbutton_AddCoin.setEnabled(True)
        self.Pushbutton_DeleteCoin.setEnabled(False)
        self.Pushbutton_DeleteCoin.setStyleSheet("Background-color: white")

    def Action_PushButton_AddCoin(self):
        UserInput = unicode(self.Lineedit_InsertCoin.text())
        self.ListView_InsertCoin.addItem(UserInput)
        self.Lineedit_InsertCoin.clear()
        self.Pushbutton_AddCoin.setStyleSheet('Background-color: White')
        self.Pushbutton_AddCoin.setEnabled(False)
        self.Pushbutton_SaveList.setEnabled(True)
        self.Pushbutton_SaveList.setStyleSheet(_fromUtf8("background-color: qlineargradient(spread:repeat, x1:0.506, y1:1, x2:0.499591, y2:0, stop:0.00568182 rgba(124, 143, 153, 255), stop:0.238636 rgba(103, 138, 158, 255), stop:0.931818 rgba(133, 131, 153, 232), stop:1 rgba(99, 111, 131, 255));\n"
"color: rgb(255, 255, 255);"))
        self.Pushbutton_SaveList.setEnabled(True)
        self.ListView_InsertCoin.sortItems(QtCore.Qt.AscendingOrder)

    def Action_PushButton_DeleteCoin(self):
        User_Selection = self.ListView_InsertCoin.takeItem(self.ListView_InsertCoin.currentRow())
        User_Selection = None
        self.Pushbutton_DeleteCoin.setStyleSheet('background-color: white')
        self.Pushbutton_DeleteCoin.setEnabled(False)
        self.Pushbutton_SaveList.setStyleSheet(_fromUtf8("background-color: qlineargradient(spread:repeat, x1:0.506, y1:1, x2:0.499591, y2:0, stop:0.00568182 rgba(124, 143, 153, 255), stop:0.238636 rgba(103, 138, 158, 255), stop:0.931818 rgba(133, 131, 153, 232), stop:1 rgba(99, 111, 131, 255));\n"
"color: rgb(255, 255, 255);"))
        self.Pushbutton_SaveList.setEnabled(True)
        self.ListView_InsertCoin.sortItems(QtCore.Qt.AscendingOrder)

    def Action_PushButton_SaveMyList(self):
        self.stackedWidget.setCurrentIndex(0)
        self.Pushbutton_SaveList.setEnabled(False)

Image here

我可以使用按钮从 Qlistwidget 添加和删除条目。

QlistItems 最终将用作调用 API 的字符串。

我希望能够使用“Pushbutton_SaveList”保存我的 QListItem,而不是每次运行程序时都向列表中添加新项目。我还希望这些保存的项目在我运行程序后自动加载。

我检查了following thread,但我无法理解它。有人能指出我正确的方向吗?

提前致谢。

【问题讨论】:

    标签: python user-interface pyqt qlistwidget


    【解决方案1】:

    您必须以某种方式创建自己的存储列表菜单文件来保存和加载它。如果应用程序启动,您会将列表菜单文件加载到您的QListWidget。并且,如果应用程序已退出或退出,您会将QListWidget 的列表日期保存到文件中。

    好的,我有例子,希望对你有所帮助;

    import sys
    import datetime
    from PyQt4 import QtCore, QtGui
    
    FILENAME = 'listMenu.txt'
    
    class AppQWidget (QtGui.QWidget):
        def __init__ (self):
            super(AppQWidget, self).__init__()
            self.allQVBoxLayout    = QtGui.QVBoxLayout()
            self.myQListWidget     = QtGui.QListWidget(self)
            self.addQPushButton    = QtGui.QPushButton('ADD', self)
            self.deleteQPushButton = QtGui.QPushButton('DELETE', self)
            self.allQVBoxLayout.addWidget(self.myQListWidget)
            self.allQVBoxLayout.addWidget(self.addQPushButton)
            self.allQVBoxLayout.addWidget(self.deleteQPushButton)
            self.setLayout(self.allQVBoxLayout)
            self.connect(self.addQPushButton, QtCore.SIGNAL('released()'), self.addListMenu)
            self.connect(self.deleteQPushButton, QtCore.SIGNAL('released()'), self.deleteListMenu)
            self.loadMenu()
    
        def addListMenu (self, menu = None):
            if menu == None:
                menu = 'MENU : ' + str(datetime.datetime.now())
            self.myQListWidget.addItem(menu)
            self.myQListWidget.sortItems(QtCore.Qt.AscendingOrder)
    
        def deleteListMenu (self):
            currentRow = self.myQListWidget.currentRow()
            if currentRow >= 0:
                self.myQListWidget.takeItem(currentRow)
                self.myQListWidget.sortItems(QtCore.Qt.AscendingOrder)
    
        def closeEvent (self, eventQCloseEvent):
            self.saveMenu()
            eventQCloseEvent.accept()
    
        def loadMenu (self):
            configFile = open(FILENAME, 'r')
            allLine = configFile.readlines()
            configFile.close()
            for currentLine in allLine:
                self.addListMenu(currentLine[:-1])
    
        def saveMenu (self):
            configFile = open(FILENAME, 'w')
            allRow = self.myQListWidget.count()
            for row in range(allRow):
                menu = str(self.myQListWidget.item(row).text())
                configFile.write(menu + '\n')
            configFile.close()
    
    app = QtGui.QApplication([])
    window = AppQWidget()
    window.show()
    sys.exit(app.exec_())
    

    QListWidget 参考http://pyqt.sourceforge.net/Docs/PyQt4/qlistwidget.html


    问候,

    【讨论】:

    • 这太棒了。我会处理这个,看看我是否可以从这里构建。非常感谢您的宝贵时间。
    猜你喜欢
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2017-02-03
    • 1970-01-01
    相关资源
    最近更新 更多