如何读取和写入文本文件日期输入到 QTableView?
这就是我所拥有的,但我想在将数据添加到表时保存数据,并且当然能够在重新打开应用程序时将其读回。有什么教程可以参考吗?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QStandardItemModel();
model->setRowCount(0);
ui->tableView->setModel(model);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QStandardItem *userName = new QStandardItem(ui->lineEdit_Name->text());
QStandardItem *userNumber = new QStandardItem(ui->lineEdit_Number->text());
QList<QStandardItem*> row;
row <<userName << userNumber;
model->appendRow(row);
}
非常感谢
编辑 - - - - - - - - - - - - - - - -
这对我有用:
添加功能:
void MainWindow::on_pushButto_Add_clicked() {
QStandardItem *userInput = new QStandardItem(ui->lineEdit->text());
QStandardItem *userInput2= new QStandardItem(ui->lineEdit_2->text());
QList<QStandardItem*> row;
row <<userInput << userInput2;
model->appendRow(row);
}
保存功能:
void MainWindow::on_pushButton_Save_clicked()
{
QFile file("C:/Users/UserName/Practicing/Resources_Files/someFile.bin");
if (file.open(QIODevice::WriteOnly))
{
QDataStream stream(&file);
qint32 n = model->rowCount();
qint32 m = model->columnCount();
stream << n << m;
for (int i=0; i<n; ++i)
{
for (int j=0; j<m; j++)
{
model->item(i,j)->write(stream);
}
}
file.close();
}
}
加载功能:
void MainWindow::on_pushButton_Load_clicked()
{
QFile file("C:/Users/UserName/Practicing/Resources_Files/someFile.bin");
if (file.open(QIODevice::ReadOnly))
{
QDataStream stream(&file);
qint32 n, m;
stream >> n >> m;
model->setRowCount(n);
model->setColumnCount(m);
for (int i = 0; i < n ; ++i) {
for (int j = 0; j < m; j++) {
QStandardItem *item = new QStandardItem;
item->read(stream);
model->setItem(i, j, item);
}
}
file.close();
}
}