1 | #include <QtGui>
|
---|
2 | #include "filereader.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | FileReader::FileReader(QWidget *parent)
|
---|
6 | : QWidget(parent)
|
---|
7 | {
|
---|
8 | textEdit = new QTextEdit;
|
---|
9 |
|
---|
10 | taxFileButton = new QPushButton("Tax File");
|
---|
11 | accountFileButton = new QPushButton("Accounts File");
|
---|
12 | reportFileButton = new QPushButton("Report File");
|
---|
13 |
|
---|
14 | //! [0]
|
---|
15 | signalMapper = new QSignalMapper(this);
|
---|
16 | signalMapper->setMapping(taxFileButton, QString("taxfile.txt"));
|
---|
17 | signalMapper->setMapping(accountFileButton, QString("accountsfile.txt"));
|
---|
18 | signalMapper->setMapping(reportFileButton, QString("reportfile.txt"));
|
---|
19 |
|
---|
20 | connect(taxFileButton, SIGNAL(clicked()),
|
---|
21 | signalMapper, SLOT (map()));
|
---|
22 | connect(accountFileButton, SIGNAL(clicked()),
|
---|
23 | signalMapper, SLOT (map()));
|
---|
24 | connect(reportFileButton, SIGNAL(clicked()),
|
---|
25 | signalMapper, SLOT (map()));
|
---|
26 | //! [0]
|
---|
27 |
|
---|
28 | //! [1]
|
---|
29 | connect(signalMapper, SIGNAL(mapped(const QString &)),
|
---|
30 | this, SLOT(readFile(const QString &)));
|
---|
31 | //! [1]
|
---|
32 |
|
---|
33 | QHBoxLayout *buttonLayout = new QHBoxLayout;
|
---|
34 | buttonLayout->addWidget(taxFileButton);
|
---|
35 | buttonLayout->addWidget(accountFileButton);
|
---|
36 | buttonLayout->addWidget(reportFileButton);
|
---|
37 |
|
---|
38 | QVBoxLayout *mainLayout = new QVBoxLayout;
|
---|
39 | mainLayout->addWidget(textEdit);
|
---|
40 | mainLayout->addLayout(buttonLayout);
|
---|
41 |
|
---|
42 | setLayout(mainLayout);
|
---|
43 | }
|
---|
44 |
|
---|
45 | void FileReader::readFile(const QString &filename)
|
---|
46 | {
|
---|
47 | QFile file(filename);
|
---|
48 |
|
---|
49 | if (!file.open(QIODevice::ReadOnly)) {
|
---|
50 | QMessageBox::information(this, tr("Unable to open file"),
|
---|
51 | file.errorString());
|
---|
52 | return;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | QTextStream in(&file);
|
---|
57 | textEdit->setPlainText(in.readAll());
|
---|
58 | }
|
---|
59 |
|
---|