Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Portuguese
  4. Abrir um arquivo .txt dentro do Qt em uma QlistView utilizando python.
Forum Update on Monday, May 27th 2025

Abrir um arquivo .txt dentro do Qt em uma QlistView utilizando python.

Scheduled Pinned Locked Moved Portuguese
5 Posts 2 Posters 3.1k 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

    Olá pessoal!
    Fiz um pequeno programa que abre um QFileDialog e pede para selecionar um arquivo. Eu desejo selecionar um arquivo .txt e abrir dentro de uma QlistView, tudo isso em python.
    O meu programa só abre a janela e seleciona o arquivo, mas eu não sei como abri-lo dentro do Qt. Ele está assim:
    from PyQt4 import QtCore, QtGui
    from Tkinter import *
    import tkFileDialog
    import Tkconstants

    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)

    def slot1():
    root = Tk()
    root.fileName = tkFileDialog.askopenfilename( filetypes = ( ("some", ".txt"),("All files", ".*") ) )

    f = open(root.fileName, "r")
    print f.readlines()
    
    #pass
    

    class Ui_Form(object):
    def setupUi(self, Form):
    Form.setObjectName(_fromUtf8("Form"))
    Form.resize(509, 308)
    self.label = QtGui.QLabel(Form)
    self.label.setGeometry(QtCore.QRect(30, 30, 71, 16))
    self.label.setObjectName(_fromUtf8("label"))
    self.toolButton = QtGui.QToolButton(Form)
    self.toolButton.setGeometry(QtCore.QRect(110, 30, 25, 19))
    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.treeWidget = QtGui.QTreeWidget(Form)
    self.treeWidget.setGeometry(QtCore.QRect(25, 80, 301, 192))
    self.treeWidget.setObjectName(_fromUtf8("treeWidget"))

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.toolButton, QtCore.SIGNAL(_fromUtf8("clicked()")), slot1)
        QtCore.QMetaObject.connectSlotsByName(Form)
    
    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.label.setText(_translate("Form", "Abrir Modelo:", None))
        self.toolButton.setText(_translate("Form", "...", None))
        self.treeWidget.headerItem().setText(0, _translate("Form", "Modelo:", None))
    

    if name == "main":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TioRoy
      wrote on last edited by
      #2

      Você quer abrir o dialog do Qt, em vez do Tk, ou quer saber como criar o QListView?

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

        o Dialog eu abri em:
        def slot1(): root = Tk() root.fileName = tkFileDialog.askopenfilename( filetypes = ( (“some”, “.txt”),(“All files”, “.*”) ) )
        agora eu quero saber como abrir o arquivo (.txt) dentro do Qt quando eu selecioná-lo na janela QFileDialog

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TioRoy
          wrote on last edited by
          #4

          Conheço pouco de Python, mas fiz um teste aqui com o PySide. Não usei o dialog do Tk (tkFileDialog) e sim o do Qt:
          @
          fileName, _ = QFileDialog.getOpenFileName(None, "Abre arquivo", ".", "Todos os arquivos(.)");

          qFile = QFile(fileName);

          if (qFile.open(QFile.ReadOnly)) :
          stream = QTextStream(qFile)
          conteudo = stream.readAll()
          print conteudo;
          @

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

            Não to conseguindo inserir este código que você enviou no meu acima.
            O código que mostrei, foi gerado pelo pyiuc4 e eu coloquei aquela função slot1() lá.

            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