https://techbase.kde.org/api.php?action=feedcontributions&user=Ta&feedformat=atom
KDE TechBase - User contributions [en]
2024-03-29T00:44:53Z
User contributions
MediaWiki 1.40.2
https://techbase.kde.org/index.php?title=Development/Tutorials/Printing_Hello_World&diff=5672
Development/Tutorials/Printing Hello World
2006-12-17T06:58:16Z
<p>Ta: syntax highlighting</p>
<hr />
<div>=The mission=<br />
Print '''Hello World''' on your printer.<br />
<br />
=The KDE version=<br />
This code will work for KDE 3 as for KDE 4.<br />
<br />
=The code=<br />
<code cppqt n><br />
#include <kprinter.h><br />
#include <qpainter.h><br />
#include <kapplication.h><br />
#include <kaboutdata.h><br />
#include <kmessagebox.h><br />
#include <kcmdlineargs.h><br />
<br />
/*<br />
This prints Hello World on your printer<br />
*/<br />
<br />
int main(int argc, char *argv[])<br />
{<br />
KAboutData aboutData( "test", "test",<br />
"1.0", "test", KAboutData::License_GPL,<br />
"(c) 2006" );<br />
KCmdLineArgs::init( argc, argv, &aboutData );<br />
KApplication khello;<br />
<br />
KPrinter job;<br />
job.setFullPage( true );<br />
if ( job.setup() )<br />
{<br />
QPainter painter;<br />
painter.begin( &job );<br />
painter.drawText(100,100,"Hello World");<br />
painter.end(); <br />
// this makes the print job start<br />
}<br />
}<br />
</code><br />
<br />
=Explanation=<br />
You need a KDE instance to print, because this instance stores your configuration, including your printer configuration. You create one by instanciating the KApplication class.<br />
<br />
=How to compile=<br />
==With KDE 4==<br />
Quite complicated:<br />
gcc print.cpp -o print -I/home/kde-devel/qt-unstable/include/Qt \<br />
-I/home/kde-devel/qt-unstable/include/Qt-Core \<br />
-I/home/kde-devel/qt-unstable/include -I/home/kde-devel/kde/include \<br />
-L/home/kde-devel/kde/lib -L/home/kde-devel/qt-unstable/lib -lkdeui \<br />
-lkdecore -ldl -lkdeprint<br />
<br />
==With KDE 3==<br />
Quite easy:<br />
gcc printtest.cpp -o print -I/usr/lib/qt3/include -I/opt/kde3/include \<br />
-L/opt/kde3/lib -L/usr/lib/qt3/lib -lqt-mt -lkdeprint<br />
<br />
=See also=<br />
* Point your konqueror to [http://developer.kde.org/documentation/library/cvs-api/kdelibs-apidocs/kdeprint/html/classKPrinter.html kde:kprinter]<br />
{{KDE3}}{{KDE4}}</div>
Ta
https://techbase.kde.org/index.php?title=Development/Tutorials/Programming_Tutorial_KDE_4/How_to_write_an_XML_parser&diff=5671
Development/Tutorials/Programming Tutorial KDE 4/How to write an XML parser
2006-12-17T06:51:14Z
<p>Ta: syntax highlighting</p>
<hr />
<div>A parser is used to distinguish between formal language and bulk data of a given grammar. See http://en.wikipedia.org/wiki/Parser for more information. There are two ways to write a parser: to split up the content of a file into an object as known from object-oriented programming (the <b>DOM approach</b>) or to trigger a function everytime a reader occurs a given syntax tag (the <b>QXML approach</b>).<br />
<br />
=The QXML approach=<br />
<br />
<b>parser.h:</b><br />
<code cppqt n><br />
/*<br />
parser.h - demonstration of a parser in C++<br />
*/<br />
<br />
#ifndef PARSER_H<br />
#define PARSER_H<br />
<br />
#include <qstring.h><br />
#include <QtXml/QXmlDefaultHandler><br />
#include <QtXml/QXmlAttributes><br />
<br />
class Parser : public QXmlDefaultHandler<br />
{<br />
public:<br />
<br />
Parser();<br />
<br />
/** given by the framework from qxml. Called when parsing the xml-document starts. */<br />
bool startDocument();<br />
<br />
/** given by the framework from qxml. Called when the reader occurs an open tag (e.g. \<b\> ) */<br />
bool startElement( const QString&, const QString&, const QString& qName, const QXmlAttributes& att );<br />
<br />
};<br />
<br />
<br />
#endif<br />
</code><br />
<br />
<b>parser.cpp:</b><br />
<code cppqt n><br />
/*<br />
parser.cpp - demonstration of a parser in C++<br />
*/<br />
<br />
#include "parser.h"<br />
#include <kdebug.h><br />
<br />
Parser::Parser()<br />
{<br />
}<br />
<br />
bool Parser::startDocument()<br />
{<br />
kDebug() << "Searching document for tags" << endl;<br />
return true;<br />
}<br />
<br />
bool Parser::startElement( const QString&, const QString&, const QString& qName, const QXmlAttributes& att )<br />
{<br />
kDebug() << "Found Element " << qName << endl;<br />
return true;<br />
}<br />
</code><br />
<br />
<b>hello.cpp:</b><br />
<code cppqt n><br />
/*<br />
hello.cpp<br />
compile it with<br />
g++ -I. -I/home/kde-devel/kde/include -I/home/kde-devel/qt-unstable/include/Qt -I/home/kde-devel/qt-unstable/include /home/kde-devel/qt-unstable/include/QtXml parser.h parser.cpp hello.cpp -L/home/kde-devel/kde/lib -L/home/kde-devel/qt-unstable/lib -lQtCore_debug -lQtXml_debug -lkdeui<br />
*/<br />
<br />
<br />
#include <qstring.h><br />
#include <QXmlInputSource><br />
#include <qfile.h><br />
#include <parser.h><br />
<br />
int main()<br />
{ <br />
Parser* handler=new Parser();<br />
QXmlInputSource* source=new QXmlInputSource(new QFile("hello.htm"));<br />
QXmlSimpleReader reader;<br />
reader.setContentHandler( handler );<br />
reader.parse( source );<br />
}<br />
</code><br />
<br />
=The DOM approach=<br />
<code cppqt n><br />
/*<br />
dom.cpp<br />
A demonstration how to use the dom parsing framework.<br />
Prints the first subnode of an HTML file, i.e. typically <br />
"head" or "body".<br />
compile it like this:<br />
g++ -I. -I/opt/kde3/include -I/usr/lib/qt3/include dom.cpp \<br />
-L/opt/kde3/lib -L/usr/lib/qt3/lib -lqt-mt -lkdeui <br />
*/<br />
#include <qdom.h><br />
#include <qfile.h><br />
#include <kdebug.h><br />
<br />
int main()<br />
{<br />
QDomDocument doc( "myDocument" );<br />
QFile qf("hello.htm");<br />
doc.setContent( &qf );<br />
QDomElement docElement = doc.documentElement(); <br />
QDomNode node;<br />
node = docElement.firstChild();<br />
kdDebug() << node.nodeName() << endl;<br />
}<br />
</code><br />
<br />
=Drawbacks=<br />
HTML parsing only works for "legal" html documents. <br />
For example, look at this code:<br />
<code><br />
<pre><br />
<html><br />
<body><br />
<a href="http://www.kde.org/"></a><br />
<a href="/index.php?title=Special:User&returnto=Main_Page">Log in</a><br />
<a href="http://www.gmx.de"></a><br />
</body><br />
</html><br />
</pre><br />
</code><br />
This code contains a &amp; and will bring your parser to an error.<br />
<br />
See here:<br />
<code><br />
<pre><br />
<html><br />
<body><br />
<a href="http://www.kde.org/"></a><br />
<a href="/index.php" nowrap>Log in</a><br />
<a href="http://www.gmx.de"></a><br />
</body><br />
</html><br />
</pre><br />
</code><br />
This code will throw an error because of the '''nowrap''' that is not xml-conform.</div>
Ta