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. .dat Files
Qt 6.11 is out! See what's new in the release blog

.dat Files

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.1k Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Currently trying to read and print a .dat file and have no idea where to start. My goal is to display each line of data on a QLabel. I had this code, but nothing happened.

    QFile inputFile(QString("/airplane.dat"));
        inputFile.open(QIODevice::ReadOnly);
        if (!inputFile.isOpen()){
        return;
        }
    
        QTextStream stream(&inputFile);
        for (QString line = stream.readLine();
             !line.isNull();
             line = stream.readLine()) {
            ui->datText->setText(line);
            qApp->processEvents();
            }
    

    Here is what the .dat file looks like:

    Screen Shot 2021-10-27 at 8.57.10 AM.png

    Any idea on what to do?

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @WesLow said in .dat Files:

      ui->datText->setText("line");

      Why do you set a text 'line' instead the content of the variable line in here?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      ? 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @WesLow said in .dat Files:

        ui->datText->setText("line");

        Why do you set a text 'line' instead the content of the variable line in here?

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @Christian-Ehrlicher Did that as a test to make sure it wasn't a GUI issue; forgot to change it back. Let me edit that really quickly...

        J.HilkJ 1 Reply Last reply
        0
        • ? A Former User

          @Christian-Ehrlicher Did that as a test to make sure it wasn't a GUI issue; forgot to change it back. Let me edit that really quickly...

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @WesLow I see processEvent calls, multi calls to readline() where the previous data is thrown away (unused).

          your dat file seems to be a simple text file, why don't you simply do:

          QTextStream stream(&inputFile);
          auto myText = stream.readAll();
          
          //Console output for tests
          qDebug() << myText;
          //Set text for ui
          ui->datText->setText(myText);
          
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • ? A Former User

            Currently trying to read and print a .dat file and have no idea where to start. My goal is to display each line of data on a QLabel. I had this code, but nothing happened.

            QFile inputFile(QString("/airplane.dat"));
                inputFile.open(QIODevice::ReadOnly);
                if (!inputFile.isOpen()){
                return;
                }
            
                QTextStream stream(&inputFile);
                for (QString line = stream.readLine();
                     !line.isNull();
                     line = stream.readLine()) {
                    ui->datText->setText(line);
                    qApp->processEvents();
                    }
            

            Here is what the .dat file looks like:

            Screen Shot 2021-10-27 at 8.57.10 AM.png

            Any idea on what to do?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @WesLow said in .dat Files:

            My goal is to display each line of data on a QLabel

            First the file just looks like text. Maybe with tabs where there are multiple spaces, we don't know; what program were you using to view that file when you took the screenshot?

            So you just need to do what @J-Hilk says, nothing fancy.

            If that works, you will see a single multi-line QLabel (if that is what your ui->datText is).

            If you then want to do something about breaking it into "each line", I do not suggest you create and place a QLabel for each line. Unless it's just a learning exercise. A QListView might be a preferable widget to use.

            P.S.

            I had this code, but nothing happened.

            I suspect something did happen. But quickly...!

                    ui->datText->setText(line);
                    qApp->processEvents();
            

            You should be left at the end with whatever the final line in the file is. I think you are expecting to see each line at a time running past, but with one qApp->processEvents(); per iteration I think it will happen much faster than your eye :)

            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