QTreeWidget自动改变行高

发布于 2024-12-20 00:52:03 字数 2806 浏览 2 评论 0原文

将文件加载到我的 QTreeWidget 后,行数增长得比之前更高。

我怎样才能避免这种行为?

这是我的申请: wrong row height

底部的 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:
wrong row height

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁把谁当真 2024-12-27 00:52:03

感谢约翰森和 AJG85!

一个简单的trimmed()就达到了目的。我不知道为什么我自己没有想到这一点。

QString strategy = csvFile.at(j).at(3);
for(int k=4;k<csvFile.at(j).size();k++)
{
   strategy += " " % csvFile.at(j).at(k);
}
strategy = strategy.trimmed();
item->setText(3, strategy);

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.

QString strategy = csvFile.at(j).at(3);
for(int k=4;k<csvFile.at(j).size();k++)
{
   strategy += " " % csvFile.at(j).at(k);
}
strategy = strategy.trimmed();
item->setText(3, strategy);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文