2012-01-17 417 views
7

我需要在模擬屏幕上顯示一個字符串。爲此,我應該從現有的Filename.txt/Filename.csv文件中讀取文本。文本參數更新如下面的一段代碼所示。我需要從文本文件中訪問字符串並在MarqueeText元素中使用它。訪問的字符串應用於MarqueeText元素的文本字段中。從qt中的.txt或.csv文件讀取一行(Qt Quick)

MarqueeText { 
    id:scrolltext 
    width: 255 
    height: 48 
    anchors.verticalCenter: parent.horizontalCenter 
    text: //i need to access the string in text file to be displayed 
} 

請幫我這個。謝謝。

+0

請去查看您以前提出的問題。如果有人回答了您的問題,請勾選答案。如果沒有人回答,請編輯您的問題,以便回覆。 – 2012-01-17 12:27:32

+0

@ Styne666有沒有什麼問題可以解答這個問題。我刪除了一些問題,因爲它們太舊而無法回答。現在可以嗎? – Rahul 2012-01-17 13:07:40

+0

那麼,如果你從不將自己的答案標記爲正確,那麼人們爲什麼會想回答你的問題?我沒有進入關於如何使用這個網站的討論。現在就閱讀整個[FAQ](http://stackoverflow.com/faq)**,完整地閱讀[Analytical Badge](http://stackoverflow.com/badges/1306/)分析)。 – 2012-01-17 13:22:56

回答

11

請按照wiki頁面閱讀有關以QML格式訪問文件的內容。諾基亞維基論壇http://web.archive.org/web/20150227025348/http://developer.nokia.com/community/wiki/Reading_and_writing_files_in_QML

摘要:

創建自定義QML類型,FileIO專注:

fileio.h

#ifndef FILEIO_H 
#define FILEIO_H 

#include <QObject> 

class FileIO : public QObject 
{ 
    Q_OBJECT 

public: 
    Q_PROPERTY(QString source 
       READ source 
       WRITE setSource 
       NOTIFY sourceChanged) 
    explicit FileIO(QObject *parent = 0); 

    Q_INVOKABLE QString read(); 
    Q_INVOKABLE bool write(const QString& data); 

    QString source() { return mSource; }; 

public slots: 
    void setSource(const QString& source) { mSource = source; }; 

signals: 
    void sourceChanged(const QString& source); 
    void error(const QString& msg); 

private: 
    QString mSource; 
}; 

#endif // FILEIO_H 

fileio.cpp

#include "fileio.h" 
#include <QFile> 
#include <QTextStream> 

FileIO::FileIO(QObject *parent) : 
    QObject(parent) 
{ 

} 

QString FileIO::read() 
{ 
    if (mSource.isEmpty()){ 
     emit error("source is empty"); 
     return QString(); 
    } 

    QFile file(mSource); 
    QString fileContent; 
    if (file.open(QIODevice::ReadOnly)) { 
     QString line; 
     QTextStream t(&file); 
     do { 
      line = t.readLine(); 
      fileContent += line; 
     } while (!line.isNull()); 

     file.close(); 
    } else { 
     emit error("Unable to open the file"); 
     return QString(); 
    } 
    return fileContent; 
} 

bool FileIO::write(const QString& data) 
{ 
    if (mSource.isEmpty()) 
     return false; 

    QFile file(mSource); 
    if (!file.open(QFile::WriteOnly | QFile::Truncate)) 
     return false; 

    QTextStream out(&file); 
    out << data; 

    file.close(); 

    return true; 
} 

註冊新的QML類型:

#include "fileio.h" 

Q_DECL_EXPORT int main(int argc, char *argv[]) 
{ 
    ... 
    qmlRegisterType<FileIO, 1>("FileIO", 1, 0, "FileIO"); 
    ... 
} 

實際QML用法:

import QtQuick 1.1 
import FileIO 1.0 

Rectangle { 
    width: 360 
    height: 360 
    Text { 
     id: myText 
     text: "Hello World" 
     anchors.centerIn: parent 
    } 

    FileIO { 
     id: myFile 
     source: "my_file.txt" 
     onError: console.log(msg) 
    } 

    Component.onCompleted: { 
     console.log("WRITE"+ myFile.write("TEST")); 
     myText.text = myFile.read(); 
    } 
} 
+0

感謝您的鏈接。真的幫助了我。是否可以檢查文件創建時間?有一些屬性可以說明這一點嗎? – SoH 2012-08-07 06:48:20

+0

[QFileInfo :: created()](http://qt-project.org/doc/qt-4.8/qfileinfo.html#created)這個函數會給你文件信息。但我希望它仍然取決於底層操作系統。您可以在FileIO類中使用此函數來返回所需的數據。 – RajaRaviVarma 2012-08-07 14:12:01

+0

我收到以下錯誤:將'FileIO'放入未註冊的命名空間'FileIO'中。如何解決這個問題?謝謝 – 2017-06-06 10:50:59