Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Python] How to manipulate a text file that is displayed in a QListView.
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Language Bindings
6 Posts 2 Posters 3.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dimitrius
    wrote on last edited by
    #1

    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

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

    try:
    _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.setObjectName(_fromUtf8("Form"))
    Form.resize(582, 409)
    self.listView = QtGui.QListView(Form)
    self.listView.setGeometry(QtCore.QRect(10, 40, 561, 351))
    self.listView.setObjectName(_fromUtf8("listView"))
    self.splitter = QtGui.QSplitter(Form)
    self.splitter.setGeometry(QtCore.QRect(20, 10, 113, 22))
    self.splitter.setOrientation(QtCore.Qt.Horizontal)
    self.splitter.setObjectName(_fromUtf8("splitter"))
    self.label = QtGui.QLabel(self.splitter)
    self.label.setObjectName(_fromUtf8("label"))
    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)
    self.toolButton.setIcon(icon)
    self.toolButton.setObjectName(_fromUtf8("toolButton"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
    
    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):
    QtGui.QMainWindow.init(self)
    self.ui = Ui_Form()
    self.ui.setupUi(self)

    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
    self.leitura()

      #pass
    

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

    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)
    lstModelo.appendRow(strItem)
    # 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 )
    
    av.show()
    sys.exit(app.exec_())
    

    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dimitrius
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dimitrius
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved