| 1 |
<?xml version="1.0" encoding="iso-8859-1"?> |
| 2 |
<!DOCTYPE html |
| 3 |
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> |
| 4 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 5 |
<!-- loader.qdoc --> |
| 6 |
<head> |
| 7 |
<title>Loading Documents</title> |
| 8 |
<link href="classic.css" rel="stylesheet" type="text/css" /> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<table border="0" cellpadding="0" cellspacing="0" width="100%"> |
| 12 |
<tr> |
| 13 |
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="57" height="67" border="0" /></td> |
| 14 |
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a></td> |
| 15 |
</tr></table><h1 class="title">Loading Documents<br /><span class="subtitle"></span> |
| 16 |
</h1> |
| 17 |
<p>The application in this example loads or prints the documents passed as commandline parameters to further instances of this application.</p> |
| 18 |
<pre><span class="comment"> /**************************************************************************** |
| 19 |
** |
| 20 |
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). |
| 21 |
** Contact: http://www.qt-project.org/legal |
| 22 |
** |
| 23 |
** This file is part of the Qt Solutions component. |
| 24 |
** |
| 25 |
** You may use this file under the terms of the BSD license as follows: |
| 26 |
** |
| 27 |
** "Redistribution and use in source and binary forms, with or without |
| 28 |
** modification, are permitted provided that the following conditions are |
| 29 |
** met: |
| 30 |
** * Redistributions of source code must retain the above copyright |
| 31 |
** notice, this list of conditions and the following disclaimer. |
| 32 |
** * Redistributions in binary form must reproduce the above copyright |
| 33 |
** notice, this list of conditions and the following disclaimer in |
| 34 |
** the documentation and/or other materials provided with the |
| 35 |
** distribution. |
| 36 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
| 37 |
** the names of its contributors may be used to endorse or promote |
| 38 |
** products derived from this software without specific prior written |
| 39 |
** permission. |
| 40 |
** |
| 41 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 42 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 43 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 44 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 45 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 46 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 47 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 48 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 49 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 50 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 51 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 52 |
** |
| 53 |
****************************************************************************/</span> |
| 54 |
|
| 55 |
#include <qtsingleapplication.h> |
| 56 |
#include <QtCore/QFile> |
| 57 |
#include <QtGui/QMainWindow> |
| 58 |
#include <QtGui/QPrinter> |
| 59 |
#include <QtGui/QPainter> |
| 60 |
#include <QtGui/QTextEdit> |
| 61 |
#include <QtGui/QMdiArea> |
| 62 |
#include <QtCore/QTextStream> |
| 63 |
|
| 64 |
class MainWindow : public QMainWindow |
| 65 |
{ |
| 66 |
Q_OBJECT |
| 67 |
public: |
| 68 |
MainWindow(); |
| 69 |
|
| 70 |
public slots: |
| 71 |
void handleMessage(const QString& message); |
| 72 |
|
| 73 |
signals: |
| 74 |
void needToShow(); |
| 75 |
|
| 76 |
private: |
| 77 |
QMdiArea *workspace; |
| 78 |
};</pre> |
| 79 |
<p>The user interface in this application is a <a href="http://qt.nokia.com/doc/4.6/qmainwindow.html">QMainWindow</a> subclass with a <a href="http://qt.nokia.com/doc/4.6/qmdiarea.html">QMdiArea</a> as the central widget. It implements a slot <tt>handleMessage()</tt> that will be connected to the messageReceived() signal of the <a href="qtsingleapplication.html">QtSingleApplication</a> class.</p> |
| 80 |
<pre> MainWindow::MainWindow() |
| 81 |
{ |
| 82 |
workspace = new QMdiArea(this); |
| 83 |
|
| 84 |
setCentralWidget(workspace); |
| 85 |
}</pre> |
| 86 |
<p>The <a href="http://qt.nokia.com/doc/4.6/designer-to-know.html">MainWindow</a> constructor creates a minimal user interface.</p> |
| 87 |
<pre> void MainWindow::handleMessage(const QString& message) |
| 88 |
{ |
| 89 |
enum Action { |
| 90 |
Nothing, |
| 91 |
Open, |
| 92 |
Print |
| 93 |
} action; |
| 94 |
|
| 95 |
action = Nothing; |
| 96 |
QString filename = message; |
| 97 |
if (message.toLower().startsWith("/print ")) { |
| 98 |
filename = filename.mid(7); |
| 99 |
action = Print; |
| 100 |
} else if (!message.isEmpty()) { |
| 101 |
action = Open; |
| 102 |
} |
| 103 |
if (action == Nothing) { |
| 104 |
emit needToShow(); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
QFile file(filename); |
| 109 |
QString contents; |
| 110 |
if (file.open(QIODevice::ReadOnly)) |
| 111 |
contents = file.readAll(); |
| 112 |
else |
| 113 |
contents = "[[Error: Could not load file " + filename + "]]"; |
| 114 |
|
| 115 |
QTextEdit *view = new QTextEdit; |
| 116 |
view->setPlainText(contents); |
| 117 |
|
| 118 |
switch(action) {</pre> |
| 119 |
<p>The handleMessage() slot interprets the message passed in as a filename that can be prepended with <i>/print</i> to indicate that the file should just be printed rather than loaded.</p> |
| 120 |
<pre> case Print: |
| 121 |
{ |
| 122 |
QPrinter printer; |
| 123 |
view->print(&printer); |
| 124 |
delete view; |
| 125 |
} |
| 126 |
break; |
| 127 |
|
| 128 |
case Open: |
| 129 |
{ |
| 130 |
workspace->addSubWindow(view); |
| 131 |
view->setWindowTitle(message); |
| 132 |
view->show(); |
| 133 |
emit needToShow(); |
| 134 |
} |
| 135 |
break; |
| 136 |
default: |
| 137 |
break; |
| 138 |
}; |
| 139 |
}</pre> |
| 140 |
<p>Loading the file will also activate the window.</p> |
| 141 |
<pre> #include "main.moc" |
| 142 |
|
| 143 |
int main(int argc, char **argv) |
| 144 |
{ |
| 145 |
QtSingleApplication instance("File loader QtSingleApplication example", argc, argv); |
| 146 |
QString message; |
| 147 |
for (int a = 1; a < argc; ++a) { |
| 148 |
message += argv[a]; |
| 149 |
if (a < argc-1) |
| 150 |
message += " "; |
| 151 |
} |
| 152 |
|
| 153 |
if (instance.sendMessage(message)) |
| 154 |
return 0;</pre> |
| 155 |
<p>The <tt>main</tt> entry point function creates a <a href="qtsingleapplication.html">QtSingleApplication</a> object, and creates a message to send to a running instance of the application. If the message was sent successfully the process exits immediately.</p> |
| 156 |
<pre> MainWindow mw; |
| 157 |
mw.handleMessage(message); |
| 158 |
mw.show(); |
| 159 |
|
| 160 |
QObject::connect(&instance, SIGNAL(messageReceived(const QString&)), |
| 161 |
&mw, SLOT(handleMessage(const QString&))); |
| 162 |
|
| 163 |
instance.setActivationWindow(&mw, false); |
| 164 |
QObject::connect(&mw, SIGNAL(needToShow()), &instance, SLOT(activateWindow())); |
| 165 |
|
| 166 |
return instance.exec(); |
| 167 |
}</pre> |
| 168 |
<p>If the message could not be sent the application starts up. Note that <tt>false</tt> is passed to the call to setActivationWindow() to prevent automatic activation for every message received, e.g. when the application should just print a file. Instead, the message handling function determines whether activation is requested, and signals that by emitting the needToShow() signal. This is then simply connected directly to <a href="qtsingleapplication.html">QtSingleApplication</a>'s activateWindow() slot.</p> |
| 169 |
<p /><address><hr /><div align="center"> |
| 170 |
<table width="100%" cellspacing="0" border="0"><tr class="address"> |
| 171 |
<td width="30%" align="left">Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)</td> |
| 172 |
<td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td> |
| 173 |
<td width="30%" align="right"><div align="right">Qt Solutions</div></td> |
| 174 |
</tr></table></div></address></body> |
| 175 |
</html> |