#include <QtGui>
{
Q_OBJECT
public:
explicit MyMainWindow
(QWidget * parent
= 0);
virtual ~MyMainWindow() {}
void setOperner(bool opener) { m_opener = opener; }
signals:
void sendDocument
(const QString &doc
);
void hereIsData
(const QString &data
);
public slots:
void takeDocumentText
(const QString &docText
);
void openDocument
(const QString &filename
);
void openFile();
private:
bool m_opener;
};
MyMainWindow
::MyMainWindow(QWidget *parent
) , m_menuFile(menuBar()->addMenu("&File"))
, m_open(m_menuFile->addAction("Open a file"))
, m_opener(true)
{
m_layout->addWidget(m_openAFile, 0, 0, 1, 1);
m_layout->addWidget(m_myText, 1, 0, 1, 1);
m_centralZone->setLayout(m_layout);
connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));
setCentralWidget(m_centralZone);
}
void MyMainWindow
::openDocument(const QString &filename
) {
{
const QString data
= File.
readAll();
File.close();
m_openAFile->setText(filename);
takeDocumentText(data);
emit hereIsData(data);
}
}
void MyMainWindow
::takeDocumentText(const QString &docText
) {
m_myText->setText(docText);
}
void MyMainWindow::openFile()
{
if(m_opener)
openDocument(file);
else
emit sendDocument(file);
}
int main(int argc, char** argv)
{
MyMainWindow window1;
MyMainWindow window2;
window1.setWindowTitle("Document Opener & Viewer");
window2.setWindowTitle("Document Viewer");
window1.setOperner(true);
window2.setOperner(false);
window1.show();
window2.show();
app.
connect(&window2,
SIGNAL(sendDocument
(QString)),
&window1,
SLOT(openDocument
(QString)));
app.
connect(&window1,
SIGNAL(hereIsData
(QString)),
&window2,
SLOT(takeDocumentText
(QString)));
return app.exec();
}
#include "main.moc"
#include <QtGui>
class MyMainWindow: public QMainWindow
{
Q_OBJECT
public:
explicit MyMainWindow(QWidget * parent = 0);
virtual ~MyMainWindow() {}
void setOperner(bool opener) { m_opener = opener; }
signals:
void sendDocument(const QString &doc);
void hereIsData(const QString &data);
public slots:
void takeDocumentText(const QString &docText);
void openDocument(const QString &filename);
void openFile();
private:
QWidget *m_centralZone;
QLineEdit *m_openAFile;
QTextEdit *m_myText;
QGridLayout * m_layout;
QMenu *m_menuFile;
QAction *m_open;
bool m_opener;
};
MyMainWindow::MyMainWindow(QWidget *parent)
: QMainWindow(parent)
, m_centralZone(new QWidget)
, m_openAFile(new QLineEdit)
, m_myText(new QTextEdit)
, m_layout(new QGridLayout)
, m_menuFile(menuBar()->addMenu("&File"))
, m_open(m_menuFile->addAction("Open a file"))
, m_opener(true)
{
m_layout->addWidget(m_openAFile, 0, 0, 1, 1);
m_layout->addWidget(m_myText, 1, 0, 1, 1);
m_centralZone->setLayout(m_layout);
connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));
setCentralWidget(m_centralZone);
}
void MyMainWindow::openDocument(const QString &filename)
{
QFile File(filename);
if(File.open(QIODevice::ReadOnly | QIODevice::Text))
{
const QString data = File.readAll();
File.close();
m_openAFile->setText(filename);
takeDocumentText(data);
emit hereIsData(data);
}
}
void MyMainWindow::takeDocumentText(const QString &docText)
{
m_myText->setText(docText);
}
void MyMainWindow::openFile()
{
const QString file = QFileDialog::getOpenFileName(this, "Information", "Open", "*.text");
if(m_opener)
openDocument(file);
else
emit sendDocument(file);
}
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MyMainWindow window1;
MyMainWindow window2;
window1.setWindowTitle("Document Opener & Viewer");
window2.setWindowTitle("Document Viewer");
window1.setOperner(true);
window2.setOperner(false);
window1.show();
window2.show();
app.connect(&window2, SIGNAL(sendDocument(QString)), &window1, SLOT(openDocument(QString)));
app.connect(&window1, SIGNAL(hereIsData(QString)), &window2, SLOT(takeDocumentText(QString)));
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks