#include <QApplication>
#include <QtGui>
#include <QtCore>
class QTextEditLoader
: public QObject // Added <<<<<<<<<<<<<<<<<<<<{
Q_OBJECT
public:
, parent(textEdit)
, fileName(filename)
{
;
}
public slots:
void loadTextFromFile(void)
{
QString text
("<Error, Unable to open file>");
QFile file(fileName
->text
());
if(file.open(file.ReadOnly | file.Text))
{
text = file.readAll();
if(text.isEmpty())
text = "<Error, Empty/Non-Text File>";
file.close();
}
parent->setText(text);
}
private:
};
int main(int argc, char *argv[])
{
window->setWindowTitle("Text Reader");
QTextEditLoader *loader = new QTextEditLoader(textbox, enterFile); // Added <<<<<<<<<<<<<<<<<<<<
layout->addWidget(textbox, 0,0,1,1);
layout->addWidget(enterFile, 1,0,1,1);
layout->addWidget(goButton, 2,0,1,1);
layout->addWidget(quitButton, 3,0,1,1);
window->setLayout(layout);
QObject::connect(quitButton,
SIGNAL(clicked
()),
qApp,
SLOT(quit
()));
QObject::connect(goButton,
SIGNAL(clicked
()), loader,
SLOT(loadTextFromFile
()));
// Added <<<<<<<<<<<<<<<<<<<<
//
//STUFF TO BE ADDED
//
window->show();
return app.exec(); //Starts main event loop
}
#include "Main.moc"
#include <QApplication>
#include <QtGui>
#include <QtCore>
class QTextEditLoader : public QObject // Added <<<<<<<<<<<<<<<<<<<<
{
Q_OBJECT
public:
explicit QTextEditLoader(QTextEdit *textEdit, QLineEdit * filename)
: QObject(textEdit)
, parent(textEdit)
, fileName(filename)
{
;
}
public slots:
void loadTextFromFile(void)
{
QString text("<Error, Unable to open file>");
QFile file(fileName->text());
if(file.open(file.ReadOnly | file.Text))
{
text = file.readAll();
if(text.isEmpty())
text = "<Error, Empty/Non-Text File>";
file.close();
}
parent->setText(text);
}
private:
QTextEdit *parent;
QLineEdit *fileName;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
window->setWindowTitle("Text Reader");
QGridLayout *layout = new QGridLayout;
QTextEdit *textbox = new QTextEdit();
QLineEdit *enterFile = new QLineEdit();
QTextEditLoader *loader = new QTextEditLoader(textbox, enterFile); // Added <<<<<<<<<<<<<<<<<<<<
QPushButton *goButton = new QPushButton("Ok");
QPushButton *quitButton = new QPushButton("Quit");
layout->addWidget(textbox, 0,0,1,1);
layout->addWidget(enterFile, 1,0,1,1);
layout->addWidget(goButton, 2,0,1,1);
layout->addWidget(quitButton, 3,0,1,1);
window->setLayout(layout);
QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
QObject::connect(goButton, SIGNAL(clicked()), loader, SLOT(loadTextFromFile())); // Added <<<<<<<<<<<<<<<<<<<<
//
//STUFF TO BE ADDED
//
window->show();
return app.exec(); //Starts main event loop
}
#include "Main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks