[Python] How to manipulate a text file that is displayed in a QListView.

  • I made a PyQt interface where I select a text file on my computer and it is displayed in a QListView.
    Now what I want to do is edit this text file (. Txt) into this QListView (Delete and add data) using python.
    Any idea?

    My code:
    @# -- coding: utf-8 --

    Form implementation generated from reading ui file 'modelo.ui'

    Created: Wed Feb 19 15:04:23 2014

    by: PyQt4 UI code generator 4.10.3

    WARNING! All changes made in this file will be lost!

    from PyQt4 import QtCore, QtGui
    from PyQt4 import QtCore, QtGui
    import sys
    import os
    from PyQt4 import QtCore, QtGui
    from Tkinter import *
    import tkFileDialog
    import Tkconstants
    from PyQt4 import QtGui as qt

    _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
    def _fromUtf8(s):
    return s

    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
    def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig)

    class Ui_Form(object):
    def setupUi(self, Form):
    Form.resize(582, 409)
    self.listView = QtGui.QListView(Form)
    self.listView.setGeometry(QtCore.QRect(10, 40, 561, 351))
    self.splitter = QtGui.QSplitter(Form)
    self.splitter.setGeometry(QtCore.QRect(20, 10, 113, 22))
    self.label = QtGui.QLabel(self.splitter)
    self.toolButton = QtGui.QToolButton(self.splitter)
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../../Users/Dimitrius/Desktop/7547_128x128.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.label.setText(_translate("Form", "Selecionar modelo:", None))
        self.toolButton.setText(_translate("Form", "...", None))

    class myclass(QtGui.QMainWindow):
    def init(self):
    self.ui = Ui_Form()

    criei um atributo que irah mante o nome do arquivo

    self._nome_do_arquivo = ""

         QtCore.QObject.connect(self.ui.toolButton, QtCore.SIGNAL('clicked()'), self.slot1)

    def slot1(self):
    fileName = qt.QFileDialog.getOpenFileName(self, "Open Data File", "", "txt data files (*.txt)")

    guardei o nome do arquivo no atributo

    self._nome_do_arquivo = fileName
    print fileName


    def leitura(self):
    lstModelo = QtGui.QStandardItemModel()

    abri o arquivo

    with open(self._nome_do_arquivo, 'r+') as txt:

    carreguei o conteúdo do arquivo para txt

    for linha in txt:
    # cada linha será impressa
    strItem = QtGui.QStandardItem(linha)
    # print linha

    adiciona o buffer de dados para o componente de tela

         self.ui.listView.setModel( lstModelo )

    if name == 'main':
    import sys
    from PyQt4 import QtCore

    app = QtGui.QApplication(sys.argv)
    av = myclass() 
    lstModelo = QtGui.QStandardItemModel()
    av.ui.listView.setModel( lstModelo )


  • Lifetime Qt Champion


    Do you mean directly edit the file ? Or just update it at some point ?

  • I really want to update it, because sometimes I will need to add or drop a line in the middle file (which will be the case Paths).

  • Lifetime Qt Champion

    Then you can add the add/remove logic to your view plus a save button that will "dump" your model content overwriting the file

  • But what is the code that I can use to remove and save?

  • Lifetime Qt Champion

    removeRow to remove a row of text.

    And to save take the other way of loading, go trough each row of your model and write its content to a QFile

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.