Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Save and load txt file
Forum Updated to NodeBB v4.3 + New Features

Save and load txt file

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 10.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jjocram
    wrote on 18 Mar 2013, 18:23 last edited by
    #1

    Hi, I'm writing a simple program with Qt 5.0 and I'd be interested, via a button, to save the written text of a text in a .Txt file in the same folder where there are all the program files.
    Then, again using a button, I'd be interested to open a file manager window and choose here another text file which is opened in the text.
    Sorry for the various repetition of the word text; cordial greetings.
    This is the code:
    @import QtQuick 2.0
    import Ubuntu.Components 0.1
    Rectangle {
    width: 300
    height: 300
    color: "lightgray"

        TextArea {
                id:testo
                width: units.gu(37)
                height: units.gu(6)
                contentWidth: units.gu(30)
                contentHeight: units.gu(60)
                activeFocusOnPress : true
    
               }
         Column{
           anchors.centerIn: parent
           spacing: parent.width/6
    
           Button{
               id: exitButton
               text: "load"
    
    
                 }
           Button{
               id:savebutton
               text:"save"
               onClicked:???????
                 }
                }
            }@
    
    1 Reply Last reply
    0
    • U Offline
      U Offline
      utcenter
      wrote on 18 Mar 2013, 18:31 last edited by
      #2

      Just create a QObject derived class, add a read and write slot and implement using QFile, register the class to QML, instantiate it in QML and connect it to the signals of your buttons.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jjocram
        wrote on 19 Mar 2013, 18:42 last edited by
        #3

        Thanks for your reply can you give me an example, please

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jens
          wrote on 19 Mar 2013, 19:12 last edited by
          #4

          There is a pretty simple example here:

          http://www.developer.nokia.com/Community/Wiki/Reading_and_writing_files_in_QML

          It is Qt Quick 1 though so it will require a bit of adaptation.

          1 Reply Last reply
          0
          • U Offline
            U Offline
            utcenter
            wrote on 19 Mar 2013, 19:23 last edited by
            #5

            Here is a quick and basic example for QtQuick2 - paths are hardcoded, since it is just an example:

            The FileHelper custom QML element:

            @class FileHelper : public QObject {
            Q_OBJECT
            public:
            explicit FileHelper(QObject *parent = 0) : QObject(parent) {}

            public slots:
            void writeFile(const QString &text) {
            QFile file("h:/test.txt");
            if (!file.open(QFile::WriteOnly | QFile::Text)) return;
            QTextStream out(&file);
            out << text;
            file.close();
            }

            QString readFile&#40;&#41; {
                QFile file("h:/test.txt"&#41;;
                if (!file.open(QFile::ReadOnly | QFile::Text&#41;&#41; return "";
                QTextStream in(&file);
                QString temp = in.readAll();
                file.close();
                return temp;
            }    
            

            };@

            Register the element to the engine in main.cpp:

            @#include <QtQuick>

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

            And last - the QML:

            @import QtQuick 2.0
            import FileHelpers 1.0

            Rectangle {
            width: 360
            height: 360

            FileHelper {
                id: helper
            }
            
            TextEdit {
                id: myText
                anchors.fill: parent
                text: "test"
            }
            
            Button {
                name: "Read file"
                anchors.left: parent.left
                anchors.bottom: parent.bottom
                onClicked: myText.text = helper.readFile(&#41;
            }
            
            Button {
                name: "Write file"
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                onClicked: helper.writeFile(myText.text&#41;
            }
            

            }@

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jjocram
              wrote on 20 Mar 2013, 16:43 last edited by
              #6

              Thank you very much for the answer.
              When i start my program the terminal gives me this error:
              @2 module "FileHelpers" is not installed@

              How can I fix?

              1 Reply Last reply
              0
              • U Offline
                U Offline
                utcenter
                wrote on 20 Mar 2013, 16:56 last edited by
                #7

                Did you register the class, i.e. the second step of my example?

                1 Reply Last reply
                0

                7/7

                20 Mar 2013, 16:56

                • Login

                • Login or register to search.
                7 out of 7
                • First post
                  7/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved