Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to save spinbox or sliders value into a text file ?
Forum Updated to NodeBB v4.3 + New Features

How to save spinbox or sliders value into a text file ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 1.8k 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.
  • A Offline
    A Offline
    Amine Djeddi
    wrote on last edited by
    #1

    hi Qties !!
    i have 6 spinboxes in my Qt designer i want to save it's values into text file !!

    any idea ?

    thank you :p

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Well just open file and write the values ?
      You can use QString::number to go from int to text version.
      Did you have something more fancy in mind ?
      Also samples just writes each value after each other.
      Maybe you had some format in mind ?

      QString filename="Data.txt";
      QFile file( filename );
      if ( file.open(QIODevice::ReadWrite | QIODevice::Text)  )
      {
          QTextStream stream( &file );
          stream << QString::number(ui->spin1->value()) << QString::number(ui->spin2->value()) ... << endl;
      }
      
      A 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        Well just open file and write the values ?
        You can use QString::number to go from int to text version.
        Did you have something more fancy in mind ?
        Also samples just writes each value after each other.
        Maybe you had some format in mind ?

        QString filename="Data.txt";
        QFile file( filename );
        if ( file.open(QIODevice::ReadWrite | QIODevice::Text)  )
        {
            QTextStream stream( &file );
            stream << QString::number(ui->spin1->value()) << QString::number(ui->spin2->value()) ... << endl;
        }
        
        A Offline
        A Offline
        Amine Djeddi
        wrote on last edited by
        #3

        @mrjj
        yes that is what i was thinking about , creating a file , and write into them ,
        but i did better to ask before programming it , because i did not though about Qstring:: number

        thank you so much , i will do it , if i get into trouble i ask again xD

        mrjjM 1 Reply Last reply
        0
        • A Amine Djeddi

          @mrjj
          yes that is what i was thinking about , creating a file , and write into them ,
          but i did better to ask before programming it , because i did not though about Qstring:: number

          thank you so much , i will do it , if i get into trouble i ask again xD

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Amine-Djeddi
          Hi
          Ok super.
          Do you plan to read them in again ?

          A 1 Reply Last reply
          0
          • mrjjM mrjj

            @Amine-Djeddi
            Hi
            Ok super.
            Do you plan to read them in again ?

            A Offline
            A Offline
            Amine Djeddi
            wrote on last edited by
            #5

            @mrjj
            yes i did it before ((you adviced me to use Plain text Edit)) so i have managed to write en read into file

            mrjjM 1 Reply Last reply
            0
            • A Amine Djeddi

              @mrjj
              yes i did it before ((you adviced me to use Plain text Edit)) so i have managed to write en read into file

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Amine-Djeddi
              Ok. just asking as then you want to separate the values with newline or something like that
              as as sample will look messy :)
              I meant, if you wanted to read them back into the spinboxes.
              If just for reading the complete text files, its not so important but might be need to be
              human readable.

              A 2 Replies Last reply
              0
              • mrjjM mrjj

                @Amine-Djeddi
                Ok. just asking as then you want to separate the values with newline or something like that
                as as sample will look messy :)
                I meant, if you wanted to read them back into the spinboxes.
                If just for reading the complete text files, its not so important but might be need to be
                human readable.

                A Offline
                A Offline
                Amine Djeddi
                wrote on last edited by
                #7

                @mrjj
                yeees , i want to to separate the values with a comma

                mrjjM 1 Reply Last reply
                0
                • A Amine Djeddi

                  @mrjj
                  yeees , i want to to separate the values with a comma

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Amine-Djeddi
                  ok.
                  you can just do that directly

                   stream << QString::number(ui->spin1->value()) <<"," << QString::number(ui->spin2->value()) <<"," .... << endl;
                  
                  1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @Amine-Djeddi
                    Ok. just asking as then you want to separate the values with newline or something like that
                    as as sample will look messy :)
                    I meant, if you wanted to read them back into the spinboxes.
                    If just for reading the complete text files, its not so important but might be need to be
                    human readable.

                    A Offline
                    A Offline
                    Amine Djeddi
                    wrote on last edited by
                    #9

                    @mrjj
                    ahh yes sorry my english is little bit bad ..... ,
                    yeees i want to read them back into spinbox it's good idea !!
                    can you help me

                    mrjjM 1 Reply Last reply
                    0
                    • A Amine Djeddi

                      @mrjj
                      ahh yes sorry my english is little bit bad ..... ,
                      yeees i want to read them back into spinbox it's good idea !!
                      can you help me

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by VRonin
                      #10

                      @Amine-Djeddi
                      Well something like
                      (disclaimer, didnt try compile it so there will by errors. My win qt died )

                      QFile file("Data.txt");
                      if(!file.open(QIODevice::ReadOnly)) {
                          QMessageBox::information(0, "error", file.errorString());
                      }
                      QTextStream in(&file);
                      while(!in.atEnd()) {
                          QString line = in.readLine();    
                          QStringList fields = line.split(",");        
                      }
                      file.close();
                      Now each entry in fields will be a value ( in text format)
                      so you can do 
                      if ( ! fields.size() )  { // this is sanity check that we did get something in list
                        QMessageBox::information(0, "error happened", "error");
                      return;
                      }
                      // assign back
                      ui->spin1->setValue( fields[0].toInt());
                      ui->spin1->setValue( fields[1].toInt());
                      
                      1 Reply Last reply
                      1

                      • Login

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