0

I know there are a lot of similar questions... but I can't implement them.

How can I load a CSV file and write that into a QTableView?

My first step was to browse it.

 def select_input_file(self):
 filename, _filter = QFileDialog.getOpenFileName(
 self.dlg, "Select input file ","", 
 ("GeoPackages (*.gpkg);;Comma separated values (*.csv);;Shapefiles (*.shp)")
 )
 self.dlg.Input_Box.setText(filename) 

In the next step, I want to load it and write it in the QTableView called table.

 def show(self):
 self.table = QtGui.QStandardItemModel(self)
 filename = self.dlg.Input_Box.text()
 with open(filename, "r") as fileInput:
 for row in csv.reader(fileInput): 
 items = [
 QtGui.QStandardItem(field)
 for field in row
 ]
 self.table.appendRow(items)

But I get this error:

 self.table = QtGui.QStandardItemModel(self)
 TypeError: arguments did not match any overloaded call:
 QStandardItemModel(parent: QObject = None): argument 1 has unexpected type 'GTool'
 QStandardItemModel(int, int, parent: QObject = None): argument 1 has unexpected type 'GTool'
 

These packages are loaded:

from PyQt5 import QtCore, QtGui, QtWidgets
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QApplication, QWidget, QTableWidget, QTableWidgetItem, QFileDialog, QDialog, QPushButton, QDialogButtonBox, QLineEdit
from qgis.core import QgsProject, Qgis
from qgis.utils import iface
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 12, 2021 at 15:58
3
  • We come to GIS SE. As a new user, please take the Tour, which emphasizes the importance of asking One question per Question. If the title has an "and" you're likely to get unfocused closure votes. Please Edit the Question to isolate the task at issue. Commented Dec 12, 2021 at 16:49
  • This is really a programming question, not a GIS question, so it probably be somewhere else, like stackoverflow. That said, your problem is that you are trying to create your table as a QStandardItemModel, when you said you were going to use QTableView Commented Dec 12, 2021 at 17:53
  • If there are a lot of similar questions please provide links to at least some of them within the body of your question. Commented Dec 12, 2021 at 18:18

1 Answer 1

1

Considering the file https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv, you can use the following

import csv
filename, _filter = QFileDialog.getOpenFileName(
 iface.mainWindow(), "Select input file ","", 
 ("Comma separated values (*.csv);;GeoPackages (*.gpkg);;Shapefiles (*.shp)")
)
model = QStandardItemModel()
tableView = QTableView()
tableView.setModel(model)
tableView.horizontalHeader().setStretchLastSection(True)
with open(filename, "r") as fileInput:
 for i, row in enumerate(csv.reader(fileInput)):
 if i == 0:
 model.setHorizontalHeaderLabels([r.strip().strip('"') for r in row])
 else:
 items = [
 QStandardItem(field.strip())
 for field in row
 ]
 model.appendRow(items)
tableView.show()
answered Dec 13, 2021 at 1:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.