Tips and tricks

How to create a QT GUI Application that reads a text file and outputs in a Text Box
  1. create new QT Project, choose QT C++ Project > Qt Gui Application, click Next
  2. name your application as you wish, click Next
  3. leave everything as it is, click Next
  4. click Finish
  5. click on the Design Tab, drag a Text Edit box
  6. include the following headers
    #include "ui_mainwindow.h"
    #include <QFile>
    #include <QTextStream>
    #include <QString>
    #define IO_ReadOnly QIODevice::ReadOnly
  7. write this function before the main function
    void MainWindow::populate(){}
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
    }

  8. inside MainWindow.h add
    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    public slots:
    void populate();

    };

  9. open main.cpp – inside main() function add w.populate();
  10. inside MainWindow::populate() write  ui->textEdit->append(“test”);
  11. if it works, we can go forward, if not, review the steps
  12. write this inside populate functionQFile file("c:/in.txt"); // Create a file handle for the file named
    QString line;

    if (!file.open(IO_ReadOnly)) // Open the file
    {
    // handle error
    }

    QTextStream stream( &file ); // Set the stream to read from myFile

    while(!stream.atEnd()){

    line = stream.readLine(); // this reads a line (QString) from the file
    ui->textEdit->append(line);
    }

  13. write your text file in.txt in C:
  14. all done, download the source if you need to – https://digitalzoomstudio.net/misc/testut.pro.zip
{"type":"main_options","images_arr":"'#ffffff'","enable_ajax":"off","soundcloud_apikey":"","bg_isparallax":"off","bg_slideshow_time":"0","bg_transition":"slidedown","site_url":"https:\/\/digitalzoomstudio.net","theme_url":"https:\/\/digitalzoomstudio.net\/wp-content\/themes\/qucreative\/","blur_ammount":"26","width_column":"50","width_section_bg":"","width_gap":"30","border_width":"0","border_color":"#ffffff","translate_cancel_comment":"Cancel reply","translate_leave_a_comment":"Leave a comment","translate_leave_a_comment_to":"Leave a comment to","is_customize_preview":"off","width_blur_margin":"30","gallery_w_thumbs_autoplay_videos":"off","enable_native_scrollbar":"on","content_enviroment_opacity":"30","menu_enviroment_opacity":"70"}
{"type":"darkfull"}