案例介绍:使用tableWidget存储文件路径。
要求:点击import按钮选择文件路径,假如tableWidget路径已经存在,选中的路径不能成功加载 到 tableWidget中,假如文件路径不同,路径便自动加载
注意:此处tableWidget列数设置默认为1 ,行数通过变量判定,insertRow()行数插入,自动增加,详细代码如下
//tableWidget列数设置
ui.tableWidget->setColumnCount(1);
完整代码
QStringList filePath = QFileDialog::getOpenFileNames(this, tr("Open File"),
t_openFilePath, tr("(*.csv)"));
if (filePath.isEmpty()) return;
//去除重复文件名
for (int i = 0; i < filePath.count(); i++)
{
int t_rowCount = ui.tableWidget->rowCount();
bool t_flag = false;
for (int j = 0; j < t_rowCount; j++)
{
QString t_fileName = ui.tableWidget->item(j, 0)->text();
if (t_fileName == filePath.at(i))
{
t_flag = true;
break;
}
}
if (!t_flag)
{
ui.tableWidget->insertRow(t_rowCount);
ui.tableWidget->setItem(t_rowCount, 0, new QTableWidgetItem(filePath.at(i)));
}
}
效果展示
1、初次选择,加载成功
2、二次选择同一文件,加载tablewidget失败