2016-12-27 78 views
0

我已經從一個文件填充了一個Qlistview,文件中的每一行都變成了一行。現在,我想有另一個函數來創建qlistview中所有選中項目的另一個文件。我的列表視圖如下。如何獲取Qlistview中的Checked項目?

def show_list(self, file_in): 
     QListView.__init__(self) 
     QListView.setWindowFlags(self, QtCore.Qt.WindowStaysOnTopHint) 
     QListView.setWindowTitle(self, "ListView") 
     self.buttonBox = QtGui.QDialogButtonBox(self) 
     self.buttonBox.setOrientation(QtCore.Qt.Horizontal) 
     self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) 
     list_view = QListView(self) 
     list_view.setMinimumSize(350,350) 

     self.verticalLayout = QtGui.QVBoxLayout(self) 
     self.verticalLayout.addWidget(list_view) 
     self.verticalLayout.addWidget(self.buttonBox) 
     self.buttonBox.accepted.connect(self.close) 
     self.buttonBox.rejected.connect(self.close) 
     model = QStandardItemModel(list_view) 
     with open(file_in) as f: 
      if f is not None: 
       item = f.readlines() 
      for line in item: 
       item = QStandardItem(line) 
       item.setCheckable(True) 
       item.setCheckState(QtCore.Qt.Unchecked) 
       model.appendRow(item) 

     list_view.setModel(model) 
     list_view.show() 

這是我迄今爲止的嘗試得到我想要的結果。不幸的是,它不打印我的檢查項目。當被調用像這樣self.print_checked_items(model)我想知道什麼可能是錯的?

def print_checked_items(self, model): 
    path = "/home/test1/checked.txt" 
    for index in range(model.rowCount()): 
     item = model.item(index) 
     if item.isCheckable() and item.checkState() == QtCore.Qt.Checked: 
      with open(path, "a") as f_out: 
       print ('%s\n' % item.text()) 
       f_out.write('%s\n' % item.text() 
+0

你的意思是你想知道用戶UN /檢查列表中的項目時,或者更確切地說,查找被選中所有列表項? – Schollii

+0

不,我希望在用戶檢查後從列表中選擇所有已檢查項目的列表。 – answerSeeker

+0

編寫一個查看列表項目並檢查其選中狀態的函數。發佈代碼,然後我們可以提供幫助。 – Schollii

回答

2
import sys 
from PyQt4 import QtGui 
from PyQt4 import QtCore 


class AppRemovalPage(QtGui.QWizardPage): 
    def __init__(self, parent=None): 
     super(AppRemovalPage, self).__init__(parent=parent) 
     self.setTitle('Apps to Remove') 
     self.setSubTitle('Listview') 
     self.list_view = QtGui.QListView(self) 
     self.list_view.setMinimumSize(465, 200) 
     layout = QtGui.QVBoxLayout(self) 
     layout.addWidget(self.list_view) 
     self.setLayout(layout) 
     self.items = [] 
     self.isWritten = False 

     loo = "/home/test1/file.txt" 

     self.model = QtGui.QStandardItemModel(self.list_view) 

     self.model.itemChanged.connect(self.setItems) 

     file = QtCore.QFile(loo) 
     if file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text): 
      while not file.atEnd(): 
       line = bytearray(file.readLine()).decode().strip() 
       item = QtGui.QStandardItem(line) 
       item.setCheckable(True) 
       item.setCheckState(QtCore.Qt.Unchecked) 
       self.model.appendRow(item) 
     self.list_view.setModel(self.model) 
     self.list_view.show() 

    def setItems(self, item): 
     if item.checkState() == QtCore.Qt.Checked: 
      self.items.append(item) 
     if item.checkState() == QtCore.Qt.Unchecked: 
      self.items.remove(item) 

    def print_checked_items(self): 
     path = "/home/test1/checked.txt" 
     mode = QtCore.QFile.Append if self.isWritten else QtCore.QFile.WriteOnly 
     if len(self.items) > 0: 
      file = QtCore.QFile(path) 
      if file.open(mode): 
       for item in self.items: 
        print('%s' % item.text()) 
        file.write(item.text() + "\n") 
      file.close() 
     print("print checked items executed") 

if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    wizard = QtGui.QWizard() 

    appremoval = AppRemovalPage() 
    wizard.addPage(appremoval) 
    wizard.addPage(QtGui.QWizardPage()) 

    wizard.button(QtGui.QWizard.NextButton).clicked.connect(appremoval.print_checked_items) 
    wizard.show() 
    sys.exit(app.exec_()) 

enter image description here

輸出:

a 
d 
e 
print checked items executed 
+0

再次感謝男人,你是最棒的!我只需將'file.write(item.text())'改爲'file.write('%s \ n'%item.text())'就可以工作。我想我應該使用'QtCore.QFile.'來編寫文件 – answerSeeker

+1

@TakakaiWasumi更新我的解決方案 – eyllanesc

+0

除非您需要Qt類的特定功能,否則您應該優先考慮Python類。在你的代碼中,Python文件對象也可以工作,請參閱我的更新答案。而且,lambda是不必要的。 – Schollii

3

當我運行這在Python 3.5和PyQt5,它工作得很好,它打印正確的模式和選定項目。我刪除了文件讀/寫行以進行測試。對於PyQt4和Python 2.7,您只需修復一些導入和打印語句。運行它,勾選一些項目,5秒後你在控制檯中看到了什麼?

from PyQt5 import QtCore 

from PyQt5 import QtGui 
from PyQt5.QtCore import QTimer 
from PyQt5.QtWidgets import QApplication, QWizardPage, QListView 


class AppRemovalPage(QWizardPage): 
    def __init__(self, parent): 
     super(AppRemovalPage, self).__init__(parent) 
     self.setTitle('Apps to Remove') 
     self.setSubTitle('Listview') 
     self.list_view = QListView(self) 
     self.list_view.setMinimumSize(465, 200) 
     self.isWritten = False 
     loo = "/home/test1/file.txt" 

     self.model = QtGui.QStandardItemModel(self.list_view) 
     for line in ('a', 'b', 'c', 'd', 'e'): 
      self.item = QtGui.QStandardItem(line) 
      self.item.setCheckable(True) 
      self.item.setCheckState(QtCore.Qt.Unchecked) 
      self.model.appendRow(self.item) 

     self.list_view.setModel(self.model) 
     self.list_view.show() 


    def print_checked_items(self): 
     for index in range(self.model.rowCount()): 
      item = self.model.item(index) 
      if item.checkState() == QtCore.Qt.Checked: 
       if self.isWritten: 
        mode = "a" 
       else: 
        mode = "w" 
        self.isWritten = True 
       print ('%s' % item.text()) 

     print("print checked items executed") 


app = QApplication([]) 
listview = AppRemovalPage(None) 
listview.show() 
QTimer.singleShot(5000, listview.print_checked_items) 
app.exec_() 

如果我對號a,c和d我看到:

a w 
c a 
d a 
print checked items executed 

更新展示如何Python的文件對象也可以(實際上是在更好的代碼,因爲它支持使用情境管理):

​​3210

連接可以寫成wizard.button(QWizard.NextButton).clicked.connect(appremoval.print_checked_items)

+0

它的工作方式,但我試圖創建一個文件,它doesn不打印任何東西,但「打印檢查項目執行」這就是令我困惑的原因 – answerSeeker

+1

@TakakaiWasumi所以你的問題與PyQt無關,這是純粹的Python問題。我更新了答案,儘管已經接受了另一個答案,表明不需要QFile(至少,在Python 3.5/PyQt 5.5/Windows 7平臺上工作)。 – Schollii

相關問題