| Home | ![]() |
#include <qtsingleapplication.h>
#include <qfile.h>
#include <qmainwindow.h>
#include <qprinter.h>
#include <qpaintdevicemetrics.h>
#include <qpainter.h>
#include <qtextview.h>
#include <qworkspace.h>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
public slots:
void handleMessage(const QString& message);
private:
QWorkspace *workspace;
};
The user interface in this application is a QMainWindow subclass
with a QWorkspace as the central widget. It implements a slot
handleMessage() that will be connected to the messageReceived()
signal of the QtSingleApplication class.
MainWindow::MainWindow()
{
workspace = new QWorkspace(this);
setCentralWidget(workspace);
}
The MainWindow constructor creates a minimal user interface.
void MainWindow::handleMessage(const QString& message)
{
enum Action {
Nothing,
Open,
Print
} action;
action = Nothing;
QString filename = message;
if (message.lower().startsWith("/print ")) {
filename = filename.mid(7);
action = Print;
} else if (!message.isEmpty()) {
action = Open;
}
if (action == Nothing)
return;
QFile file(filename);
if (!file.open(IO_ReadOnly))
return;
QTextStream ts(&file);
QString contents = ts.read();
switch(action) {
The handleMessage() slot interprets the message passed in as a
filename that can be prepended with /print to indicate that
the file should be printed rather than loaded.
case Print:
{
QPrinter printer;
QPainter p(&printer);
if (!p.isActive())
return;
p.setFont(font());
QFontMetrics fm = p.fontMetrics();
QPaintDeviceMetrics metrics(&printer);
const int lines = contents.contains("\n");
const int Margin = 10;
int yPos = 0;
for( int i = 0 ; i < lines ; i++ ) {
if ( Margin + yPos > metrics.height() - Margin ) {
printer.newPage();
yPos = 0;
}
int nl = contents.find("\n");
QString line = contents.left(nl);
contents = contents.mid(nl+1);
p.drawText(Margin, Margin + yPos, metrics.width(), fm.lineSpacing(),
ExpandTabs | DontClip, line);
yPos = yPos + fm.lineSpacing();
}
}
break;
case Open:
{
QTextView *view = new QTextView(workspace);
view->setText(contents);
view->setCaption(message);
view->setModified(FALSE);
view->show();
setActiveWindow();
}
break;
default:
break;
};
}
Loading the file will also activate the window.
#include "main.moc"
int main(int argc, char **argv)
{
QtSingleApplication instance("QtSingleApplication File Loader", argc, argv);
QString message;
for (int a = 1; a < argc; ++a) {
message += argv[a];
if (a < argc-1)
message += " ";
}
if (instance.sendMessage(message))
return 0;
The main entry point function creates a QtSingleApplication
object, and creates a message to send to a running instance
of the application. If the message was sent successfully the
process exits immediately.
instance.initialize(FALSE);
MainWindow mw;
mw.handleMessage(message);
mw.show();
QObject::connect(&instance, SIGNAL(messageReceived(const QString&)),
&mw, SLOT(handleMessage(const QString&)));
instance.setMainWidget(&mw);
return instance.exec();
}
If the message could not be sent the application starts up.
Note that FALSE is passed to the call to initialize() to
prevent automatic activation of the instance for every message
received, e.g. when the application should just print a file.
| Copyright © 2003-2006 Trolltech | Trademarks | Qt Solutions
|