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

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 = [
                for field in row

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
  • 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.
    – Vince
    Dec 12 '21 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
    – Llaves
    Dec 12 '21 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.
    – PolyGeo
    Dec 12 '21 at 18:18

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


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])
            items = [
                for field in row


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.