Have a look at this, this is one way to do it.

Qt Code:
  1. #include <QApplication>
  2. #include <QtGui>
  3. #include <QtCore>
  4.  
  5. class QTextEditLoader : public QObject // Added <<<<<<<<<<<<<<<<<<<<
  6. {
  7. Q_OBJECT
  8.  
  9. public:
  10. explicit QTextEditLoader(QTextEdit *textEdit, QLineEdit * filename)
  11. : QObject(textEdit)
  12. , parent(textEdit)
  13. , fileName(filename)
  14. {
  15. ;
  16. }
  17.  
  18. public slots:
  19. void loadTextFromFile(void)
  20. {
  21. QString text("<Error, Unable to open file>");
  22. QFile file(fileName->text());
  23.  
  24. if(file.open(file.ReadOnly | file.Text))
  25. {
  26. text = file.readAll();
  27. if(text.isEmpty())
  28. text = "<Error, Empty/Non-Text File>";
  29. file.close();
  30. }
  31.  
  32. parent->setText(text);
  33. }
  34.  
  35. private:
  36. QTextEdit *parent;
  37. QLineEdit *fileName;
  38. };
  39.  
  40. int main(int argc, char *argv[])
  41. {
  42. QApplication app(argc, argv);
  43. QWidget *window = new QWidget;
  44. window->setWindowTitle("Text Reader");
  45.  
  46. QGridLayout *layout = new QGridLayout;
  47.  
  48. QTextEdit *textbox = new QTextEdit();
  49. QLineEdit *enterFile = new QLineEdit();
  50. QTextEditLoader *loader = new QTextEditLoader(textbox, enterFile); // Added <<<<<<<<<<<<<<<<<<<<
  51. QPushButton *goButton = new QPushButton("Ok");
  52. QPushButton *quitButton = new QPushButton("Quit");
  53.  
  54. layout->addWidget(textbox, 0,0,1,1);
  55. layout->addWidget(enterFile, 1,0,1,1);
  56. layout->addWidget(goButton, 2,0,1,1);
  57. layout->addWidget(quitButton, 3,0,1,1);
  58.  
  59. window->setLayout(layout);
  60.  
  61. QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
  62. QObject::connect(goButton, SIGNAL(clicked()), loader, SLOT(loadTextFromFile())); // Added <<<<<<<<<<<<<<<<<<<<
  63.  
  64. //
  65. //STUFF TO BE ADDED
  66. //
  67.  
  68. window->show();
  69. return app.exec(); //Starts main event loop
  70. }
  71.  
  72. #include "Main.moc"
To copy to clipboard, switch view to plain text mode