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. I would like to open and read a text file at program launch, using Qt
Forum Update on Monday, May 27th 2025

I would like to open and read a text file at program launch, using Qt

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.7k 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.
  • P Offline
    P Offline
    pditty8811
    wrote on 9 Oct 2013, 05:18 last edited by
    #1

    I would like to open a text file at program launch, using Qt. I would like the text to appear in the text field which is called textEdit.

    It is a simple notepad program that I am changing into an app I want to do other special things.

    How do I input a text file, say "text.txt" into my textEdit widget upon program launch? All of the text file.

    Writing with C++.

    Thanks.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 9 Oct 2013, 05:40 last edited by
      #2

      Hi,

      You can open and read the file from your QMainWindow or QDialog's constructor.
      Try the following code:
      @ #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QFile>
      #include <QTextStream>

      MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          loadFile&#40;&#41;;
      }
       
      MainWindow::~MainWindow()
      {
          delete ui;
      }
       
      void MainWindow::loadFile&#40;&#41;
      {
          QFile file&#40;":/read.txt"&#41;; //If present in Resource 
      

      // QFile file("D:/Test/read.txt"); //If present on system
      file.open(QIODevice::ReadOnly);

          QTextStream stream(&file);
          QString line = stream.readAll();
          file.close();
       
          ui->textEdit->setText(line);
      }
      

      @

      157

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pditty8811
        wrote on 9 Oct 2013, 07:25 last edited by
        #3

        Thank you. That did it.

        1 Reply Last reply
        0

        1/3

        9 Oct 2013, 05:18

        • Login

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