为什么QFileDialog :: getOpenFileName不起作用?

时间:2014-08-18 12:29:48

标签: c++ qt model-view-controller qfile qfiledialog

在String中,路径是正确的,但它不会在我的表中放置字符串。没有错误或警告。当我这样做时:

 meinModel->setData(filename, filename);

它查看字符串:E:/test.txt

我有一个QTableView和一个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);
            }        
    }

奇怪,因为这有效:

  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);
        }   
}   

1 个答案:

答案 0 :(得分:1)

您错过了OpenFileButtonClicked中文件的打开:

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