如何在PyQt4中加载和保存数据?
How to load and save data in PyQt4?
我正在尝试编写一个应用程序来处理 QTableWidget 的数据保存和加载。
我想写一个更复杂的应用程序,所以,我用了很多pyqt 类来定义不同的页面。
这是代码:
import sys
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self)
load_file = QtGui.QAction("Load", self)
load_file.triggered.connect(self.loadFile)
save_file = QtGui.QAction("Save", self)
save_file.triggered.connect(self.saveFile)
menubar = self.menuBar()
file = menubar.addMenu('&File')
file.addAction(load_file)
file.addAction(save_file)
table = Table()
self.setCentralWidget(table)
def loadFile(self):
load_file = QtGui.QFileDialog.getOpenFileName(self, "Open file", "./", "All files(*)")
if load_file:
with open(load_file, "r") as load_data:
data = eval(load_data.read())
Table().filling(data)
def saveFile(self):
save_file = QtGui.QFileDialog.getSaveFileName(self, "Save file", "./", "All files(*)")
if save_file:
with open(save_file, "w") as save_data:
save_data.write(repr(DATA))
class Table(QtGui.QTableWidget):
def __init__(self, parent = None):
QtGui.QTableWidget.__init__(self)
self.setRowCount(4)
self.setColumnCount(2)
self.itemChanged.connect(self.getData)
def getData(self):
data = []
for row in range(4):
row_data = []
for col in range(2):
if self.item(row, col):
text = self.item(row, col).text()
row_data.append(str(text))
else:
row_data.append("")
data.append(row_data)
global DATA
DATA = data
def filling(self, data):
for row in range(4):
for col in range(2):
new_item = QtGui.QTableWidgetItem("")
self.setItem(row, col, new_item)
self.item(row, col).setText(data[row][col])
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
应用程序可以在QTableWidget中保存数据过滤器,但无法显示加载的数据。
我想知道是什么问题。
问题是因为在调用Table().filling()
时你正在创建和填充另一个table,使class的table成员是合适的。
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
[...]
self.table = Table(self)
self.setCentralWidget(self.table)
def loadFile(self):
load_file = QtGui.QFileDialog.getOpenFileName(self, "Open file", "./", "All files(*)")
if load_file:
with open(load_file, "r") as load_data:
data = eval(load_data.read())
print(data)
self.table.filling(data)
[...]
我正在尝试编写一个应用程序来处理 QTableWidget 的数据保存和加载。
我想写一个更复杂的应用程序,所以,我用了很多pyqt 类来定义不同的页面。 这是代码:
import sys
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self)
load_file = QtGui.QAction("Load", self)
load_file.triggered.connect(self.loadFile)
save_file = QtGui.QAction("Save", self)
save_file.triggered.connect(self.saveFile)
menubar = self.menuBar()
file = menubar.addMenu('&File')
file.addAction(load_file)
file.addAction(save_file)
table = Table()
self.setCentralWidget(table)
def loadFile(self):
load_file = QtGui.QFileDialog.getOpenFileName(self, "Open file", "./", "All files(*)")
if load_file:
with open(load_file, "r") as load_data:
data = eval(load_data.read())
Table().filling(data)
def saveFile(self):
save_file = QtGui.QFileDialog.getSaveFileName(self, "Save file", "./", "All files(*)")
if save_file:
with open(save_file, "w") as save_data:
save_data.write(repr(DATA))
class Table(QtGui.QTableWidget):
def __init__(self, parent = None):
QtGui.QTableWidget.__init__(self)
self.setRowCount(4)
self.setColumnCount(2)
self.itemChanged.connect(self.getData)
def getData(self):
data = []
for row in range(4):
row_data = []
for col in range(2):
if self.item(row, col):
text = self.item(row, col).text()
row_data.append(str(text))
else:
row_data.append("")
data.append(row_data)
global DATA
DATA = data
def filling(self, data):
for row in range(4):
for col in range(2):
new_item = QtGui.QTableWidgetItem("")
self.setItem(row, col, new_item)
self.item(row, col).setText(data[row][col])
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
应用程序可以在QTableWidget中保存数据过滤器,但无法显示加载的数据。
问题是因为在调用Table().filling()
时你正在创建和填充另一个table,使class的table成员是合适的。
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
[...]
self.table = Table(self)
self.setCentralWidget(self.table)
def loadFile(self):
load_file = QtGui.QFileDialog.getOpenFileName(self, "Open file", "./", "All files(*)")
if load_file:
with open(load_file, "r") as load_data:
data = eval(load_data.read())
print(data)
self.table.filling(data)
[...]