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 – http://digitalzoomstudio.net/misc/testut.pro.zip
  • redbellfood March 6, 2015 at 3:08 pm

    Way too much time on your hands Cary, but a fantastic job

  • showcarsupplyauto March 29, 2015 at 4:08 pm

    Valuable very touchable information. I would like to thank you for sharing your great facts.

  • greersautomotive March 29, 2015 at 4:11 pm

    Superbly written article, if only all bloggers oered the same content as you, the internet would be a far better place..

  • essex-x-tech.com May 30, 2015 at 8:03 am

    The post is written in very a good manner and it entails much useful information for me. I am happy to find your distinguished way of writing the post.

  • cureforyoutech.com May 30, 2015 at 8:06 am

    Very interesting post. really informative. of all the blogs I have read the same topic, this one is actually enlightening.

  • vhghealthcenters June 2, 2015 at 11:04 pm

    Wow, this post is pleasant, my younger sister is analyzing these things, so I am going to tell her.

  • healthconceptswi June 2, 2015 at 11:07 pm

    Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your blog? My blog is in the very same area of interest as yours and my users would certainly benefit from some of the information you provide here. Please let me know if this alright with you. Thanks a lot!

  • treehousepastryshop June 9, 2015 at 6:05 pm

    Needed to compose you a word regarding the nice opinions you have contributed. Your good knowledge in playing with all the pieces is very useful. To be honest, this has been one outstanding blog

Leave a comment

Your email address will not be published

{"type":"main_options","images_arr":"'#ffffff'","enable_ajax":"off","soundcloud_apikey":"","bg_isparallax":"off","bg_slideshow_time":"0","bg_transition":"none","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","content_enviroment_opacity":"30","menu_enviroment_opacity":"70","base_url":"https:\/\/digitalzoomstudio.net"}
{"type":"darkfull"}