使用Qt中的按钮打开文本文件

时间:2014-04-02 11:53:23

标签: qt signals-slots

Qt中按钮的信号和插槽区域内容应该是什么,这样点击按钮后只会打开文本文件。

void MainWindow::on_pushButton_clicked()
 {
 ....
 }

2 个答案:

答案 0 :(得分:0)

你可以使用你想要的任何东西来打开文件,比如FILE,fstream,QFile ecc。你只需要调用一个类方法,但在该函数中你可以放置所有东西。 您正在使用Qt,因此您可以查看QT的QFile类。

答案 1 :(得分:0)

可以这样做:

void MainWindow::on_pushButton_clicked()
{

        QFile file( filename );

        if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
            QMessageBox::warning(this, tr("Error opening!"), tr("Could not open the file"));

        QTextStream stream( &file );

        while( !stream.atEnd() )
        {
            QString lineText;
            lineText = stream.readLine(); //Read a line of text

            QStringList tokens= lineText.split(" ",QString::SkipEmptyParts); //Take tokens from the line
        }

        file.close();
}
相关问题