Frage

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

War es hilfreich?

Lösung

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);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top