Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [PyQt] how to transform pyqt4 code to pyqt5 code
#5
(Jan-17-2020, 08:50 PM)atlass218 Wrote: I have another question : how to insert the tablewidget header into the file with this code

copy the first row, set it as headers and delete the first row

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys, csv
  
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
  
class Window(QWidget):
    def __init__(self, rows, columns):
        QWidget.__init__(self)
        self.table = QTableWidget(rows, columns, self)
        for column in range(columns):
            for row in range(rows):
                item = QTableWidgetItem('Text%d' % row)
                self.table.setItem(row, column, item)
            header = QTableWidgetItem("Column " + str(column))
            self.table.setHorizontalHeaderItem(column, header)
        self.buttonOpen = QPushButton('Open', self)
        self.buttonOpen.setFixedWidth(100)
        self.buttonSave = QPushButton('Save', self)
        self.buttonSave.setFixedWidth(100)
        self.buttonOpen.clicked.connect(self.handleOpen)
        self.buttonSave.clicked.connect(self.handleSave)
        layout = QVBoxLayout(self)
        layout.addWidget(self.table)
        empty = QWidget()
        btnGroup = QHBoxLayout()
        btnGroup.addWidget(self.buttonOpen)
        btnGroup.addWidget(self.buttonSave)
        btnGroup.addWidget(empty)
        layout.addLayout(btnGroup)
        
  
    def handleSave(self):
        path,_ = QFileDialog.getSaveFileName(
                self, 'Save File', '', 'CSV(*.csv)')
        if path:
            with open(path, 'w') as stream:
                writer = csv.writer(stream, delimiter='\t')
                for row in range(self.table.rowCount()):
                    rowdata = []
                    for column in range(self.table.columnCount()):
                        item = self.table.item(row, column)
                        if item is not None:
                            rowdata.append(
                                item.text())
                        else:
                            rowdata.append('')
                    writer.writerow(rowdata)
  
    def handleOpen(self):
        path,_ = QFileDialog.getOpenFileName(
                self, 'Open File', '', 'CSV Files(*.csv)')
        if path:
            with open(path, 'r') as stream:
                self.table.setRowCount(0)
                self.table.setColumnCount(0)
                for rowdata in csv.reader(stream, delimiter='\t'):
                    row = self.table.rowCount()
                    self.table.insertRow(row)
                    self.table.setColumnCount(len(rowdata))
                    for column, data in enumerate(rowdata):
                        item = QTableWidgetItem(data)
                        self.table.setItem(row, column, item)

                self.setHeaders()
            
                
    def setHeaders(self):
        row = 0
        for column in range(self.table.columnCount()):
            if self.table.item(0, column).text() == "":
                newItem = QTableWidgetItem(str(column))
                self.table.setHorizontalHeaderItem(column, newItem)
            else:
                newItem = QTableWidgetItem(self.table.item(0, column))
                self.table.setHorizontalHeaderItem(column, newItem)
                    
        self.table.removeRow(0)
        self.table.resizeColumnsToContents()
 
if __name__ == '__main__':
 
    app = QApplication(sys.argv)
    window = Window(10, 5)
    window.setGeometry(20, 20, 640, 480)
    window.show()
    sys.exit(app.exec_())

Quote

Top Page

Messages In This Thread
RE: how to transform pyqt4 code to pyqt5 code - by Axel_Erfurt - Jan-20-2020, 05:45 PM

Possibly Related Threads...
Thread Author Replies Views Last Post
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 524 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  Updating Python version from command prompt and Conversion from PyQt4 to PyQt5 Vysero 4 1,536 Jul-19-2018, 03:15 PM
Last Post: Vysero
  [PyQt] problem with PyQt4 jss15497 0 917 Jan-16-2018, 08:32 AM
Last Post: jss15497
  [PyQt] Learn pyqt5 using pyqt4 jimclay75051 5 2,668 Nov-13-2017, 06:29 AM
Last Post: zykbee

Forum Jump:


Users browsing this thread: 1 Guest(s)