0
votes

My programming knowledge and experience is very poor. I am using this code block to open the desired file when clicked on a push button ;

QString filename = QFileDialog::getOpenFileName();

QFile file(filename);
 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
     return;

 while (!file.atEnd())
      {
          QByteArray line = file.readLine();
          processline(line);
      }

And by this line i am showing it on QtextBrowser

void MainWindow::processline(QByteArray paramline) {

ui->veri_cikis->append(paramline.constData());

}

The data on the file is like this

0;100;0

0;100;24

24;500;24

24;100;6

6;100;6

i have to split the datas by ";" mark and display them on a Qtreewidget columns. How do i do that ? And i have to show each first part on first column and second on second column and so. I have 3 columns in total

1
Do you want tree view or table view ? - Pratham
Use the "split" Function . The seperator is ";". - Matthias
Tree view would be better. - Ashtaroth

1 Answers

0
votes

I think what you describe is better fit rather to a table view than a tree view. To parse your strings and split them by ';' character you can use QByteArray::split() function. Here is the sample code, that creates and populates table view with items that read from the file:

QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    return;

QTableWidget *table = new QTableWidget;
int row = 0;
while (!file.atEnd()) {
    QByteArray line = file.readLine();
    QList<QByteArray> tokens = line.split(';');
    int column = 0;
    row++;
    foreach (QByteArray ba, tokens) {
        QTableWidgetItem *item = new QTableWidgetItem(ba);
        table->setItem(row, column++, item);
    }
}