简体   繁体   中英

Why QFileDialog::getOpenFileName doesn't work?

In the String the path is right, but it doesn't put strings in my table . No Error or Warnings. When I do this for example:

 meinModel->setData(filename, filename);

it views the Strings : E:/test.txt

I have a QTableView and a QAbstractTableModel.

 void View::OpenFileButtonClicked()
    {
        QString filename = QFileDialog::getOpenFileName(0, QString(), QString()
                                             ,tr("Data (*.txt)"));
        fileMy = new QFile(filename);
        QTextStream stream (&*fileMy);  
        while (!stream.atEnd())
            {
              QString line = stream.readLine();
              QStringList list = line.split(",");
              QString firststring;
              firststring = (list.first());
              QString secondstring;
              secondstring = (list.last());
             // Strings are sent to Model which view they in my tableview.
              meinModel->setData(firststring, secondstring);
            }        
    }

Strange, because this works:

  void View::FileButtonClicked()
{
    QString fileName;
    if (txtPfad->text().length() > 0 )
    {
        fileMy = new QFile(txtPfad->text());
    } 
    else 
    {   fileMy = new QFile("E:\\test.txt"); }

    if (!fileMy->open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QMessageBox::information(this, tr("ERROR"), tr("Daten konnten icht eingelesen werden"));
        return;
    }
    QTextStream stream (&*fileMy);  
    while (!stream.atEnd())
        {
         QString line = stream.readLine();
         QStringList list = line.split(",");
         QString firststring;
         firststring = (list.first());
         QString secondstring;
         secondstring = (list.last());
         meinModel->setData(firststring, secondstring);
        }   
}   

You missed opening of a file in OpenFileButtonClicked :

if (!fileMy->open(QIODevice::ReadOnly | QIODevice::Text))
{
    QMessageBox::information(this, tr("ERROR"), tr("Daten konnten icht eingelesen werden"));
    return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM