QTreeWidget自动改变行高
将文件加载到我的 QTreeWidget 后,行数增长得比之前更高。
我怎样才能避免这种行为?
这是我的申请:
底部的 QTreeWidget 与上面的设置相同。
这是 csv 文件的加载函数:
void MainWindow::openCSV()
{
if(!arffRead)
{
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Warning);
msgBox.setText("You have to open the .arff file at first.");
msgBox.exec();
return;
}
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilter(tr("CSV (*.csv)"));
dialog.setViewMode(QFileDialog::Detail);
QString fileName;
QString buffer;
if (dialog.exec() == QFileDialog::Accepted)
{
fileName = dialog.selectedFiles()[0];
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
cout << "Error opening " + fileName.toStdString() + "." << endl;
}
gameTable->clear();
QVector<QStringList> csvFile;
while (!file.atEnd())
{
QString line(file.readLine());
QStringList tokens = line.split(",");
csvFile.push_back(tokens);
}
if(distanceTable->topLevelItemCount() != csvFile.size())
{
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Warning);
msgBox.setText("Your files have different numbers of lines.");
msgBox.exec();
return;
}
for(int i=0; i<distanceTable->topLevelItemCount();i++)
{
for(int j=0; j<csvFile.size(); j++)
{
if(distanceTable->topLevelItem(i)->text(1) == csvFile.at(j).at(0))
{
QTreeWidgetItem* item = new QTreeWidgetItem;
item->setText(0, QString::number(i+1));
item->setText(1, csvFile.at(j).at(0));
item->setText(2, csvFile.at(j).at(2));
QString strategy = csvFile.at(j).at(3);
for(int k=4;k<csvFile.at(j).size();k++)
{
strategy += " " % csvFile.at(j).at(k);
}
item->setText(3, strategy);
gameTable->insertTopLevelItem(i,item);
break;
}
}
}
gameTable->resizeColumnToContents(3);
}
}
这里作为我的 csv 文件中的一个缩短行的示例:
1,Zerg,lose,Train Drone,3,Train Overlord,37,Train Drone,44,Build Spawning,80, (...)
我将 csv 文件中第三个字段的所有数据连接起来。
我希望所有数据都按相同的顺序排列,因此我从 distanceTable 中获取 GameId,然后插入 csv 文件中的数据。
提前致谢!
托比亚斯
after loading a file into my QTreeWidget, the rows are growing higher than bevor.
How can I avoid this behavior?
Here is my application:
The bottom QTreeWidget has the same settings as the one above.
Here is the loading function of a csv file:
void MainWindow::openCSV()
{
if(!arffRead)
{
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Warning);
msgBox.setText("You have to open the .arff file at first.");
msgBox.exec();
return;
}
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilter(tr("CSV (*.csv)"));
dialog.setViewMode(QFileDialog::Detail);
QString fileName;
QString buffer;
if (dialog.exec() == QFileDialog::Accepted)
{
fileName = dialog.selectedFiles()[0];
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
cout << "Error opening " + fileName.toStdString() + "." << endl;
}
gameTable->clear();
QVector<QStringList> csvFile;
while (!file.atEnd())
{
QString line(file.readLine());
QStringList tokens = line.split(",");
csvFile.push_back(tokens);
}
if(distanceTable->topLevelItemCount() != csvFile.size())
{
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Warning);
msgBox.setText("Your files have different numbers of lines.");
msgBox.exec();
return;
}
for(int i=0; i<distanceTable->topLevelItemCount();i++)
{
for(int j=0; j<csvFile.size(); j++)
{
if(distanceTable->topLevelItem(i)->text(1) == csvFile.at(j).at(0))
{
QTreeWidgetItem* item = new QTreeWidgetItem;
item->setText(0, QString::number(i+1));
item->setText(1, csvFile.at(j).at(0));
item->setText(2, csvFile.at(j).at(2));
QString strategy = csvFile.at(j).at(3);
for(int k=4;k<csvFile.at(j).size();k++)
{
strategy += " " % csvFile.at(j).at(k);
}
item->setText(3, strategy);
gameTable->insertTopLevelItem(i,item);
break;
}
}
}
gameTable->resizeColumnToContents(3);
}
}
and here as an example one shortened line from my csv file:
1,Zerg,lose,Train Drone,3,Train Overlord,37,Train Drone,44,Build Spawning,80, (...)
I concatenate all data from the third field on in the csv file.
I want all the data in the same order, so I grab the GameId from the distanceTable and insert then the data from the csv file.
Thanks in advance!
Tobias
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢约翰森和 AJG85!
一个简单的trimmed()就达到了目的。我不知道为什么我自己没有想到这一点。
Thanks to johnathon and AJG85!
A simple trimmed() did the trick. I don't know why I didn't think about that by my self.