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