1

I am a beginner of PyQGIS and I'm trying to do a QGIS plugin, with QGIS 3.4.7 and PyCharm 2016.3.6. I already did the "dialog" box on Qt and after I wrote on Pycharm. It is a table only with the columns that I want.

I have the CSV file and my code. But when read the file directly on code and trying to write the iteraccion, it's kinda missing something, it means, the variable that belongs to the class QTableWidget (called tbwidget_data_list_service) on the code doesn't show up with .setItem (function and property of QTableWidget class). I have all libraries imported (PyQt5, and others), so I don't understand that's wrong. I have some commented code 'cause I had many attempts to try this working...

Can someone tell me what's wrong or give another example?

enter image description here

Here's the code:

class DataListService(FrameworkSupportConsumer):

    def __init__(self, framework_support):
        super(DataListService, self).__init__()
        self.set_framework_support(framework_support)

        # Set QAction
        icon_path = ":/plugins/marineo_ocean/service_management.png"
        icon_action = QIcon(icon_path)
        message_action = 'Data List Service'
        self.action_data_list_service = QAction(icon_action, message_action, self.iface.mainWindow())

        # Get and register dialog
        self.dlg = DataListServiceDialog(self.iface.mainWindow())
        self.register_dialog('data list service', self.dlg)

        #  ler csv
        dadosaimportar = []
        ruta = "C:/Users/gviana/Desktop/MARINEO_PHASE_1/marineo_1/qgis/profiles/default/python/plugins/marineo_ocean/request.txt"
        reader = csv.reader(open(ruta), delimiter=';')
        for row in reader:
            dadosaimportar.append(row)
        numLinhas = len(dadosaimportar)
        numColunas = 7

        # self.tbwidget_data_list_service = QtWidgets.QTableWidget()


        #self.dlg.tbwidget_data_list_service.setRowCount(numLinhas)
        #self.dlg.tbwidget_data_list_service.setColumnCount(numColunas)

        for linha, umaLinha in enumerate(dadosaimportar):
            for coluna, value in enumerate(umaLinha):
                item = QtWidgets.QTableWidgetItem()
                item.setText(self,value)
                self.dlg.tbwidget_data_list_service.setItem(linha, coluna, item)



        # Connect events
        self.connect_signals()

    def unload(self):
        pass

    def connect_signals(self):
        self.action_data_list_service.triggered.connect(self.show_dialog)
        self.dlg.pushbtn_close_service.clicked.connect(self.close_dialog)


    def download_data(self):
        pass

    def display_data(self):
        pass

    def close_dialog(self):
        self.dlg.close()

    def show_dialog(self):
        self.dlg.show()

1 Answer 1

0

For this I have converted my CSV file to XLSX file as xlrd does not take CSV. Download these:

pip install pandas
pip install openpyxl

read_file = pd.read_csv(r"path where your csv file is stored/Details.csv")
read_file.to_excel(r"path where you want to store xlsx/Details.xlsx", index=None, header=True)

self.anomaly_details = xlrd.open_workbook("./appData/Details.xlsx")
self.sheet = self.anomaly_details.sheet_by_index(0)
self.data = [
    [self.sheet.cell_value(r, c) for c in range(self.sheet.ncols)]
    for r in range(self.sheet.nrows)
]
# print(self.data)
self.alarm_tableWidget.setColumnCount(4)
self.alarm_tableWidget.setRowCount(
    self.sheet.nrows - 1
)  # same no.of rows as of csv file
for row, columnvalues in enumerate(self.data):
    for column, value in enumerate(columnvalues):
        self.item = QtWidgets.QTableWidgetItem(
            str(value)
        )  # str is to also display the integer values
        self.alarm_tableWidget.setItem(row - 1, column, self.item)
        # to set the elements read only
        self.item.setFlags(QtCore.Qt.ItemIsEnabled)

Your Answer

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

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