<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://techbase.kde.org/skins/common/feed.css?0.2"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://techbase.kde.org/api.php?action=feedcontributions&amp;user=Fresbeeplayer&amp;feedformat=atom</id>
		<title>KDE TechBase - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://techbase.kde.org/api.php?action=feedcontributions&amp;user=Fresbeeplayer&amp;feedformat=atom"/>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Special:Contributions/Fresbeeplayer"/>
		<updated>2013-05-20T12:49:09Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.20.2</generator>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2009-04-07T20:11:17Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Fix delayed initialization link&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere mal utilizzati o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;controllare il passaggio file-static&amp;lt;/font&amp;gt;&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;un namespace anonimo non ha linkage interno?&amp;lt;/font&amp;gt;&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile con qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferisci l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator assicurati di stare chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;; altrimenti, a meno che il tuo container sia esso stesso costante, potrebbero esserci detach non necessari del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
// codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QList&amp;lt;QualcheClasse&amp;gt;::ConstIterator end = container.constEnd();&lt;br /&gt;
QList&amp;lt;QualcheClasse&amp;gt;::ConstIterator itr = container.constBegin();&lt;br /&gt;
&lt;br /&gt;
for ( ; itr != end; ++itr ) {&lt;br /&gt;
  // usa *itr (oppure itr.value()) qui dentro&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
Ogni volta che usi gli iteratori, utilizza sempre operatori di pre-incremento e pre-decremento (ad esempio, &amp;lt;tt&amp;gt;++itr&amp;lt;/tt&amp;gt;) a meno di avere uno motivo specifico per non farlo. L'utilizzo di operatori di post-incrementi e post-decrementi (come &amp;lt;tt&amp;gt;itr++&amp;lt;/tt&amp;gt;) causano la creazione di un oggetto temporaneo.&lt;br /&gt;
&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;approfondire timer e job&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== Falle nella memoria ===&lt;br /&gt;
&lt;br /&gt;
Un errore di programmazione molto &amp;quot;popolare&amp;quot; consiste nel fare un &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; senza un &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; come in questo programma:&lt;br /&gt;
&lt;br /&gt;
'''mem_buongustaio.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void inquina()&lt;br /&gt;
{&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) inquina();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come puoi vedere, ''inquina()'' istanzia un nuovo oggetto ''inquinatore'' di tipo ''t''. Quindi, la variabile ''inquinatore'' viene persa dato che è locale, ma il contenuto (l'oggetto) rimane nello heap. Posso usare questo programma per rendere il mio computer inutilizzabile in 10 secondi.&lt;br /&gt;
&lt;br /&gt;
Per risolvere, ci sono i seguenti approcci:&lt;br /&gt;
* tieni la variabile nello stack invece che nello heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
diventerà&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t inquinatore();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* cancella l'inquinatore usando la funzione complementare a new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete inquinatore;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uno strumento per individuare le falle di memoria come queste è [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
Puoi fare un dynamic_cast al tipo T dal tipo T2 tali che:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare e approfondire&amp;lt;/font&amp;gt;&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported --&amp;gt;&lt;br /&gt;
* T è definito in una libreria a cui fai il link (avrai un errore dal linker se non è così, dal momento che non troverà le informazioni vtable e RTTI)&lt;br /&gt;
* T è &amp;quot;ben-ancorato&amp;quot; in quella libreria. Con &amp;quot;ben-ancorato&amp;quot; intendo che vtable non è un simbolo COMUNE soggetto a fusioni a run-time da parte del linker dinamico. In altre parole, il primo membro virtuale nella definizione della classe deve esistere e non essere inline: deve essere in un file .cpp.&lt;br /&gt;
* T e T2 sono esportati.&lt;br /&gt;
&lt;br /&gt;
Per esempio, noi abbiamo incontrato qualche problema difficile da individuare nel codice C++ non KDE (NMM credo) a cui stavamo facendo il link:&lt;br /&gt;
* libphonon carica il plugin NMM&lt;br /&gt;
* il plugin NMM fa il link a NMM&lt;br /&gt;
* NMM carica i suoi plugins&lt;br /&gt;
* i plugins propri di NMM, fanno il link a NMM&lt;br /&gt;
&lt;br /&gt;
Qualche classe nella libreria di NMM non aveva vtables ben-ancorate, così il dynamic_casting falliva dentro al plugin NMM di Phonon per gli oggetti creatii nei plugins di NMM&lt;br /&gt;
&lt;br /&gt;
== Progettazione delle applicazioni ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione copriremo un po' di problemi comuni relativi alla progettazione di applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Inizializzazione ritardata ===&lt;br /&gt;
&lt;br /&gt;
Sebbene il design di moderne applicazioni C++ può essere molto complesso, un problema ricorrente, generalmente facile da sistemare, è il non usare la tecnica dell'[http://www.kdedevelopers.org/node/509 inizializzazione ritardata].&lt;br /&gt;
&lt;br /&gt;
Per prima cosa, diamo un'occhiata al modo standard di inizializzare un'applicazione KDE:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Nota che &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; viene creata prima di &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; il quale fa partire il ciclo degli eventi. Ciò implica che vogliamo evitare di fare cose non banali nella parte alta del costruttore, visto che verranno eseguite prima ancora che la finestra venga mostrata.&lt;br /&gt;
&lt;br /&gt;
La soluzione è semplice: abbiamo bisogno di ritardare la costruzione di qualunque cosa oltre alla GUI fino a che il ciclo degli eventi sia partito. Qui di seguito è mostrato come il costruttore della classe MainWindow dovrebbe essere per ottenere questo risultato:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Costruisci i tuoi widget qui. Nota che non devono&lt;br /&gt;
     * richiedere una complessa inizializzazione,&lt;br /&gt;
     * o verrà meno lo scopo di questa tecnica.&lt;br /&gt;
     * Tutto ciò che vorresti fare è creare i tuoi oggetti&lt;br /&gt;
     * della GUI ed usare QObject::connect per connettere&lt;br /&gt;
     * i segnali agli slots appropriati.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* Questo slot sarà chiamato non appena parte il ciclo eventi.&lt;br /&gt;
     * Metti tutto il resto che deve essere fatto, compresi&lt;br /&gt;
     * assegnazione di valori, lettura files, ristebilire sessioni, etc...&lt;br /&gt;
     * Tutto ciò prenderà lo stesso del tempo, ma almeno la tua&lt;br /&gt;
     * finestra sarà visibile a schermo, facendo apparire&lt;br /&gt;
     * attiva l'applicazione.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Usare questa tecnica potrebbe non far risparmiare del tempo in più, ma farà ''sembrare'' più veloce l'applicazione agli utenti che la eseguono. Questa percezione di reattività incrementata è rassicurante per l'utente il quale riceve un rapido feedback per il riuscito avvio dell'applicazione.&lt;br /&gt;
&lt;br /&gt;
Quando (e solo in questo caso) l'avvio non può essere reso ragionevolmente abbastanza veloce, considera l'uso di {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Strutture Dati ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione spazieremo su alcune delle nostre più comuni persecuzioni che affliggono le più comuni strutture dati viste nelle applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Passaggio di tipi non POD ===&lt;br /&gt;
&lt;br /&gt;
I tipi di dato diversi da POD ([http://en.wikipedia.org/wiki/Plain_old_data &amp;quot;Plain Old Data&amp;quot;], dati semplici senza la logica di controllo) dovrebbero essere passati sempre per riferimento costante. Questo include qualunque cosa tranne i tipi base come &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Prendi, per esempio, {{qt|QString}}. Dovrebbero sempre essere passati ai metodi come &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Anche se {{qt|QString}} è implicitamente condiviso è comunque più efficiente (e sicuro) passarlo per referenza costante piuttosto che come oggetto per valore.&lt;br /&gt;
&lt;br /&gt;
Quindi la dichiarazione canonica di un metodo che prende QString come argomento è:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void mioMetodo( const QString &amp;amp; x, const QString &amp;amp; y );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
Se avrai mai bisogno di eliminare una classe derivata da QObject dall'interno di uno dei suoi metodi, non farlo mai in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima o poi ciò causerà un crash perché un metodo di quell'oggetto potrebbe essere invocato dal ciclo eventi delle Qt via slots/signals dopo che tu l'hai eliminato.&lt;br /&gt;
&lt;br /&gt;
Invece usa sempre &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; il quale cerca di fare la stessa cosa di &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; ma in modo più sicuro.&lt;br /&gt;
&lt;br /&gt;
=== QStrings vuote ===&lt;br /&gt;
&lt;br /&gt;
Si è soliti voler verificare se un {{qt|QString}} è vuoto. Di seguito ci sono tre modi per farlo, dei quali i primi due sono corretti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sbagliato! &amp;quot;&amp;quot;&lt;br /&gt;
if ( miaStringa == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mentre c'è distinzione tra {{qt|QString}} nulle e vuote, ciò è puramente un artefatto storico e per il nuovo codice se ne scoraggia l'uso.&lt;br /&gt;
&lt;br /&gt;
=== QString e lettura da file ===&lt;br /&gt;
&lt;br /&gt;
Se stai leggendo un file, è più veloce convertirlo dalla codifica locale a Unicode ({{qt|QString}}) tutto in una volta, piuttosto che riga per riga. Questo vuol dire che metodi come &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; sono spesso una buona soluzione, seguiti da una singola istanza di {{qt|QString}}.&lt;br /&gt;
&lt;br /&gt;
Per file molto grandi, considera la possibilità di leggere un blocco di righe e quindi eseguire la conversione. In questo modo hai l'opportunità di aggiornare la GUI. Ciò può essere fatto rientrando normalmente nel ciclo eventi, contemporaneamente utilizzando un timer per leggere i blocchi in background, oppure creando un ciclo eventi locale.&lt;br /&gt;
&lt;br /&gt;
Anche se si potrebbe usare anche &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, è scoraggiato siccome porta facilmente a subdoli problemi fatali.&lt;br /&gt;
&lt;br /&gt;
=== Leggere QString da un KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emette il segnale &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; appena arrivano dei dati.&lt;br /&gt;
Un errore comune sta nel leggere tutti i dati disponibili nello slot connesso e convertirli in un {{qt|QString}} così come sono: i dati arrivano arbitrariamente segmentati, così caratteri multi-byte potrebbero essere tagliati in pezzi e perciò invalidati. Esistono molti approcci al problema:&lt;br /&gt;
&lt;br /&gt;
* Hai veramente bisogno di processare i dati che arrivano? Se no, basta utilizzare &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; dopo che il processo è uscito. Diversamente da KDE3, KProcess è ora capace di accumulare i dati per te.&lt;br /&gt;
* Racchiudi il processo in un {{qt|QTextStream}} e leggi intere righe. Questo dovrebbe funzionare a partire dalle Qt 4.4&lt;br /&gt;
* Accumula i pezzi di dati negli slots e processali ogni volta che arriva una riga e dopo un certo timeout. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Codice di esempio]&lt;br /&gt;
&lt;br /&gt;
=== QString e QByteArray ===&lt;br /&gt;
&lt;br /&gt;
Mentre {{qt|QString}} è lo strumento scelto per molte situazioni che richiedono la gestione di stringhe, ce n'è una dove è particolarmente inefficiente. Se stai lavorando con dati in un {{qt|QByteArray}}, fai attenzione a non passarlo a metodi che prendono parametri di tipo {{qt|QString}}, e che ne tirano fuori un QByteArrays di nuovo.&lt;br /&gt;
&lt;br /&gt;
Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QString mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QString maciullaDati( const QString&amp;amp; dati ) {&lt;br /&gt;
    QByteArray str = dati.toLatin1();&lt;br /&gt;
    // maciulla&lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
L'operazione dispendiosa che si verifica è la conversione in {{qt|QString}}, la quale internamente fa conversioni a Unicode. Ciò è inutile perché la prima cosa che il metodo fa è riconvertirla di nuovo con &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. Quindi, se sei sicuro che la conversione a Unicode non è necessaria, cerca di evitare di usare inavvertitamente QString.&lt;br /&gt;
&lt;br /&gt;
L'esempio di cui sopra dovrebbe invece essere scritto come:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QByteArray mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QByteArray maciullaDati( const QByteArray&amp;amp; dati )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
Quando si deve fare il paring di documenti XML, si ha bisogno di iterare su tutti gli elementi. Potresti essere tentato di usare il seguente codice:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ebbene non è corretto: questo ciclo si fermerà prematuramente non appena incontra un {{qt|QDomNode}} che è qualcosa di diverso da un elemento, come un commento.&lt;br /&gt;
&lt;br /&gt;
Il ciclo corretto è qualcosa di simile:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-12-10T16:42:55Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: sincronizzazione con il testo inglese&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere mal utilizzati o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;controllare il passaggio file-static&amp;lt;/font&amp;gt;&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;un namespace anonimo non ha linkage interno?&amp;lt;/font&amp;gt;&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile con qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferisci l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator assicurati di stare chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;; altrimenti, a meno che il tuo container sia esso stesso costante, potrebbero esserci detach non necessari del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
// codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QList&amp;lt;QualcheClasse&amp;gt;::ConstIterator end = container.constEnd();&lt;br /&gt;
QList&amp;lt;QualcheClasse&amp;gt;::ConstIterator itr = container.constBegin();&lt;br /&gt;
&lt;br /&gt;
for ( ; itr != end; ++itr ) {&lt;br /&gt;
  // usa *itr (oppure itr.value()) qui dentro&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
Ogni volta che usi gli iteratori, utilizza sempre operatori di pre-incremento e pre-decremento (ad esempio, &amp;lt;tt&amp;gt;++itr&amp;lt;/tt&amp;gt;) a meno di avere uno motivo specifico per non farlo. L'utilizzo di operatori di post-incrementi e post-decrementi (come &amp;lt;tt&amp;gt;itr++&amp;lt;/tt&amp;gt;) causano la creazione di un oggetto temporaneo.&lt;br /&gt;
&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;approfondire timer e job&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== Falle nella memoria ===&lt;br /&gt;
&lt;br /&gt;
Un errore di programmazione molto &amp;quot;popolare&amp;quot; consiste nel fare un &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; senza un &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; come in questo programma:&lt;br /&gt;
&lt;br /&gt;
'''mem_buongustaio.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void inquina()&lt;br /&gt;
{&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) inquina();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come puoi vedere, ''inquina()'' istanzia un nuovo oggetto ''inquinatore'' di tipo ''t''. Quindi, la variabile ''inquinatore'' viene persa dato che è locale, ma il contenuto (l'oggetto) rimane nello heap. Posso usare questo programma per rendere il mio computer inutilizzabile in 10 secondi.&lt;br /&gt;
&lt;br /&gt;
Per risolvere, ci sono i seguenti approcci:&lt;br /&gt;
* tieni la variabile nello stack invece che nello heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
diventerà&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t inquinatore();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* cancella l'inquinatore usando la funzione complementare a new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete inquinatore;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uno strumento per individuare le falle di memoria come queste è [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
Puoi fare un dynamic_cast al tipo T dal tipo T2 tali che:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare e approfondire&amp;lt;/font&amp;gt;&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported --&amp;gt;&lt;br /&gt;
* T è definito in una libreria a cui fai il link (avrai un errore dal linker se non è così, dal momento che non troverà le informazioni vtable e RTTI)&lt;br /&gt;
* T è &amp;quot;ben-ancorato&amp;quot; in quella libreria. Con &amp;quot;ben-ancorato&amp;quot; intendo che vtable non è un simbolo COMUNE soggetto a fusioni a run-time da parte del linker dinamico. In altre parole, il primo membro virtuale nella definizione della classe deve esistere e non essere inline: deve essere in un file .cpp.&lt;br /&gt;
* T e T2 sono esportati.&lt;br /&gt;
&lt;br /&gt;
Per esempio, noi abbiamo incontrato qualche problema difficile da individuare nel codice C++ non KDE (NMM credo) a cui stavamo facendo il link:&lt;br /&gt;
* libphonon carica il plugin NMM&lt;br /&gt;
* il plugin NMM fa il link a NMM&lt;br /&gt;
* NMM carica i suoi plugins&lt;br /&gt;
* i plugins propri di NMM, fanno il link a NMM&lt;br /&gt;
&lt;br /&gt;
Qualche classe nella libreria di NMM non aveva vtables ben-ancorate, così il dynamic_casting falliva dentro al plugin NMM di Phonon per gli oggetti creatii nei plugins di NMM&lt;br /&gt;
&lt;br /&gt;
== Progettazione delle applicazioni ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione copriremo un po' di problemi comuni relativi alla progettazione di applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Inizializzazione ritardata ===&lt;br /&gt;
&lt;br /&gt;
Sebbene il design di moderne applicazioni C++ può essere molto complesso, un problema ricorrente, generalmente facile da sistemare, è il non usare la tecnica dell'[http://www.kdedevelopers.org/node/view/509 inizializzazione ritardata].&lt;br /&gt;
&lt;br /&gt;
Per prima cosa, diamo un'occhiata al modo standard di inizializzare un'applicazione KDE:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Nota che &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; viene creata prima di &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; il quale fa partire il ciclo degli eventi. Ciò implica che vogliamo evitare di fare cose non banali nella parte alta del costruttore, visto che verranno eseguite prima ancora che la finestra venga mostrata.&lt;br /&gt;
&lt;br /&gt;
La soluzione è semplice: abbiamo bisogno di ritardare la costruzione di qualunque cosa oltre alla GUI fino a che il ciclo degli eventi sia partito. Qui di seguito è mostrato come il costruttore della classe MainWindow dovrebbe essere per ottenere questo risultato:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Costruisci i tuoi widget qui. Nota che non devono&lt;br /&gt;
     * richiedere una complessa inizializzazione,&lt;br /&gt;
     * o verrà meno lo scopo di questa tecnica.&lt;br /&gt;
     * Tutto ciò che vorresti fare è creare i tuoi oggetti&lt;br /&gt;
     * della GUI ed usare QObject::connect per connettere&lt;br /&gt;
     * i segnali agli slots appropriati.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* Questo slot sarà chiamato non appena parte il ciclo eventi.&lt;br /&gt;
     * Metti tutto il resto che deve essere fatto, compresi&lt;br /&gt;
     * assegnazione di valori, lettura files, ristebilire sessioni, etc...&lt;br /&gt;
     * Tutto ciò prenderà lo stesso del tempo, ma almeno la tua&lt;br /&gt;
     * finestra sarà visibile a schermo, facendo apparire&lt;br /&gt;
     * attiva l'applicazione.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Usare questa tecnica potrebbe non far risparmiare del tempo in più, ma farà ''sembrare'' più veloce l'applicazione agli utenti che la eseguono. Questa percezione di reattività incrementata è rassicurante per l'utente il quale riceve un rapido feedback per il riuscito avvio dell'applicazione.&lt;br /&gt;
&lt;br /&gt;
Quando (e solo in questo caso) l'avvio non può essere reso ragionevolmente abbastanza veloce, considera l'uso di {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Strutture Dati ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione spazieremo su alcune delle nostre più comuni persecuzioni che affliggono le più comuni strutture dati viste nelle applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Passaggio di tipi non POD ===&lt;br /&gt;
&lt;br /&gt;
I tipi di dato diversi da POD ([http://en.wikipedia.org/wiki/Plain_old_data &amp;quot;Plain Old Data&amp;quot;], dati semplici senza la logica di controllo) dovrebbero essere passati sempre per riferimento costante. Questo include qualunque cosa tranne i tipi base come &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Prendi, per esempio, {{qt|QString}}. Dovrebbero sempre essere passati ai metodi come &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Anche se {{qt|QString}} è implicitamente condiviso è comunque più efficiente (e sicuro) passarlo per referenza costante piuttosto che come oggetto per valore.&lt;br /&gt;
&lt;br /&gt;
Quindi la dichiarazione canonica di un metodo che prende QString come argomento è:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void mioMetodo( const QString &amp;amp; x, const QString &amp;amp; y );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
Se avrai mai bisogno di eliminare una classe derivata da QObject dall'interno di uno dei suoi metodi, non farlo mai in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima o poi ciò causerà un crash perché un metodo di quell'oggetto potrebbe essere invocato dal ciclo eventi delle Qt via slots/signals dopo che tu l'hai eliminato.&lt;br /&gt;
&lt;br /&gt;
Invece usa sempre &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; il quale cerca di fare la stessa cosa di &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; ma in modo più sicuro.&lt;br /&gt;
&lt;br /&gt;
=== QStrings vuote ===&lt;br /&gt;
&lt;br /&gt;
Si è soliti voler verificare se un {{qt|QString}} è vuoto. Di seguito ci sono tre modi per farlo, dei quali i primi due sono corretti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sbagliato! &amp;quot;&amp;quot;&lt;br /&gt;
if ( miaStringa == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mentre c'è distinzione tra {{qt|QString}} nulle e vuote, ciò è puramente un artefatto storico e per il nuovo codice se ne scoraggia l'uso.&lt;br /&gt;
&lt;br /&gt;
=== QString e lettura da file ===&lt;br /&gt;
&lt;br /&gt;
Se stai leggendo un file, è più veloce convertirlo dalla codifica locale a Unicode ({{qt|QString}}) tutto in una volta, piuttosto che riga per riga. Questo vuol dire che metodi come &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; sono spesso una buona soluzione, seguiti da una singola istanza di {{qt|QString}}.&lt;br /&gt;
&lt;br /&gt;
Per file molto grandi, considera la possibilità di leggere un blocco di righe e quindi eseguire la conversione. In questo modo hai l'opportunità di aggiornare la GUI. Ciò può essere fatto rientrando normalmente nel ciclo eventi, contemporaneamente utilizzando un timer per leggere i blocchi in background, oppure creando un ciclo eventi locale.&lt;br /&gt;
&lt;br /&gt;
Anche se si potrebbe usare anche &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, è scoraggiato siccome porta facilmente a subdoli problemi fatali.&lt;br /&gt;
&lt;br /&gt;
=== Leggere QString da un KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emette il segnale &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; appena arrivano dei dati.&lt;br /&gt;
Un errore comune sta nel leggere tutti i dati disponibili nello slot connesso e convertirli in un {{qt|QString}} così come sono: i dati arrivano arbitrariamente segmentati, così caratteri multi-byte potrebbero essere tagliati in pezzi e perciò invalidati. Esistono molti approcci al problema:&lt;br /&gt;
&lt;br /&gt;
* Hai veramente bisogno di processare i dati che arrivano? Se no, basta utilizzare &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; dopo che il processo è uscito. Diversamente da KDE3, KProcess è ora capace di accumulare i dati per te.&lt;br /&gt;
* Racchiudi il processo in un {{qt|QTextStream}} e leggi intere righe. Questo dovrebbe funzionare a partire dalle Qt 4.4&lt;br /&gt;
* Accumula i pezzi di dati negli slots e processali ogni volta che arriva una riga e dopo un certo timeout. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Codice di esempio]&lt;br /&gt;
&lt;br /&gt;
=== QString e QByteArray ===&lt;br /&gt;
&lt;br /&gt;
Mentre {{qt|QString}} è lo strumento scelto per molte situazioni che richiedono la gestione di stringhe, ce n'è una dove è particolarmente inefficiente. Se stai lavorando con dati in un {{qt|QByteArray}}, fai attenzione a non passarlo a metodi che prendono parametri di tipo {{qt|QString}}, e che ne tirano fuori un QByteArrays di nuovo.&lt;br /&gt;
&lt;br /&gt;
Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QString mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QString maciullaDati( const QString&amp;amp; dati ) {&lt;br /&gt;
    QByteArray str = dati.toLatin1();&lt;br /&gt;
    // maciulla&lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
L'operazione dispendiosa che si verifica è la conversione in {{qt|QString}}, la quale internamente fa conversioni a Unicode. Ciò è inutile perché la prima cosa che il metodo fa è riconvertirla di nuovo con &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. Quindi, se sei sicuro che la conversione a Unicode non è necessaria, cerca di evitare di usare inavvertitamente QString.&lt;br /&gt;
&lt;br /&gt;
L'esempio di cui sopra dovrebbe invece essere scritto come:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QByteArray mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QByteArray maciullaDati( const QByteArray&amp;amp; dati )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
Quando si deve fare il paring di documenti XML, si ha bisogno di iterare su tutti gli elementi. Potresti essere tentato di usare il seguente codice:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ebbene non è corretto: questo ciclo si fermerà prematuramente non appena incontra un {{qt|QDomNode}} che è qualcosa di diverso da un elemento, come un commento.&lt;br /&gt;
&lt;br /&gt;
Il ciclo corretto è qualcosa di simile:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/First_program_(it)</id>
		<title>Development/Tutorials/First program (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/First_program_(it)"/>
				<updated>2008-12-05T23:37:07Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: sostituito app.exec() con 0 per far chiudere correttamente l'app&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/First_program}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Hello World|&lt;br /&gt;
&lt;br /&gt;
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KXmlGuiWindow_(it)|Tutorial 2 - KXmlGuiWindow]]| &lt;br /&gt;
&lt;br /&gt;
reading=[[Development/Tutorials/CMake|CMake]]&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Il tuo primo programma dovrebbe salutare il mondo con un amichevole &amp;quot;Ciao Mondo&amp;quot;, giusto? Per farlo, useremo un {{class|KMessageBox}} personalizzando uno dei pulsanti.&lt;br /&gt;
&lt;br /&gt;
[[image:Ciaomondo_tutorial1.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
{{tip|Per avere più informazioni su qualunque classe che incontri, Konqueror offre una veloce scorciatoia. Per cercare informazioni circa KMessageBox, basta digitare &amp;quot;kde:kmessagebox&amp;quot; in Konqueror e sarai portato alla documentazione.}}&lt;br /&gt;
&lt;br /&gt;
{{tip|Vorresti poter usare KDevelop per i tuoi progetti, il quale fa molte cose carine come il completamento del codice, facile accesso alla documentazione delle API, supporto per il debugging.&lt;br /&gt;
Leggi [[Getting_Started/Set_up_KDE_4_for_development#KDevelop|questo tutorial]] per configurare KDevelop correttamente. Puoi controllare se il setup è andato a buon fine aprendo un'applicazione KDE4 esistente con KDevelop.&lt;br /&gt;
Tuttavia hai ancora bisogno di editare a mano i files di CMake.}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Il Codice ==&lt;br /&gt;
&lt;br /&gt;
Tutto il codice di cui abbiamo bisogno starà in un file, &amp;lt;tt&amp;gt;main.cpp&amp;lt;/tt&amp;gt;. Crealo con il codice qua sotto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;KMessageBox&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&lt;br /&gt;
                         // Il nome del programma, usato internamente.&lt;br /&gt;
                         &amp;quot;tutorial1&amp;quot;,&lt;br /&gt;
                         // Il nome nel catalogo messaggi.&lt;br /&gt;
                         // Se nullo, verrà usato il nome del programma.&lt;br /&gt;
                         0,&lt;br /&gt;
                         // Una stringa con il nome del programma.&lt;br /&gt;
                         // Quella che verrà effettivamente visualizzata.&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 1&amp;quot;),&lt;br /&gt;
                         // La stringa con la versione del programma.&lt;br /&gt;
                         &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         // Piccola descrizione su cosa fa il programma.&lt;br /&gt;
                         ki18n(&amp;quot;Visualizza una finestra KMessageBox.&amp;quot;),&lt;br /&gt;
                         // La licenza con la quale il codice è rilasciato.&lt;br /&gt;
                         KAboutData::License_GPL,&lt;br /&gt;
                         // Copyright&lt;br /&gt;
                         ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         // Testo opzionale mostrato in &amp;quot;Informazioni su&amp;quot;.&lt;br /&gt;
                         // Può contenere qualunque informazione desiderata.&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         // La stringa con la homepage del programma.&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         // L'indirizzo email per la segnalazione di bug.&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
    KGuiItem yesButton( i18n( &amp;quot;Ciao&amp;quot; ), QString(),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un tooltip&amp;quot; ),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un testo di aiuto CheCos'è?.&amp;quot; ) );&lt;br /&gt;
    KMessageBox::questionYesNo( 0, i18n( &amp;quot;Ciao Mondo&amp;quot; ),&lt;br /&gt;
                                i18n( &amp;quot;Ciao&amp;quot; ), yesButton );&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Il primo codice specifico di KDE che incontriamo in questo programma è {{class|KAboutData}}. Questa è la classe utilizzata per immagazzinare le informazioni sul programma, come ad esempio una piccola descrizione, gli autori, o le informazioni sulla licenza. Praticamente ogni applicazione KDE dovrebbe usare questa classe.&lt;br /&gt;
&lt;br /&gt;
Poi arriviamo a {{class|KCmdLineArgs}}. Questa è la classe che si usa quando si vogliono specificare istruzioni da riga di comando per, ad esempio, aprire il programma con uno specifico file. Comunque, in questo tutorial, semplicemente lo inizializziamo con l'oggetto {{class|KAboutData}} che abbiamo appena creato, in modo da poter usare le opzioni &amp;lt;tt&amp;gt;--version&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;--author&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quindi adiamo a creare l'oggetto {{class|KApplication}}. Ciò bisogna farlo esattamente una volta in ogni programma, visto che è necessario per cose come l'[[Development/Tutorials/Localization/i18n|i18n]].&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo finito con le operazioni indispensabili di KDE, possiamo cominciare a fare cose interessanti con la nostra applicazione. Creeremo una finestra pop-up, ma personalizzeremo uno dei pulsanti. Per la personalizzazione avremo bisogno di un oggetto {{class|KGuiItem}}. Il primo argomento del costruttore di {{class|KGuiItem}} è il testo che apparirà sull'oggetto (nel nostro caso, un pulsante). Quindi ci sarà un opzione per inserire un'icona, ma non ne vogliamo una così gli mettiamo &amp;lt;tt&amp;gt;QString&amp;lt;/tt&amp;gt;. Impostiamo quindi il tooltip (quello che appare quando fermi il puntatore su un elemento) ed infine il testo &amp;quot;Che Cos'è?&amp;quot; (accessibile tramite click col tasto destro, con Shift-F1 oppure cliccando su &amp;quot;?&amp;quot; nella barra del titolo e poi sul pulsante &amp;quot;Ciao&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo il nostro pulsante, possiamo creare il nostro pop-up. Chiamiamo la funzione &amp;lt;tt&amp;gt;{{class|KMessageBox}}::questionYesNo()&amp;lt;/tt&amp;gt; la quale, in modo predefinito, crea una finestra con due pulsanti &amp;quot;Si&amp;quot; e &amp;quot;No&amp;quot;. Il secondo argomento compone il testo mostrato nella finestra sopra i due pulsanti. Il terzo è l'intestazione della finestra; e infine impostiamo il KGuiItem di (quello che normalmente è) il pulsante &amp;quot;Si&amp;quot; con il nostro &amp;lt;tt&amp;gt;KGuiItem yesButton&amp;lt;/tt&amp;gt; appena creato.&lt;br /&gt;
&lt;br /&gt;
Notare come tutto il testo visibile all'utente passa attraverso la funzione i18n(); questo è necessario per permettere la traduzione della UI. Più informazioni sulla localizzazione possono essere trovate nel [[Development/Tutorials/Localization/i18n|tutorial sulla localizzazione]].&lt;br /&gt;
&lt;br /&gt;
Abbiamo finito per quanto riguarda il codice. Ora la compilazione e la prova.&lt;br /&gt;
&lt;br /&gt;
== Compilazione ==&lt;br /&gt;
&lt;br /&gt;
Vorrai usare [[Development/Tutorials/CMake|CMake]] come ambiente di compilazione. Devi fornire un file chiamato CMakeLists.txt, cmake usa questo file per generare tutti i Makefiles.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
Crea un file di nome CMakeLists.txt nella stessa cartella di main.cpp con questo contenuto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial1)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial1_SRCS&lt;br /&gt;
  main.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial1 ${tutorial1_SRCS})&lt;br /&gt;
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial1  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
La funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; trova tutti i pacchetti che hai richiesto (in questo caso KDE4) ed imposta alcune variabili che descrivono la posizione degli header e delle librerie del pacchetto. In questo caso useremo la variabile &amp;lt;tt&amp;gt;KDE4_INCLUDES&amp;lt;/tt&amp;gt; contenente il percorso ai files header di KDE4.&lt;br /&gt;
&lt;br /&gt;
Per permettere al compilatore di trovare questi files, passiamo quella variabile alla funzione &amp;lt;tt&amp;gt;include_directories()&amp;lt;/tt&amp;gt; la quale aggiunge gli header di KDE4 al percorso di ricerca degli header.&lt;br /&gt;
&lt;br /&gt;
Poi creiamo una variabile chiamata &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; con la funzione &amp;lt;tt&amp;gt;set()&amp;lt;/tt&amp;gt;. In questo caso la impostiamo con il nome del nostro unico file sorgente.&lt;br /&gt;
&lt;br /&gt;
Quindi usiamo &amp;lt;tt&amp;gt;kde4_add_executable()&amp;lt;/tt&amp;gt; per creare un eseguibile chiamato &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; dai sorgenti elencati nella nostra variabile &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt;. In seguito, facciamo il link del nostro eseguibile alle librerie kdeui di KDE4 con &amp;lt;tt&amp;gt;target_link_libraries()&amp;lt;/tt&amp;gt; e la variabile &amp;lt;tt&amp;gt;KDE4_KDEUI_LIBS&amp;lt;/tt&amp;gt; precedentemente riempita dalla funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt;. La riga che comincia con &amp;lt;tt&amp;gt;install&amp;lt;/tt&amp;gt; scrive un target di default &amp;quot;install&amp;quot; dentro al Makefile.&lt;br /&gt;
&lt;br /&gt;
=== Make ed Esecuzione ===&lt;br /&gt;
&lt;br /&gt;
Puoi invocare CMake e make manualmente:&lt;br /&gt;
&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake .. # Nota questi due punti - non è una mancanza,&lt;br /&gt;
          # stanno per &amp;quot;cartella superiore&amp;quot;.&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
Oppure, se hai preparato il tuo ambiente come descritto in [[Getting_Started/Build/KDE4|Getting Started/Build/KDE4]], puoi compilare questo codice con:&lt;br /&gt;
 cmakekde&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Quindi lancialo con:&lt;br /&gt;
 ./tutorial1&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KXmlGuiWindow|usare KXmlGuiWindow]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development_(it)</id>
		<title>Development (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development_(it)"/>
				<updated>2008-12-01T14:57:34Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOEDITSECTION__ __NOTOC__ {{Template:I18n/Language Navigation Bar|Development}}&lt;br /&gt;
{| style=&amp;quot;margin: 1em 2.5% 0 2.5%; padding: 0 5px;&amp;quot; cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|colspan=2|[[Image:Discover.png|noframe]]&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot; |[[Image:Action_launch.svg|noframe|left|40px]] ||&lt;br /&gt;
;[[Development/Architecture_(it)|Architettura KDE]]&lt;br /&gt;
:Documenti sul design architetturale che spiegano le tecnologie alla base di KDE.&lt;br /&gt;
:''Riferimento'': [http://api.kde.org Documentazione API]&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot;|[[Image:Action_configure.svg|noframe|left|40px]] ||&lt;br /&gt;
;[[Development/Tutorials_(it)|Tutorials di programmazione]]&lt;br /&gt;
:Tutorials passo passo per lo sviluppo di KDE.&lt;br /&gt;
:''Riferimento:'' [[Development/Tools|Strumenti di sviluppo]] | [[Development/FAQs|FAQs]]&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot;|[[Image:Action_rebuild.svg|noframe|left|40px]] ||&lt;br /&gt;
;[[Development/Languages|Bindings ai linguaggi]]&lt;br /&gt;
:Linguaggi di programmazione supportati.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot;|[[Image:CMake-logo-48.png|noframe|left|48px]] ||&lt;br /&gt;
;[[Development/CMake|CMake]]&lt;br /&gt;
:Informazioni su CMake.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot; |[[Image:Action_note.svg|noframe|left|40px]] ||&lt;br /&gt;
;[[Development/Guidelines|Linee Guida &amp;amp; Standards]]&lt;br /&gt;
:Linee guida allo sviluppo e uso di tecniche standard per KDE.&lt;br /&gt;
:''Riferimento:'' [[Development/Further Information|Ulteriori informazioni]] (links, books, blogs, etc.)&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot; |[[Image:Action_tool.svg|noframe|left|40px]] ||&lt;br /&gt;
;[[Development/Software Engineering Framework|Framework Ingegneria del Software]]&lt;br /&gt;
:Strumenti e processi di Ingegneria del Software usati da KDE.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development_(it)</id>
		<title>Development (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development_(it)"/>
				<updated>2008-12-01T14:34:15Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: aggiunte due sezioni (cmake e ing software)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOEDITSECTION__ __NOTOC__ {{Template:I18n/Language Navigation Bar|Development}}&lt;br /&gt;
{| style=&amp;quot;margin: 1em 2.5% 0 2.5%; padding: 0 5px;&amp;quot; cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|colspan=2|[[Image:Discover.png|noframe]]&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot; |[[Image:Action_launch.svg|noframe|left|40px]] ||&lt;br /&gt;
;[[Development/Architecture|Architettura KDE]]&lt;br /&gt;
:Documenti sul design architetturale che spiegano le tecnologie alla base di KDE.&lt;br /&gt;
:''Riferimento'': [http://api.kde.org Documentazione API]&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot;|[[Image:Action_configure.svg|noframe|left|40px]] ||&lt;br /&gt;
;[[Development/Tutorials|Tutorials di programmazione]]&lt;br /&gt;
:Tutorials passo passo per lo sviluppo di KDE.&lt;br /&gt;
:''Riferimento:'' [[Development/Tools|Strumenti di sviluppo]] | [[Development/FAQs|FAQs]]&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot;|[[Image:Action_rebuild.svg|noframe|left|40px]] ||&lt;br /&gt;
;[[Development/Languages|Bindings ai linguaggi]]&lt;br /&gt;
:Linguaggi di programmazione supportati.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot;|[[Image:CMake-logo-48.png|noframe|left|48px]] ||&lt;br /&gt;
;[[Development/CMake|CMake]]&lt;br /&gt;
:Informazioni su CMake.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot; |[[Image:Action_note.svg|noframe|left|40px]] ||&lt;br /&gt;
;[[Development/Guidelines|Linee Guida &amp;amp; Standards]]&lt;br /&gt;
:Linee guida allo sviluppo e uso di tecniche standard per KDE.&lt;br /&gt;
:''Riferimento:'' [[Development/Further Information|Ulteriori informazioni]] (links, books, blogs, etc.)&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;padding-left: 50px;&amp;quot; |[[Image:Action_tool.svg|noframe|left|40px]] ||&lt;br /&gt;
;[[Development/Software Engineering Framework|Framework Ingegneria del Software]]&lt;br /&gt;
:Strumenti e processi di Ingegneria del Software usati da KDE.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials_(it)</id>
		<title>Development/Tutorials (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials_(it)"/>
				<updated>2008-11-29T12:05:51Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: primi due tutorial in itailano&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials}}&lt;br /&gt;
&lt;br /&gt;
I Tutorials sono la via piu' veloce di comprendere cosa KDE fa per te, e come lo fa.&lt;br /&gt;
Qui c'e' una lista dei tutorial disponibilie ''per KDE4''. Materiale per KDE3 e KDE2 e' disponibile in fondo alla pagina.&lt;br /&gt;
&lt;br /&gt;
== Introduzione alla programmazione di KDE 4 ==&lt;br /&gt;
&lt;br /&gt;
Sei interessato a scrivere applicazioni con KDE 4? Questa serie di tutorial e' mirata a questo completamente nuovo modo di programmare KDE.&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/First program (it)|Hello World]]&lt;br /&gt;
:''Un'introduzione preliminare alle basi della programmazione KDE4''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Using KXmlGuiWindow (it)|Creazione della finestra principale (Main Window)]]&lt;br /&gt;
:''Questo tutorial ti mostra la magia di un aspetto molto importante di un'applicazione: La finestra principale (main window).''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Using KActions|Usare le KActions]]&lt;br /&gt;
:''Come aggiungere eventi ai menu e alle toolbar.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Saving and loading|Salvataggio e Caricamento]]&lt;br /&gt;
:''Introduzione alla libreria KIO per il supporto al caricamento e salvataggio da una propria applicazione.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/KCmdLineArgs|Argomenti della linea di comando]]&lt;br /&gt;
:''Aggiungere la capacita' di specificare quale file aprire dalla riga di comando nel proprio editor di testo.''&lt;br /&gt;
&lt;br /&gt;
== Basi ==&lt;br /&gt;
;[[Development/Tutorials/KDE4 Porting Guide|Effettuare il porting delle tue applicazioni]]&lt;br /&gt;
:''Aiuto per il porting di applicazioni da Qt3/KDE3 a Qt4/KDE4''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/CMake|Introduzione a CMake]]&lt;br /&gt;
:''Come usare il sistema di build CMake usato da KDE 4.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Common Programming Mistakes|Errori di Programmazione Comuni]]&lt;br /&gt;
:''Svariati errori comuni fatti durante lo sviluppo di Qt e KDE e come neutralizzarli.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Using Qt Designer|Usare il Qt Designer per costruire interfacce utente]]&lt;br /&gt;
:''Come creare file UI con il designer, e come integrarli in un programma KDE.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Creating Libraries|Creare librerie per condividere il codice]]&lt;br /&gt;
:''Come aggiungere una libreria al sistema di build e come predisporre il codice sorgente.''&lt;br /&gt;
&lt;br /&gt;
== Testing e Debugging ==&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Debugging|Debugging delle tue applicazioni]]&lt;br /&gt;
:''Suggerimenti, strumenti e tecniche da applicare quando si esegue il debug delle proprie applicazioni KDE''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Unittests|Scrivere Unittests per Qt4 e KDE4 con QTestLib]] ([http://developer.kde.org/documentation/tutorials/writingunittests/writingunittests.html Link originale])&lt;br /&gt;
:''Tutorial da [mailto:bradh@frogmouth.net Brad Hards] che descrive come scrivere unit test usando il framework QTestLib. E' mostrato come un tutorial basato su esempi, ed e' in fase di sviluppo.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Code_Checking|Vie Semi-automatiche per rilevare errori nel codice]]&lt;br /&gt;
:''Tecniche che si possono usare per rilevare errori nel codice di KDE''&lt;br /&gt;
&lt;br /&gt;
== Gestire i dati di configurazione con KConfig ==&lt;br /&gt;
;[[Development/Tutorials/KConfig|Introduzione a KConfig]]&lt;br /&gt;
:''Una visione d'insieme delle classi di KConfig e come usarle nelle tue applicazioni''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Using KConfig XT|Usare KConfig XT]]&lt;br /&gt;
:''Tutorial su come usare efficientemente il framework KConfig XT.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Updating KConfig Files|Aggiornare i file di KConfig]]&lt;br /&gt;
:''Tutorial su come scrivere uno script di aggiornamento per mantenere i cambiamenti nel formato dei file delle tue applicazioni in sincronia con i file di configurazione degli utenti gia' esistenti''&lt;br /&gt;
&lt;br /&gt;
== Servizi: Applicazioni e Plugins ==&lt;br /&gt;
;[[Development/Tutorials/Services/Introduction|Introduzione al Services Framework]]&lt;br /&gt;
:''Una visione d'insieme del services framework in KDE e di cosa fornisce agli sviluppatori. Copre la cache alla configurazione di sistema (SyCoCa), i files sorgente dati e come le informazioni indicizzate possono essere usate.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Services/Traders|Trovare i servizi tramite le queries di scambio (Trader Queries)]]&lt;br /&gt;
:''Come trovare servizi, come plugins o mimetypes, che sono indicizzati nella SyCoCa usando la sintassi delle Trader Queries''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Services/Plugins|Creare e Caricare Plugins Usando KService]]&lt;br /&gt;
:''Impara come definire tipi di plugin personalizzati, trovare plugin installati (inclusi plugin di terze parti) e caricarli in una maniera facile e portabile usando KService.''&lt;br /&gt;
&lt;br /&gt;
== Localizzazione ==&lt;br /&gt;
;[[Development/Tutorials/Localization/Unicode|Introduzione A Unicode]]&lt;br /&gt;
:''Una introduzione su cos'e' Unicode e come manipolare i dati Unicode nelle applicazioni KDE.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Localization/i18n|Scrivere applicazioni con la localizzazione in mente]]&lt;br /&gt;
:''Questo tutorial spiega che cos'e' la localizzazione, perche' e' importante e come assicurarsi che le tue applicazioni sono pronte ad essere localizzate. Un MUST per tutti gli sviluppatori.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Localization/i18n Mistakes|Evitare i rischi comuni legati alla localizzazione]]&lt;br /&gt;
:''Ci sono svariati errori comuni che non consentono alle applicazioni di essere correttamente localizzate. Scopri in questo tutorial quali sono e come prevenirli facilmente.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Localization/Building KDE's l10n Module|Compilare Moduli di localizzazione di KDE]]&lt;br /&gt;
:''Potrebbe essere una buona idea effetture la compilazione ed installazione del supporto alle lingue dal modulo di localizzazione (l10n) di KDE lavorandoci sopra. Questo modo ti permettera' di testare le tue applicazioni in altre lingue ed evidenziare le aree problematiche. Impara come farlo in questo tutorial.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Localization/i18n Build Systems|Incorporare i18n nel sistema di build]]&lt;br /&gt;
:''Una volta che la tua applicazione e' pronta per essere localizzata, il prossimo passo e' di assicurarsi che i files di traduzione siano impacchettati automaticamente e mantenuti aggiornati. Questo tutorial tratta della necessarie aggiunte al CMakeFiles.txt oltre che del processo di distribuzione degli elenchi di messaggi risultanti con le tue applicazioni.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Localization/i18n Challenges|Common i18n Challenges and Solutions]]&lt;br /&gt;
:''Questo tutorial mostra le sfide che potrai eventualmente affrontare traducendo gli handbook o altri dati che esistono al di fuori del codice sorgente, fondendo e maneggiando files .po obsoleti, codifiche in altri linguaggi rispetto all'inglese e la creazione di versioni indipendenti di o muovendo applicazioni tra moduli KDE. -- pessima traduzione ---&lt;br /&gt;
This tutorial covers challenges that you may eventually run into such as translating handbooks and other data that exists outside of the source code, merging and handling obsolete .po files, dealing with freezes, coding in languages other than English and creating independent releases of or moving applications between KDE modules.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Localization/i18n_Semantics|Markup Semantico dei Messaggi]]&lt;br /&gt;
:''Per assicurare una presentazione consistente e una rappresentazione maggiormente comprensibile nelle applicazioni, il markup semantico puo' essere applicato per le traduzioni usando il sistema KUIT. Questo tutorial descrive come lavora questo sistema.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Localization/i18n Krazy|Automated i18n Code Checking]]&lt;br /&gt;
:''Il validatore di codice Krazy controlla il codice di KDE e riporta errori comuni sull'i18n.''&lt;br /&gt;
&lt;br /&gt;
== Documentazione ==&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/API_Documentation|Documentazione alle API]]&lt;br /&gt;
:''Questo tutorial spiega come documentare correttamente le tue API.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Man_Pages|Man Pages]]&lt;br /&gt;
:''Scrittura e generazione delle Man Pages di riferimento.''&lt;br /&gt;
&lt;br /&gt;
== Automazione e Scripting delle applicazioni ==&lt;br /&gt;
&lt;br /&gt;
=== D-Bus ===&lt;br /&gt;
; [[Development/Tutorials/D-Bus/Introduction|Introduzione a D-Bus]]&lt;br /&gt;
:''Una introduzione ai concetti chiave di D-Bus dalla prospettiva dello sviluppatore, questo tutorial mostra cos'e' DBus e come puo' essere usato dalle applicazioni.''&lt;br /&gt;
; [[Development/Tutorials/D-Bus/Accessing Interfaces|Accesso alle interfacce di D-Bus]]&lt;br /&gt;
:''Una guida passo-passo per chiamare i metodi di D-Bus e connettere i segnali di D-Bus tramite QtDBus.''&lt;br /&gt;
; [[Development/Tutorials/D-Bus/Intermediate_D-Bus|D-Bus Intermedio]]&lt;br /&gt;
:''Suggerimenti per far uso di QtDBus quando si ha a che fare con interfacce del mondo reale problematiche.''&lt;br /&gt;
; [[Development/Tutorials/D-Bus/Creating Interfaces|Creare Interfacce D-Bus]]&lt;br /&gt;
:''Impara come esporre funzionalita' nella tua applicazione creando ed usando interfacce D-Bus personalizzate. Tratta la generazione di descrizioni XML, instanziazione di interfacce a runtime e impostazione del sistema di build con CMake.''&lt;br /&gt;
; [[Development/Tutorials/D-Bus/Autostart Services|Servizi in autoavvio con D-Bus]]&lt;br /&gt;
:''Trasforma la tua applicazione in un servizio in autoavvio di D-Bus con questo tutorial. Questa funzionalita' di D-Bus, anche conosciuta come &amp;quot;D-Bus service activation&amp;quot;, assicura che quando la tua applicazione non e' avviata le chiamate fatte verso di essa vengono intradate verso il demone di D-Bus che provvede ad avviare l'applicazione se e quando e' necessario.''&lt;br /&gt;
; [[Development/Tutorials/Porting_to_D-Bus|Porting da DCOP a D-Bus]]&lt;br /&gt;
: ''Effettua il Port della tua applicazione da DCOP a D-Bus con questa maneggevole guida.''&lt;br /&gt;
&lt;br /&gt;
=== Konqueror ===&lt;br /&gt;
; [[Development/Tutorials/Creating Konqueror Service Menus|Creare Menu di servizio su Konqueror]]&lt;br /&gt;
:''Questo tutorial ti mostra come creare azioni specifiche per mimetype nel menu contestuale di Konqueror (detto &amp;quot;servicemenu&amp;quot;).''&lt;br /&gt;
&lt;br /&gt;
=== Kross ===&lt;br /&gt;
; [[Development/Tutorials/Kross/Introduction|Introduzione a Kross]]&lt;br /&gt;
:''Una introduzione al Framework per lo Scripting Kross.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Kross/Hello_World|Hello World]]&lt;br /&gt;
:''Una prima applicazione che lavora con codice kross.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Kross/Call_Functions_in_Kross|Chiamare Funzioni in Kross]]&lt;br /&gt;
:''Semplice dimostrazione di chiamate a funzioni di scripting''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Kross/Connecting_Signals_and_slots_in_Kross|Connettere Signals e Slots a Kross]]&lt;br /&gt;
:''Semplice dimostrazione per la connessione di oggetti signals con script slots''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Kross/Scripts-as-Plugins|Scripts come Plugins con Kross]]&lt;br /&gt;
:''Questo tutorial fornisce una introduzione passo passo su come integrare script come plugin in un'applicazione KDE.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Kross/Script-Actions|Posizionare comandi da script nei menu della tua applicazione]]&lt;br /&gt;
:''Semplice dimostrazinoe su come estendere i menu della tua applicazione per eseguire script.''&lt;br /&gt;
&lt;br /&gt;
=== KOffice ===&lt;br /&gt;
; [[Development/Tutorials/KOffice Overview|Panoramica su KOffice]]&lt;br /&gt;
:''Questo documento mostra una panoramica sui differenti tipi di plugin di KOffice ed illustra per ognuno lo scopo ed i punti di forza. Se sei nuovizio ai plugin di KOffice, questo e' il punto di partenza.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Write a Flake Plugin|Creare Plugins per KOffice con Flake]]&lt;br /&gt;
:''Questo tutorial mostra come costruire un plugin per le applicazioni di KOffice per consentirti di inglobare contenuto di documenti ODF usando Flake.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/KWord Scripting|KWord Scripting]]&lt;br /&gt;
:''Questo tutorial mostra come creare script per KWord con Python, Ruby o JavaScript usando Kross.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/KSpread Scripting|KSpread Scripting]]&lt;br /&gt;
:''Questo tutorial mostra come creare script per KSpread con Python, Ruby o JavaScript usando Kross.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Krita Scripting|Krita Scripting]]&lt;br /&gt;
:''Questo tutorial mostra come creare script per Krita con Python, Ruby o JavaScript usando Kross.''&lt;br /&gt;
&lt;br /&gt;
=== SuperKaramba ===&lt;br /&gt;
; [[Development/Tutorials/SuperKaramba|SuperKaramba Tutorial]]&lt;br /&gt;
:''Questo tutorial fornisce una panoramica di SuperKaramba, file di tema e di scripting con Python, Ruby e JavaScript.''&lt;br /&gt;
&lt;br /&gt;
== Plugins e KParts ==&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Writing kontact plugins|Scrivere kontact plugins]]&lt;br /&gt;
:''Kontact plugins sono KParts. Questo tutorial descrive come scriverne uno.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Using KParts|Usare KParts]]&lt;br /&gt;
:''Impara come caricare una KPart in una finestra di applicazione.''&lt;br /&gt;
&lt;br /&gt;
== Ricerca e  Metadati ==&lt;br /&gt;
&lt;br /&gt;
=== Strigi ===&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Writing file analyzers|Scrivere analizzatori di file]]&lt;br /&gt;
:''Gli analizzatori di file estraggono dati dai file per mostrarli nelle finestre di dialogo e nel file manager. I dati raccolti in questo modo sono anche usati per la ricerca di files. KDE4 permette l'uso di analizzatori multipli per ogni tipo di file. Questo tutorial descrive come puoi scrivere nuovi analizzatori.''&lt;br /&gt;
&lt;br /&gt;
=== [http://nepomuk.kde.org Nepomuk] ===&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Metadata/Nepomuk/Quickstart|Introduzione a Nepomuk]]&lt;br /&gt;
:''Come usare le risorse di Nepomuk in modo rapido ed indolore senza troppo chiasso.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Metadata/Nepomuk/RDFIntroduction|RDF e Ontologie in Nepomuk]]&lt;br /&gt;
:''Una introduzione a RDF e all'uso delle ontologie in Nepomuk.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Metadata/Nepomuk/Resources|Gestire risorse con Nepomuk]]&lt;br /&gt;
:''Nepomuk e' la libreria KDE che fornisce un semplice accesso ai metadati del sistema. Impara come rendere la tua applicazione abile alla creazione e lettura di metadati usando Nepomuk.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Metadata/Nepomuk/ResourceGenerator|Usare il Nepomuk Resource Generator]]&lt;br /&gt;
:''Nepomuk include un generatore di risorse con cui creare classi di comodo per la gestione dei metadati.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Metadata/Nepomuk/NepomukServer|Il Server Nepomuk]]&lt;br /&gt;
:''Il Server Nepomuk ospita il repository principale dei dati di Nepomuk e puo' essere acceduto direttamente via l'API Soprano.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Metadata/Nepomuk/NepomukServices|Nepomuk Services]]&lt;br /&gt;
:''Il Server Nepomuk gestisce un set di servizi Nepomuk.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Metadata/Nepomuk/AdvancedQueries|Queries Avanzate]]&lt;br /&gt;
:''Il potenziale reale di Nepomuk puo' essere esposto quando si eseguono pesanti queries sul repository dei dati. Questo tutorial fornisce un'introduzione alla semantica ed alla ricerca full-text in Nepomuk.''&lt;br /&gt;
&lt;br /&gt;
== Conoscenza dell'Hardware (Solid) ==&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Solid_Tutorials|Introduzione a Solid]]&lt;br /&gt;
:''Una introduzione all'uso della ricerca di hardware di Solid e all'interazione del sistema nelle applicazioni KDE.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Solid_Network_Tutorial|Accesso alle Informazioni di Rete]]&lt;br /&gt;
:''Come usare il Sistema Solid per reperire informazioni sulla rete.''&lt;br /&gt;
&lt;br /&gt;
== Multimedia (Phonon) ==&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Phonon/Introduction|Phonon]]&lt;br /&gt;
:''Come iniziare con le API multimediali''&lt;br /&gt;
&lt;br /&gt;
:''Come compilare e usare Phonon ed il suo backend GStreamer su Linux usando Qt 4.3.x''&lt;br /&gt;
::''Questo articolo ti da' un rapida introduzione su come usare checkout, compilare Phonon e il suo backend GStreamer su GNU/Linux con solo Qt 4.3.x. Fino alla fine, l'articolo descrive anche come uno sviluppatore puo' fare uso di Phonon per creare semplici video e audio players. Puoi leggere l'articolo [http://www.vcreatelogic.com/oss/docs/CompilingPhononOnLinux.pdf qui]. Puoi scaricare il file editabile in formato OpenDocumentText da [http://www.prashanthudupa.com/phonon/CompilingPhononOnLinux.odt qui].''&lt;br /&gt;
&lt;br /&gt;
== Plasma ==&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Plasma/GettingStarted|Iniziare con i Plasmoidi]]&lt;br /&gt;
:''Crea il tuo primo widget Plasma, o Plasmoide, in C++ con uno sfondo SVG, una icona ed un testo di esempio.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Plasma/DataEngines|Scrivere un DataEngine]]&lt;br /&gt;
:''I DataEngines forniscono una [http://api.kde.org/4.0-api/kdebase-apidocs/workspace/libs/plasma/html/classPlasma_1_1DataEngine.html  interfaccia standardizzata] a varie sorgenti dati per la visualizzazione e l'uso. Scopri cos'e' un DataEngine e come scriverne uno.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Plasma/UsingDataEngines|Usare DataEngines in un Plasmoide]]&lt;br /&gt;
:''Con un DataEngine, e' possibile recuperare dati per visualizzarli in un modo semplice e standard. Questo tutorial copre l'argomento di come usare i DataEngines per questo scopo nei Plasmoidi.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Plasma/AbstractRunner|Creare Runners]]&lt;br /&gt;
:''I Runners sono plugin che forniscono funzionalita' di ricerca action-based nel dialogo &amp;quot;esegui comando&amp;quot; dello spazio di lavoro di Plasma. Questi plugin possono essere usati da qualsiasi applicazione linkata a libplasma.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Plasma/Theme|Creare un tema per Plasma]]&lt;br /&gt;
:''Guida per la creazione del tuo primo tema per Plasma.''&lt;br /&gt;
&lt;br /&gt;
== Comunicazione (Decibel) ==&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Decibel/GettingStarted|Iniziare con Decibel]]&lt;br /&gt;
:''Questo tutorial mostra come impostare Decibel.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Decibel/Handling_TextChannels|Manipolare TextChannels]]&lt;br /&gt;
:''Questo tutorial introduce le basi sulla gestione di TextChannels in ingresso guidandoti attraverso la costruzione di una semplice applicazione di chat.''&lt;br /&gt;
&lt;br /&gt;
== Kate / Kwrite ==&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Kate/KTextEditor Plugins|Iniziare con i plugins di KTextEditor]]&lt;br /&gt;
:''Creare il tuo primo plugin di KTextEditor''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Kate/KTextEditor_Plugins_Advanced|Sviluppare un plugin con il dialogo di configurazione]]&lt;br /&gt;
:''Aggiungere un dialogo di configurazione all'esempio &amp;quot;Time &amp;amp; Date&amp;quot;''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Kate/KTextEditor_Example|Un semplice Editor]]&lt;br /&gt;
:''Creare una piccola applicazione con KTextEditor''&lt;br /&gt;
&lt;br /&gt;
== Stampa ==&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Printing Hello World|Hello World]]&lt;br /&gt;
:''Introduzione al sistema di stampa di KDE''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Printing Print Dialog|Finestra di dialogo di Stampa]]&lt;br /&gt;
:''Usare la finestra di dialogo di stampa di KDE''&lt;br /&gt;
&lt;br /&gt;
== Get Hot New Stuff ==&lt;br /&gt;
; [[Development/Tutorials/K_Hot_New_Stuff2|New introduction to KGet Hot New Stuff2]]&lt;br /&gt;
:''A short tutorial about how to use KHotNewStuff2 in your application.''&lt;br /&gt;
&lt;br /&gt;
* old links for KNS1 content:&lt;br /&gt;
; [[Development/Tutorials/Introduction to Get Hot New Stuff|Introduction to Get Hot New Stuff]]&lt;br /&gt;
:''An introduction to the developer-friendly network update system that allows KDE applications to fetch new application data at runtime in a user friendly manner.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/KNewStuffSecure|KNewStuff Secure]] ([http://developer.kde.org/documentation/tutorials/knewstuffsecure/index.html Original Link])&lt;br /&gt;
:''Tutorial showing how to share resources in a secured way (KDE 3.4 and later).''  By Andr&amp;amp;#225;s Mantia &amp;amp;lt;amantia@kde.org&amp;amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Goya ==&lt;br /&gt;
; [[Development/Tutorials/Introduction to Goya usage|Introduzione all'uso di Goya]]&lt;br /&gt;
:''Una introduzione all'uso del sottosistema Goya, che ti permette di aggiungere widgets semplicemente  alle tue itemview e connettere i loro signal al tuo codice, come se fossero widgets reali.''&lt;br /&gt;
&lt;br /&gt;
; [[Development/Tutorials/Introduction to Goya usage 2|Introduzione all'uso di Goya(parte 2)]]&lt;br /&gt;
:''La seconda parte del tutorial, con una with a leggera complessita' in piu' rispetto alla prima parte.''&lt;br /&gt;
&lt;br /&gt;
== Sviluppo Rapido di Applicazioni (RAD) ==&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Python introduction to signals and slots|101 Introduzione ai signals e slots]]&lt;br /&gt;
:''Una semplice introduzione all'architettura signal e slot di Qt.''&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
&lt;br /&gt;
;[http://developer.kde.org/language-bindings/ruby/kde3tutorial/index.html KDE Ruby Korundum tutorial]&lt;br /&gt;
:''A ruby version of Antonio Larrosa Jim&amp;amp;eacute;nez's KDE tutorial by Richard Dale. See the [http://developer.kde.org/language-bindings/ruby/index.html Ruby Developers Corner] for Qt tutorials and other info.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Qt4_Ruby_Tutorial|Tutorial su Qt4 Ruby]]&lt;br /&gt;
:''Favoloso tutorial introduttivo sulle Qt di Trolltech, tradotto in Ruby.''&lt;br /&gt;
&lt;br /&gt;
=== Shell ===&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Shell_Scripting_with_KDE_Dialogs|Shell Scripting con i dialoghi di KDE]] ([http://developer.kde.org/documentation/tutorials/kdialog/t1.html Link Originale]) &lt;br /&gt;
:''Tutorial da [mailto:bradh@frogmouth.net Brad Hards] che descrive come usare le finestre di dialogo di KDE negli script di shell usando kdialog. E' presentato come un tutorial basato su esempi.''&lt;br /&gt;
&lt;br /&gt;
== Programmazione Grafica ==&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/Graphics/Performance|Perfomance di QPainter]]&lt;br /&gt;
:''Suggerimenti per evitare errori comuni che producono scarse prestazioni nell'uso di QPainter''&lt;br /&gt;
&lt;br /&gt;
== Altri tutorials ==&lt;br /&gt;
&lt;br /&gt;
=== Usare la Games Library di KDE ===&lt;br /&gt;
;[[Development/Tutorials/Games/KStandardGameAction| KStandardGameAction]]&lt;br /&gt;
:''Usare libkdegames per rendere i propri giochi adatti allo standard per i giochi di kde''&lt;br /&gt;
;[[Development/Tutorials/Games/Highscores| Highscores]]&lt;br /&gt;
:''Implementare una tabella per la classifica nei tuoi giochi''&lt;br /&gt;
;[[Development/Tutorials/Games/Theme Selector| Selettore dei temi]]&lt;br /&gt;
:''Usare la finestra di dialogo per i temi da libkdegames''&lt;br /&gt;
&lt;br /&gt;
=== 2D Plotting (KPlotWidget) ===&lt;br /&gt;
;[[Development/Tutorials/KPlotWidget|Usare il widget data-plotting di kde]]&lt;br /&gt;
:''Questo tutorial introduce KPlotWidget, usato per il plotting di dati 2-D. Include informazioni sull'uso semplice del widget (includendo aggiunta e modifica dei data sets, e personalizzazione degli assi e delle etichette), e personalizzazioni avanzate (inclusa l'estensione del widget attraverso sottoclassi).''&lt;br /&gt;
&lt;br /&gt;
=== Controllo Lessicale e Grammaticale (Sonnet) ===&lt;br /&gt;
;[[Development/Tutorials/Sonnet/SonnetTutorial|Aggiungere controllo lessicale o grammaticale alle applicazioni KDE]]&lt;br /&gt;
:''Questo tutorial introduce Sonnet e come uno puo' usarli per aggiungere la correzione linguistica nelle tue applicazioni KDE. Le funzionalita' ausiliarie di Sonnet saranno descritte in un tutorial separato.''&lt;br /&gt;
&lt;br /&gt;
=== Pixmap cache (KPixmapCache) ===&lt;br /&gt;
;[[Development/Tutorials/KPixmapCache|Usare la pixmap cache di KDE]]&lt;br /&gt;
:''Questo tutorial mostra come usare KPixmapCache per il caching(per es. pixmaps generate da SVG o altri dati).''&lt;br /&gt;
&lt;br /&gt;
=== Usare MarbleWidget (Marble) ===&lt;br /&gt;
;[[Development/Tutorials/MarbleWidget|Usare MarbleWidget]]&lt;br /&gt;
:''Questo breve tutorial descrive come usare il MarbleWidget nei tuoi progetti''&lt;br /&gt;
&lt;br /&gt;
=== Usare SCM locali per lo sviluppo di KDE ===&lt;br /&gt;
;[[Development/Tutorials/Git|Usare Git per sviluppare per KDE]]&lt;br /&gt;
:''Questo tutorial mostra come usare Git per sviluppare per KDE''&lt;br /&gt;
&lt;br /&gt;
== Materiali su KDE2 e KDE3 ==&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/KDE3|Tutorials su KDE3]]&lt;br /&gt;
:''Questi tutorials coprono gli argomenti relativi a KDE3.''&lt;br /&gt;
&lt;br /&gt;
;[[Development/Tutorials/KDE2|Tutorials su KDE2]]&lt;br /&gt;
:''Questi tutorials coprono gli argomenti relativi a KDE2.''&lt;br /&gt;
&lt;br /&gt;
[[Category:KDE4]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/First_program_(it)</id>
		<title>Development/Tutorials/First program (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/First_program_(it)"/>
				<updated>2008-11-29T11:58:40Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: punta al secondo tutorial&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/First_program}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Hello World|&lt;br /&gt;
&lt;br /&gt;
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KXmlGuiWindow_(it)|Tutorial 2 - KXmlGuiWindow]]| &lt;br /&gt;
&lt;br /&gt;
reading=[[Development/Tutorials/CMake|CMake]]&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Il tuo primo programma dovrebbe salutare il mondo con un amichevole &amp;quot;Ciao Mondo&amp;quot;, giusto? Per farlo, useremo un {{class|KMessageBox}} personalizzando uno dei pulsanti.&lt;br /&gt;
&lt;br /&gt;
[[image:Ciaomondo_tutorial1.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
{{tip|Per avere più informazioni su qualunque classe che incontri, Konqueror offre una veloce scorciatoia. Per cercare informazioni circa KMessageBox, basta digitare &amp;quot;kde:kmessagebox&amp;quot; in Konqueror e sarai portato alla documentazione.}}&lt;br /&gt;
&lt;br /&gt;
{{tip|Vorresti poter usare KDevelop per i tuoi progetti, il quale fa molte cose carine come il completamento del codice, facile accesso alla documentazione delle API, supporto per il debugging.&lt;br /&gt;
Leggi [[Getting_Started/Set_up_KDE_4_for_development#KDevelop|questo tutorial]] per configurare KDevelop correttamente. Puoi controllare se il setup è andato a buon fine aprendo un'applicazione KDE4 esistente con KDevelop.&lt;br /&gt;
Tuttavia hai ancora bisogno di editare a mano i files di CMake.}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Il Codice ==&lt;br /&gt;
&lt;br /&gt;
Tutto il codice di cui abbiamo bisogno starà in un file, &amp;lt;tt&amp;gt;main.cpp&amp;lt;/tt&amp;gt;. Crealo con il codice qua sotto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;KMessageBox&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&lt;br /&gt;
                         // Il nome del programma, usato internamente.&lt;br /&gt;
                         &amp;quot;tutorial1&amp;quot;,&lt;br /&gt;
                         // Il nome nel catalogo messaggi.&lt;br /&gt;
                         // Se nullo, verrà usato il nome del programma.&lt;br /&gt;
                         0,&lt;br /&gt;
                         // Una stringa con il nome del programma.&lt;br /&gt;
                         // Quella che verrà effettivamente visualizzata.&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 1&amp;quot;),&lt;br /&gt;
                         // La stringa con la versione del programma.&lt;br /&gt;
                         &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         // Piccola descrizione su cosa fa il programma.&lt;br /&gt;
                         ki18n(&amp;quot;Visualizza una finestra KMessageBox.&amp;quot;),&lt;br /&gt;
                         // La licenza con la quale il codice è rilasciato.&lt;br /&gt;
                         KAboutData::License_GPL,&lt;br /&gt;
                         // Copyright&lt;br /&gt;
                         ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         // Testo opzionale mostrato in &amp;quot;Informazioni su&amp;quot;.&lt;br /&gt;
                         // Può contenere qualunque informazione desiderata.&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         // La stringa con la homepage del programma.&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         // L'indirizzo email per la segnalazione di bug.&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
    KGuiItem yesButton( i18n( &amp;quot;Ciao&amp;quot; ), QString(),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un tooltip&amp;quot; ),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un testo di aiuto CheCos'è?.&amp;quot; ) );&lt;br /&gt;
    KMessageBox::questionYesNo( 0, i18n( &amp;quot;Ciao Mondo&amp;quot; ),&lt;br /&gt;
                                i18n( &amp;quot;Ciao&amp;quot; ), yesButton );&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Il primo codice specifico di KDE che incontriamo in questo programma è {{class|KAboutData}}. Questa è la classe utilizzata per immagazzinare le informazioni sul programma, come ad esempio una piccola descrizione, gli autori, o le informazioni sulla licenza. Praticamente ogni applicazione KDE dovrebbe usare questa classe.&lt;br /&gt;
&lt;br /&gt;
Poi arriviamo a {{class|KCmdLineArgs}}. Questa è la classe che si usa quando si vogliono specificare istruzioni da riga di comando per, ad esempio, aprire il programma con uno specifico file. Comunque, in questo tutorial, semplicemente lo inizializziamo con l'oggetto {{class|KAboutData}} che abbiamo appena creato, in modo da poter usare le opzioni &amp;lt;tt&amp;gt;--version&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;--author&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quindi adiamo a creare l'oggetto {{class|KApplication}}. Ciò bisogna farlo esattamente una volta in ogni programma, visto che è necessario per cose come l'[[Development/Tutorials/Localization/i18n|i18n]].&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo finito con le operazioni indispensabili di KDE, possiamo cominciare a fare cose interessanti con la nostra applicazione. Creeremo una finestra pop-up, ma personalizzeremo uno dei pulsanti. Per la personalizzazione avremo bisogno di un oggetto {{class|KGuiItem}}. Il primo argomento del costruttore di {{class|KGuiItem}} è il testo che apparirà sull'oggetto (nel nostro caso, un pulsante). Quindi ci sarà un opzione per inserire un'icona, ma non ne vogliamo una così gli mettiamo &amp;lt;tt&amp;gt;QString&amp;lt;/tt&amp;gt;. Impostiamo quindi il tooltip (quello che appare quando fermi il puntatore su un elemento) ed infine il testo &amp;quot;Che Cos'è?&amp;quot; (accessibile tramite click col tasto destro, con Shift-F1 oppure cliccando su &amp;quot;?&amp;quot; nella barra del titolo e poi sul pulsante &amp;quot;Ciao&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo il nostro pulsante, possiamo creare il nostro pop-up. Chiamiamo la funzione &amp;lt;tt&amp;gt;{{class|KMessageBox}}::questionYesNo()&amp;lt;/tt&amp;gt; la quale, in modo predefinito, crea una finestra con due pulsanti &amp;quot;Si&amp;quot; e &amp;quot;No&amp;quot;. Il secondo argomento compone il testo mostrato nella finestra sopra i due pulsanti. Il terzo è l'intestazione della finestra; e infine impostiamo il KGuiItem di (quello che normalmente è) il pulsante &amp;quot;Si&amp;quot; con il nostro &amp;lt;tt&amp;gt;KGuiItem yesButton&amp;lt;/tt&amp;gt; appena creato.&lt;br /&gt;
&lt;br /&gt;
Notare come tutto il testo visibile all'utente passa attraverso la funzione i18n(); questo è necessario per permettere la traduzione della UI. Più informazioni sulla localizzazione possono essere trovate nel [[Development/Tutorials/Localization/i18n|tutorial sulla localizzazione]].&lt;br /&gt;
&lt;br /&gt;
Abbiamo finito per quanto riguarda il codice. Ora la compilazione e la prova.&lt;br /&gt;
&lt;br /&gt;
== Compilazione ==&lt;br /&gt;
&lt;br /&gt;
Vorrai usare [[Development/Tutorials/CMake|CMake]] come ambiente di compilazione. Devi fornire un file chiamato CMakeLists.txt, cmake usa questo file per generare tutti i Makefiles.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
Crea un file di nome CMakeLists.txt nella stessa cartella di main.cpp con questo contenuto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial1)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial1_SRCS&lt;br /&gt;
  main.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial1 ${tutorial1_SRCS})&lt;br /&gt;
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial1  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
La funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; trova tutti i pacchetti che hai richiesto (in questo caso KDE4) ed imposta alcune variabili che descrivono la posizione degli header e delle librerie del pacchetto. In questo caso useremo la variabile &amp;lt;tt&amp;gt;KDE4_INCLUDES&amp;lt;/tt&amp;gt; contenente il percorso ai files header di KDE4.&lt;br /&gt;
&lt;br /&gt;
Per permettere al compilatore di trovare questi files, passiamo quella variabile alla funzione &amp;lt;tt&amp;gt;include_directories()&amp;lt;/tt&amp;gt; la quale aggiunge gli header di KDE4 al percorso di ricerca degli header.&lt;br /&gt;
&lt;br /&gt;
Poi creiamo una variabile chiamata &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; con la funzione &amp;lt;tt&amp;gt;set()&amp;lt;/tt&amp;gt;. In questo caso la impostiamo con il nome del nostro unico file sorgente.&lt;br /&gt;
&lt;br /&gt;
Quindi usiamo &amp;lt;tt&amp;gt;kde4_add_executable()&amp;lt;/tt&amp;gt; per creare un eseguibile chiamato &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; dai sorgenti elencati nella nostra variabile &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt;. In seguito, facciamo il link del nostro eseguibile alle librerie kdeui di KDE4 con &amp;lt;tt&amp;gt;target_link_libraries()&amp;lt;/tt&amp;gt; e la variabile &amp;lt;tt&amp;gt;KDE4_KDEUI_LIBS&amp;lt;/tt&amp;gt; precedentemente riempita dalla funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt;. La riga che comincia con &amp;lt;tt&amp;gt;install&amp;lt;/tt&amp;gt; scrive un target di default &amp;quot;install&amp;quot; dentro al Makefile.&lt;br /&gt;
&lt;br /&gt;
=== Make ed Esecuzione ===&lt;br /&gt;
&lt;br /&gt;
Puoi invocare CMake e make manualmente:&lt;br /&gt;
&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake .. # Nota questi due punti - non è una mancanza,&lt;br /&gt;
          # stanno per &amp;quot;cartella superiore&amp;quot;.&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
Oppure, se hai preparato il tuo ambiente come descritto in [[Getting_Started/Build/KDE4|Getting Started/Build/KDE4]], puoi compilare questo codice con:&lt;br /&gt;
 cmakekde&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Quindi lancialo con:&lt;br /&gt;
 ./tutorial1&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KXmlGuiWindow|usare KXmlGuiWindow]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-29T11:57:08Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In order to learn how to develop cool KDE application I've start from the beginning: I aim to translate all the tutorial that I'm facing with.&lt;br /&gt;
&lt;br /&gt;
== Done ==&lt;br /&gt;
&lt;br /&gt;
* [[Template:TutorialBrowser_(it)]]&lt;br /&gt;
* [[Development/Tutorials/First_program_(it)]]&lt;br /&gt;
* [[Development/Tutorials/Using_KXmlGuiWindow_(it)]]&lt;br /&gt;
&lt;br /&gt;
== Work in Progress ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== to recheck ===&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it) | Development/Tutorials/Common_Programming_Mistakes_(it)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Who's Next ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/Using_KActions]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Using_KXmlGuiWindow_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Using KXmlGuiWindow (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Using_KXmlGuiWindow_(it)"/>
				<updated>2008-11-29T11:56:20Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: User:Fresbeeplayer/Development/Tutorials/Using KXmlGuiWindow (it) moved to Development/Tutorials/Using KXmlGuiWindow (it): Finita la traduzione&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Development/Tutorials/Using KXmlGuiWindow (it)]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)</id>
		<title>Development/Tutorials/Using KXmlGuiWindow (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)"/>
				<updated>2008-11-29T11:56:20Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: User:Fresbeeplayer/Development/Tutorials/Using KXmlGuiWindow (it) moved to Development/Tutorials/Using KXmlGuiWindow (it): Finita la traduzione&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KXmlGuiWindow}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Come usare KXmlGuiWindow|&lt;br /&gt;
&lt;br /&gt;
pre=[[Development/Tutorials/First_program_(it)|Tutorial 1 - Ciao Mondo]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions and XMLGUI]]| &lt;br /&gt;
&lt;br /&gt;
reading={{class|KXmlGuiWindow}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial parte dal precedente [[Development/Tutorials/First_program_(it)|Ciao Mondo]] ed introdurrà la classe {{class|KXmlGuiWindow}}.&lt;br /&gt;
&lt;br /&gt;
Nel precedente tutorial il programma faceva apparire una finestra di dialogo, ma ora faremo dei passi verso un programma funzionale.&lt;br /&gt;
&lt;br /&gt;
[[Image:Kxmlguiwindow_tutorial2.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
== KXmlGuiWindow ==&lt;br /&gt;
&lt;br /&gt;
{{class|KXmlGuiWindow}} offre una finestra completa con la barra dei menu, una degli strumenti, la barra di stato ed un'area principale al centro per un grande widget. Molte applicazioni KDE deriveranno da questa classe perché fornisce un modo semplice per la disposizione di questi elementi tramite un file XML (questa tecnologia è chiamata XMLGUI). Anche se non useremo XMLGUI in ''questo'' tutorial, la useremo nel prossimo.&lt;br /&gt;
&lt;br /&gt;
Per poter avere un KXmlGuiWindow utile, dobbiamo ereditarlo. Creeremo due files, un &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; ed un &amp;lt;tt&amp;gt;mainwindow.h&amp;lt;/tt&amp;gt; i quali conterranno il nostro codice.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#ifndef MAINWINDOW_H&lt;br /&gt;
#define MAINWINDOW_H&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;KXmlGuiWindow&amp;gt;&lt;br /&gt;
#include &amp;lt;KTextEdit&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class MainWindow : public KXmlGuiWindow&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    MainWindow(QWidget *genitore = 0);&lt;br /&gt;
		&lt;br /&gt;
  private:&lt;br /&gt;
    KTextEdit* areaDiTesto;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto creiamo una sotto-classe di KXmlGuiWindow alla linea 7 con &amp;lt;tt&amp;gt;class MainWindow : public KXmlGuiWindow&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Poi dichiariamo il costruttore con &amp;lt;tt&amp;gt;MainWindow(QWidget *genitore=0);&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
E in fine definiamo un puntatore all'oggetto che sarà il corpo del nostro programma. {{class|KTextEdit}} è un generico editor di testo ricco con qualche finezza di KDE come il nascondere automaticamente il cursore.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MainWindow::MainWindow(QWidget *genitore) : KXmlGuiWindow(genitore)&lt;br /&gt;
{&lt;br /&gt;
  areaDiTesto = new KTextEdit();&lt;br /&gt;
  setCentralWidget(areaDiTesto);&lt;br /&gt;
  setupGUI();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alla riga 1, ovviamente, dobbiamo includere gli headers contenenti la definizione delle classi.&lt;br /&gt;
&lt;br /&gt;
Poi sulla riga 5, inizializziamo il nostro editor di testo con un oggetto. Quindi alla sesta riga usiamo la funzione setCentralWidget() interna a KXmlGuiWindow, la quale gli dice cosa dovrebbe apparire nella sezione centrale della finestra.&lt;br /&gt;
&lt;br /&gt;
Infine viene chiamato KXmlGuiWindow::setupGUI(), il quale fa un sacco di cose dietro le quinte e crea la barra dei menu predefinita (Settings, Help).&lt;br /&gt;
&lt;br /&gt;
== Ritorno a main.cpp ==&lt;br /&gt;
&lt;br /&gt;
Affinché di fatto la finestra funzioni, abbiamo bisogno di aggiungere poche linee in main.cpp:&lt;br /&gt;
&lt;br /&gt;
=== main.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&amp;quot;tutorial2&amp;quot;, 0,&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 2&amp;quot;), &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         ki18n(&amp;quot;Una semplice area di testo.&amp;quot;),&lt;br /&gt;
                         KAboutData::License_GPL, ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
&lt;br /&gt;
    MainWindow* window = new MainWindow();&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le sole righe nuove qui (comparate con il primo tutorial) sono 6, 21 e 22. Alla riga 21, creiamo il nostro oggetto MainWindow e alla 22 lo visualizziamo.&lt;br /&gt;
&lt;br /&gt;
== CMake ==&lt;br /&gt;
&lt;br /&gt;
Il miglior modo di compilare il programma è usando CMake. Ciò che cambia dal primo tutorial consiste nell'aggiunta di &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; nella lista dei sorgenti e tutti i &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; diventano &amp;lt;tt&amp;gt;tutorial2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial2)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial2_SRCS &lt;br /&gt;
  main.cpp&lt;br /&gt;
  mainwindow.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial2 ${tutorial2_SRCS})&lt;br /&gt;
target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial2  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compilazione ===&lt;br /&gt;
&lt;br /&gt;
Per la compilazione, il link e l'esecuzione, usa:&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake ..&lt;br /&gt;
 make&lt;br /&gt;
 ./tutorial2&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KActions|usare KActions]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)</id>
		<title>Development/Tutorials/Using KXmlGuiWindow (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)"/>
				<updated>2008-11-29T11:54:43Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: cursor auto-hiding&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KXmlGuiWindow}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Come usare KXmlGuiWindow|&lt;br /&gt;
&lt;br /&gt;
pre=[[Development/Tutorials/First_program_(it)|Tutorial 1 - Ciao Mondo]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions and XMLGUI]]| &lt;br /&gt;
&lt;br /&gt;
reading={{class|KXmlGuiWindow}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial parte dal precedente [[Development/Tutorials/First_program_(it)|Ciao Mondo]] ed introdurrà la classe {{class|KXmlGuiWindow}}.&lt;br /&gt;
&lt;br /&gt;
Nel precedente tutorial il programma faceva apparire una finestra di dialogo, ma ora faremo dei passi verso un programma funzionale.&lt;br /&gt;
&lt;br /&gt;
[[Image:Kxmlguiwindow_tutorial2.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
== KXmlGuiWindow ==&lt;br /&gt;
&lt;br /&gt;
{{class|KXmlGuiWindow}} offre una finestra completa con la barra dei menu, una degli strumenti, la barra di stato ed un'area principale al centro per un grande widget. Molte applicazioni KDE deriveranno da questa classe perché fornisce un modo semplice per la disposizione di questi elementi tramite un file XML (questa tecnologia è chiamata XMLGUI). Anche se non useremo XMLGUI in ''questo'' tutorial, la useremo nel prossimo.&lt;br /&gt;
&lt;br /&gt;
Per poter avere un KXmlGuiWindow utile, dobbiamo ereditarlo. Creeremo due files, un &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; ed un &amp;lt;tt&amp;gt;mainwindow.h&amp;lt;/tt&amp;gt; i quali conterranno il nostro codice.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#ifndef MAINWINDOW_H&lt;br /&gt;
#define MAINWINDOW_H&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;KXmlGuiWindow&amp;gt;&lt;br /&gt;
#include &amp;lt;KTextEdit&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class MainWindow : public KXmlGuiWindow&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    MainWindow(QWidget *genitore = 0);&lt;br /&gt;
		&lt;br /&gt;
  private:&lt;br /&gt;
    KTextEdit* areaDiTesto;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto creiamo una sotto-classe di KXmlGuiWindow alla linea 7 con &amp;lt;tt&amp;gt;class MainWindow : public KXmlGuiWindow&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Poi dichiariamo il costruttore con &amp;lt;tt&amp;gt;MainWindow(QWidget *genitore=0);&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
E in fine definiamo un puntatore all'oggetto che sarà il corpo del nostro programma. {{class|KTextEdit}} è un generico editor di testo ricco con qualche finezza di KDE come il nascondere automaticamente il cursore.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MainWindow::MainWindow(QWidget *genitore) : KXmlGuiWindow(genitore)&lt;br /&gt;
{&lt;br /&gt;
  areaDiTesto = new KTextEdit();&lt;br /&gt;
  setCentralWidget(areaDiTesto);&lt;br /&gt;
  setupGUI();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alla riga 1, ovviamente, dobbiamo includere gli headers contenenti la definizione delle classi.&lt;br /&gt;
&lt;br /&gt;
Poi sulla riga 5, inizializziamo il nostro editor di testo con un oggetto. Quindi alla sesta riga usiamo la funzione setCentralWidget() interna a KXmlGuiWindow, la quale gli dice cosa dovrebbe apparire nella sezione centrale della finestra.&lt;br /&gt;
&lt;br /&gt;
Infine viene chiamato KXmlGuiWindow::setupGUI(), il quale fa un sacco di cose dietro le quinte e crea la barra dei menu predefinita (Settings, Help).&lt;br /&gt;
&lt;br /&gt;
== Ritorno a main.cpp ==&lt;br /&gt;
&lt;br /&gt;
Affinché di fatto la finestra funzioni, abbiamo bisogno di aggiungere poche linee in main.cpp:&lt;br /&gt;
&lt;br /&gt;
=== main.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&amp;quot;tutorial2&amp;quot;, 0,&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 2&amp;quot;), &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         ki18n(&amp;quot;Una semplice area di testo.&amp;quot;),&lt;br /&gt;
                         KAboutData::License_GPL, ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
&lt;br /&gt;
    MainWindow* window = new MainWindow();&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le sole righe nuove qui (comparate con il primo tutorial) sono 6, 21 e 22. Alla riga 21, creiamo il nostro oggetto MainWindow e alla 22 lo visualizziamo.&lt;br /&gt;
&lt;br /&gt;
== CMake ==&lt;br /&gt;
&lt;br /&gt;
Il miglior modo di compilare il programma è usando CMake. Ciò che cambia dal primo tutorial consiste nell'aggiunta di &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; nella lista dei sorgenti e tutti i &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; diventano &amp;lt;tt&amp;gt;tutorial2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial2)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial2_SRCS &lt;br /&gt;
  main.cpp&lt;br /&gt;
  mainwindow.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial2 ${tutorial2_SRCS})&lt;br /&gt;
target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial2  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compilazione ===&lt;br /&gt;
&lt;br /&gt;
Per la compilazione, il link e l'esecuzione, usa:&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake ..&lt;br /&gt;
 make&lt;br /&gt;
 ./tutorial2&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KActions|usare KActions]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)</id>
		<title>Development/Tutorials/Using KXmlGuiWindow (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)"/>
				<updated>2008-11-29T11:44:44Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: svista in mainwindow.cpp&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KXmlGuiWindow}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Come usare KXmlGuiWindow|&lt;br /&gt;
&lt;br /&gt;
pre=[[Development/Tutorials/First_program_(it)|Tutorial 1 - Ciao Mondo]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions and XMLGUI]]| &lt;br /&gt;
&lt;br /&gt;
reading={{class|KXmlGuiWindow}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial parte dal precedente [[Development/Tutorials/First_program_(it)|Ciao Mondo]] ed introdurrà la classe {{class|KXmlGuiWindow}}.&lt;br /&gt;
&lt;br /&gt;
Nel precedente tutorial il programma faceva apparire una finestra di dialogo, ma ora faremo dei passi verso un programma funzionale.&lt;br /&gt;
&lt;br /&gt;
[[Image:Kxmlguiwindow_tutorial2.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
== KXmlGuiWindow ==&lt;br /&gt;
&lt;br /&gt;
{{class|KXmlGuiWindow}} offre una finestra completa con la barra dei menu, una degli strumenti, la barra di stato ed un'area principale al centro per un grande widget. Molte applicazioni KDE deriveranno da questa classe perché fornisce un modo semplice per la disposizione di questi elementi tramite un file XML (questa tecnologia è chiamata XMLGUI). Anche se non useremo XMLGUI in ''questo'' tutorial, la useremo nel prossimo.&lt;br /&gt;
&lt;br /&gt;
Per poter avere un KXmlGuiWindow utile, dobbiamo ereditarlo. Creeremo due files, un &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; ed un &amp;lt;tt&amp;gt;mainwindow.h&amp;lt;/tt&amp;gt; i quali conterranno il nostro codice.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#ifndef MAINWINDOW_H&lt;br /&gt;
#define MAINWINDOW_H&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;KXmlGuiWindow&amp;gt;&lt;br /&gt;
#include &amp;lt;KTextEdit&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class MainWindow : public KXmlGuiWindow&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    MainWindow(QWidget *genitore = 0);&lt;br /&gt;
		&lt;br /&gt;
  private:&lt;br /&gt;
    KTextEdit* areaDiTesto;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto creiamo una sotto-classe di KXmlGuiWindow alla linea 7 con &amp;lt;tt&amp;gt;class MainWindow : public KXmlGuiWindow&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Poi dichiariamo il costruttore con &amp;lt;tt&amp;gt;MainWindow(QWidget *genitore=0);&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;come cacchio si traduce '''cursor auto-hiding''' ???&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
E in fine definiamo un puntatore all'oggetto che sarà il corpo del nostro programma. {{class|KTextEdit}} è un generico editor di testo ricco con qualche finezza di KDE come il &amp;quot;nascondimento&amp;quot; automatico del cursore.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MainWindow::MainWindow(QWidget *genitore) : KXmlGuiWindow(genitore)&lt;br /&gt;
{&lt;br /&gt;
  areaDiTesto = new KTextEdit();&lt;br /&gt;
  setCentralWidget(areaDiTesto);&lt;br /&gt;
  setupGUI();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alla riga 1, ovviamente, dobbiamo includere gli headers contenenti la definizione delle classi.&lt;br /&gt;
&lt;br /&gt;
Poi sulla riga 5, inizializziamo il nostro editor di testo con un oggetto. Quindi alla sesta riga usiamo la funzione setCentralWidget() interna a KXmlGuiWindow, la quale gli dice cosa dovrebbe apparire nella sezione centrale della finestra.&lt;br /&gt;
&lt;br /&gt;
Infine viene chiamato KXmlGuiWindow::setupGUI(), il quale fa un sacco di cose dietro le quinte e crea la barra dei menu predefinita (Settings, Help).&lt;br /&gt;
&lt;br /&gt;
== Ritorno a main.cpp ==&lt;br /&gt;
&lt;br /&gt;
Affinché di fatto la finestra funzioni, abbiamo bisogno di aggiungere poche linee in main.cpp:&lt;br /&gt;
&lt;br /&gt;
=== main.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&amp;quot;tutorial2&amp;quot;, 0,&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 2&amp;quot;), &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         ki18n(&amp;quot;Una semplice area di testo.&amp;quot;),&lt;br /&gt;
                         KAboutData::License_GPL, ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
&lt;br /&gt;
    MainWindow* window = new MainWindow();&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le sole righe nuove qui (comparate con il primo tutorial) sono 6, 21 e 22. Alla riga 21, creiamo il nostro oggetto MainWindow e alla 22 lo visualizziamo.&lt;br /&gt;
&lt;br /&gt;
== CMake ==&lt;br /&gt;
&lt;br /&gt;
Il miglior modo di compilare il programma è usando CMake. Ciò che cambia dal primo tutorial consiste nell'aggiunta di &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; nella lista dei sorgenti e tutti i &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; diventano &amp;lt;tt&amp;gt;tutorial2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial2)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial2_SRCS &lt;br /&gt;
  main.cpp&lt;br /&gt;
  mainwindow.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial2 ${tutorial2_SRCS})&lt;br /&gt;
target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial2  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compilazione ===&lt;br /&gt;
&lt;br /&gt;
Per la compilazione, il link e l'esecuzione, usa:&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake ..&lt;br /&gt;
 make&lt;br /&gt;
 ./tutorial2&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KActions|usare KActions]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)</id>
		<title>Development/Tutorials/Using KXmlGuiWindow (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)"/>
				<updated>2008-11-29T11:37:12Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KXmlGuiWindow}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Come usare KXmlGuiWindow|&lt;br /&gt;
&lt;br /&gt;
pre=[[Development/Tutorials/First_program_(it)|Tutorial 1 - Ciao Mondo]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions and XMLGUI]]| &lt;br /&gt;
&lt;br /&gt;
reading={{class|KXmlGuiWindow}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial parte dal precedente [[Development/Tutorials/First_program_(it)|Ciao Mondo]] ed introdurrà la classe {{class|KXmlGuiWindow}}.&lt;br /&gt;
&lt;br /&gt;
Nel precedente tutorial il programma faceva apparire una finestra di dialogo, ma ora faremo dei passi verso un programma funzionale.&lt;br /&gt;
&lt;br /&gt;
[[Image:Kxmlguiwindow_tutorial2.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
== KXmlGuiWindow ==&lt;br /&gt;
&lt;br /&gt;
{{class|KXmlGuiWindow}} offre una finestra completa con la barra dei menu, una degli strumenti, la barra di stato ed un'area principale al centro per un grande widget. Molte applicazioni KDE deriveranno da questa classe perché fornisce un modo semplice per la disposizione di questi elementi tramite un file XML (questa tecnologia è chiamata XMLGUI). Anche se non useremo XMLGUI in ''questo'' tutorial, la useremo nel prossimo.&lt;br /&gt;
&lt;br /&gt;
Per poter avere un KXmlGuiWindow utile, dobbiamo ereditarlo. Creeremo due files, un &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; ed un &amp;lt;tt&amp;gt;mainwindow.h&amp;lt;/tt&amp;gt; i quali conterranno il nostro codice.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#ifndef MAINWINDOW_H&lt;br /&gt;
#define MAINWINDOW_H&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;KXmlGuiWindow&amp;gt;&lt;br /&gt;
#include &amp;lt;KTextEdit&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class MainWindow : public KXmlGuiWindow&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    MainWindow(QWidget *genitore=0);&lt;br /&gt;
		&lt;br /&gt;
  private:&lt;br /&gt;
    KTextEdit* areaDiTesto;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto creiamo una sotto-classe di KXmlGuiWindow alla linea 7 con &amp;lt;tt&amp;gt;class MainWindow : public KXmlGuiWindow&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Poi dichiariamo il costruttore con &amp;lt;tt&amp;gt;MainWindow(QWidget *genitore=0);&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;come cacchio si traduce '''cursor auto-hiding''' ???&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
E in fine definiamo un puntatore all'oggetto che sarà il corpo del nostro programma. {{class|KTextEdit}} è un generico editor di testo ricco con qualche finezza di KDE come il &amp;quot;nascondimento&amp;quot; automatico del cursore.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent)&lt;br /&gt;
{&lt;br /&gt;
  areaDiTesto = new KTextEdit();&lt;br /&gt;
  setCentralWidget(areaDiTesto);&lt;br /&gt;
  setupGUI();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alla riga 1, ovviamente, dobbiamo includere gli headers contenenti la definizione delle classi.&lt;br /&gt;
&lt;br /&gt;
Poi sulla riga 5, inizializziamo il nostro editor di testo con un oggetto. Quindi alla sesta riga usiamo la funzione setCentralWidget() interna a KXmlGuiWindow, la quale gli dice cosa dovrebbe apparire nella sezione centrale della finestra.&lt;br /&gt;
&lt;br /&gt;
Infine viene chiamato KXmlGuiWindow::setupGUI(), il quale fa un sacco di cose dietro le quinte e crea la barra dei menu predefinita (Settings, Help).&lt;br /&gt;
&lt;br /&gt;
== Ritorno a main.cpp ==&lt;br /&gt;
&lt;br /&gt;
Affinché di fatto la finestra funzioni, abbiamo bisogno di aggiungere poche linee in main.cpp:&lt;br /&gt;
&lt;br /&gt;
=== main.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&amp;quot;tutorial2&amp;quot;, 0,&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 2&amp;quot;), &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         ki18n(&amp;quot;Una semplice area di testo.&amp;quot;),&lt;br /&gt;
                         KAboutData::License_GPL, ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
&lt;br /&gt;
    MainWindow* window = new MainWindow();&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le sole righe nuove qui (comparate con il primo tutorial) sono 6, 21 e 22. Alla riga 21, creiamo il nostro oggetto MainWindow e alla 22 lo visualizziamo.&lt;br /&gt;
&lt;br /&gt;
== CMake ==&lt;br /&gt;
&lt;br /&gt;
Il miglior modo di compilare il programma è usando CMake. Ciò che cambia dal primo tutorial consiste nell'aggiunta di &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; nella lista dei sorgenti e tutti i &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; diventano &amp;lt;tt&amp;gt;tutorial2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial2)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial2_SRCS &lt;br /&gt;
  main.cpp&lt;br /&gt;
  mainwindow.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial2 ${tutorial2_SRCS})&lt;br /&gt;
target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial2  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compilazione ===&lt;br /&gt;
&lt;br /&gt;
Per la compilazione, il link e l'esecuzione, usa:&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake ..&lt;br /&gt;
 make&lt;br /&gt;
 ./tutorial2&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KActions|usare KActions]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-29T11:35:57Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In order to learn how to develop cool KDE application I've start from the beginning: I aim to translate all the tutorial that I'm facing with.&lt;br /&gt;
&lt;br /&gt;
== Done ==&lt;br /&gt;
&lt;br /&gt;
* [[Template:TutorialBrowser_(it)]]&lt;br /&gt;
* [[Development/Tutorials/First_program_(it)]]&lt;br /&gt;
&lt;br /&gt;
== Work in Progress ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== to recheck ===&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Using_KXmlGuiWindow_(it)]]&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it) | Development/Tutorials/Common_Programming_Mistakes_(it)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Who's Next ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/Using_KActions]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/First_program_(it)</id>
		<title>Development/Tutorials/First program (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/First_program_(it)"/>
				<updated>2008-11-29T11:33:40Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Sistemazione del codice e della descrizione di CMakeLists.txt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/First_program}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Hello World|&lt;br /&gt;
&lt;br /&gt;
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KXmlGuiWindow|Tutorial 2 - KXmlGuiWindow]]| &lt;br /&gt;
&lt;br /&gt;
reading=[[Development/Tutorials/CMake|CMake]]&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Il tuo primo programma dovrebbe salutare il mondo con un amichevole &amp;quot;Ciao Mondo&amp;quot;, giusto? Per farlo, useremo un {{class|KMessageBox}} personalizzando uno dei pulsanti.&lt;br /&gt;
&lt;br /&gt;
[[image:Ciaomondo_tutorial1.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
{{tip|Per avere più informazioni su qualunque classe che incontri, Konqueror offre una veloce scorciatoia. Per cercare informazioni circa KMessageBox, basta digitare &amp;quot;kde:kmessagebox&amp;quot; in Konqueror e sarai portato alla documentazione.}}&lt;br /&gt;
&lt;br /&gt;
{{tip|Vorresti poter usare KDevelop per i tuoi progetti, il quale fa molte cose carine come il completamento del codice, facile accesso alla documentazione delle API, supporto per il debugging.&lt;br /&gt;
Leggi [[Getting_Started/Set_up_KDE_4_for_development#KDevelop|questo tutorial]] per configurare KDevelop correttamente. Puoi controllare se il setup è andato a buon fine aprendo un'applicazione KDE4 esistente con KDevelop.&lt;br /&gt;
Tuttavia hai ancora bisogno di editare a mano i files di CMake.}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Il Codice ==&lt;br /&gt;
&lt;br /&gt;
Tutto il codice di cui abbiamo bisogno starà in un file, &amp;lt;tt&amp;gt;main.cpp&amp;lt;/tt&amp;gt;. Crealo con il codice qua sotto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;KMessageBox&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&lt;br /&gt;
                         // Il nome del programma, usato internamente.&lt;br /&gt;
                         &amp;quot;tutorial1&amp;quot;,&lt;br /&gt;
                         // Il nome nel catalogo messaggi.&lt;br /&gt;
                         // Se nullo, verrà usato il nome del programma.&lt;br /&gt;
                         0,&lt;br /&gt;
                         // Una stringa con il nome del programma.&lt;br /&gt;
                         // Quella che verrà effettivamente visualizzata.&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 1&amp;quot;),&lt;br /&gt;
                         // La stringa con la versione del programma.&lt;br /&gt;
                         &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         // Piccola descrizione su cosa fa il programma.&lt;br /&gt;
                         ki18n(&amp;quot;Visualizza una finestra KMessageBox.&amp;quot;),&lt;br /&gt;
                         // La licenza con la quale il codice è rilasciato.&lt;br /&gt;
                         KAboutData::License_GPL,&lt;br /&gt;
                         // Copyright&lt;br /&gt;
                         ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         // Testo opzionale mostrato in &amp;quot;Informazioni su&amp;quot;.&lt;br /&gt;
                         // Può contenere qualunque informazione desiderata.&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         // La stringa con la homepage del programma.&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         // L'indirizzo email per la segnalazione di bug.&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
    KGuiItem yesButton( i18n( &amp;quot;Ciao&amp;quot; ), QString(),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un tooltip&amp;quot; ),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un testo di aiuto CheCos'è?.&amp;quot; ) );&lt;br /&gt;
    KMessageBox::questionYesNo( 0, i18n( &amp;quot;Ciao Mondo&amp;quot; ),&lt;br /&gt;
                                i18n( &amp;quot;Ciao&amp;quot; ), yesButton );&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Il primo codice specifico di KDE che incontriamo in questo programma è {{class|KAboutData}}. Questa è la classe utilizzata per immagazzinare le informazioni sul programma, come ad esempio una piccola descrizione, gli autori, o le informazioni sulla licenza. Praticamente ogni applicazione KDE dovrebbe usare questa classe.&lt;br /&gt;
&lt;br /&gt;
Poi arriviamo a {{class|KCmdLineArgs}}. Questa è la classe che si usa quando si vogliono specificare istruzioni da riga di comando per, ad esempio, aprire il programma con uno specifico file. Comunque, in questo tutorial, semplicemente lo inizializziamo con l'oggetto {{class|KAboutData}} che abbiamo appena creato, in modo da poter usare le opzioni &amp;lt;tt&amp;gt;--version&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;--author&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quindi adiamo a creare l'oggetto {{class|KApplication}}. Ciò bisogna farlo esattamente una volta in ogni programma, visto che è necessario per cose come l'[[Development/Tutorials/Localization/i18n|i18n]].&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo finito con le operazioni indispensabili di KDE, possiamo cominciare a fare cose interessanti con la nostra applicazione. Creeremo una finestra pop-up, ma personalizzeremo uno dei pulsanti. Per la personalizzazione avremo bisogno di un oggetto {{class|KGuiItem}}. Il primo argomento del costruttore di {{class|KGuiItem}} è il testo che apparirà sull'oggetto (nel nostro caso, un pulsante). Quindi ci sarà un opzione per inserire un'icona, ma non ne vogliamo una così gli mettiamo &amp;lt;tt&amp;gt;QString&amp;lt;/tt&amp;gt;. Impostiamo quindi il tooltip (quello che appare quando fermi il puntatore su un elemento) ed infine il testo &amp;quot;Che Cos'è?&amp;quot; (accessibile tramite click col tasto destro, con Shift-F1 oppure cliccando su &amp;quot;?&amp;quot; nella barra del titolo e poi sul pulsante &amp;quot;Ciao&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo il nostro pulsante, possiamo creare il nostro pop-up. Chiamiamo la funzione &amp;lt;tt&amp;gt;{{class|KMessageBox}}::questionYesNo()&amp;lt;/tt&amp;gt; la quale, in modo predefinito, crea una finestra con due pulsanti &amp;quot;Si&amp;quot; e &amp;quot;No&amp;quot;. Il secondo argomento compone il testo mostrato nella finestra sopra i due pulsanti. Il terzo è l'intestazione della finestra; e infine impostiamo il KGuiItem di (quello che normalmente è) il pulsante &amp;quot;Si&amp;quot; con il nostro &amp;lt;tt&amp;gt;KGuiItem yesButton&amp;lt;/tt&amp;gt; appena creato.&lt;br /&gt;
&lt;br /&gt;
Notare come tutto il testo visibile all'utente passa attraverso la funzione i18n(); questo è necessario per permettere la traduzione della UI. Più informazioni sulla localizzazione possono essere trovate nel [[Development/Tutorials/Localization/i18n|tutorial sulla localizzazione]].&lt;br /&gt;
&lt;br /&gt;
Abbiamo finito per quanto riguarda il codice. Ora la compilazione e la prova.&lt;br /&gt;
&lt;br /&gt;
== Compilazione ==&lt;br /&gt;
&lt;br /&gt;
Vorrai usare [[Development/Tutorials/CMake|CMake]] come ambiente di compilazione. Devi fornire un file chiamato CMakeLists.txt, cmake usa questo file per generare tutti i Makefiles.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
Crea un file di nome CMakeLists.txt nella stessa cartella di main.cpp con questo contenuto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial1)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial1_SRCS&lt;br /&gt;
  main.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial1 ${tutorial1_SRCS})&lt;br /&gt;
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial1  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
La funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; trova tutti i pacchetti che hai richiesto (in questo caso KDE4) ed imposta alcune variabili che descrivono la posizione degli header e delle librerie del pacchetto. In questo caso useremo la variabile &amp;lt;tt&amp;gt;KDE4_INCLUDES&amp;lt;/tt&amp;gt; contenente il percorso ai files header di KDE4.&lt;br /&gt;
&lt;br /&gt;
Per permettere al compilatore di trovare questi files, passiamo quella variabile alla funzione &amp;lt;tt&amp;gt;include_directories()&amp;lt;/tt&amp;gt; la quale aggiunge gli header di KDE4 al percorso di ricerca degli header.&lt;br /&gt;
&lt;br /&gt;
Poi creiamo una variabile chiamata &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; con la funzione &amp;lt;tt&amp;gt;set()&amp;lt;/tt&amp;gt;. In questo caso la impostiamo con il nome del nostro unico file sorgente.&lt;br /&gt;
&lt;br /&gt;
Quindi usiamo &amp;lt;tt&amp;gt;kde4_add_executable()&amp;lt;/tt&amp;gt; per creare un eseguibile chiamato &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; dai sorgenti elencati nella nostra variabile &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt;. In seguito, facciamo il link del nostro eseguibile alle librerie kdeui di KDE4 con &amp;lt;tt&amp;gt;target_link_libraries()&amp;lt;/tt&amp;gt; e la variabile &amp;lt;tt&amp;gt;KDE4_KDEUI_LIBS&amp;lt;/tt&amp;gt; precedentemente riempita dalla funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt;. La riga che comincia con &amp;lt;tt&amp;gt;install&amp;lt;/tt&amp;gt; scrive un target di default &amp;quot;install&amp;quot; dentro al Makefile.&lt;br /&gt;
&lt;br /&gt;
=== Make ed Esecuzione ===&lt;br /&gt;
&lt;br /&gt;
Puoi invocare CMake e make manualmente:&lt;br /&gt;
&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake .. # Nota questi due punti - non è una mancanza,&lt;br /&gt;
          # stanno per &amp;quot;cartella superiore&amp;quot;.&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
Oppure, se hai preparato il tuo ambiente come descritto in [[Getting_Started/Build/KDE4|Getting Started/Build/KDE4]], puoi compilare questo codice con:&lt;br /&gt;
 cmakekde&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Quindi lancialo con:&lt;br /&gt;
 ./tutorial1&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KXmlGuiWindow|usare KXmlGuiWindow]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)</id>
		<title>Development/Tutorials/Using KXmlGuiWindow (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)"/>
				<updated>2008-11-29T11:26:05Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Traduzione completata&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KXmlGuiWindow}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Come usare KXmlGuiWindow|&lt;br /&gt;
&lt;br /&gt;
pre=[[Development/Tutorials/First_program_(it)|Tutorial 1 - Ciao Mondo]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions and XMLGUI]]| &lt;br /&gt;
&lt;br /&gt;
reading={{class|KXmlGuiWindow}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial parte dal precedente [[Development/Tutorials/First_program_(it)|Ciao Mondo]] ed introdurrà la classe {{class|KXmlGuiWindow}}.&lt;br /&gt;
&lt;br /&gt;
Nel precedente tutorial il programma faceva apparire una finestra di dialogo, ma ora faremo dei passi verso un programma funzionale.&lt;br /&gt;
&lt;br /&gt;
[[Image:Kxmlguiwindow_tutorial2.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
== KXmlGuiWindow ==&lt;br /&gt;
&lt;br /&gt;
{{class|KXmlGuiWindow}} offre una finestra completa con la barra dei menu, una degli strumenti, la barra di stato ed un'area principale al centro per un grande widget. Molte applicazioni KDE deriveranno da questa classe perché fornisce un modo semplice per la disposizione di questi elementi tramite un file XML (questa tecnologia è chiamata XMLGUI). Anche se non useremo XMLGUI in ''questo'' tutorial, la useremo nel prossimo.&lt;br /&gt;
&lt;br /&gt;
Per poter avere un KXmlGuiWindow utile, dobbiamo ereditarlo. Creeremo due files, un &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; ed un &amp;lt;tt&amp;gt;mainwindow.h&amp;lt;/tt&amp;gt; i quali conterranno il nostro codice.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#ifndef MAINWINDOW_H&lt;br /&gt;
#define MAINWINDOW_H&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;KXmlGuiWindow&amp;gt;&lt;br /&gt;
#include &amp;lt;KTextEdit&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class MainWindow : public KXmlGuiWindow&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    MainWindow(QWidget *genitore=0);&lt;br /&gt;
		&lt;br /&gt;
  private:&lt;br /&gt;
    KTextEdit* areaDiTesto;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto creiamo una sotto-classe di KXmlGuiWindow alla linea 7 con &amp;lt;tt&amp;gt;class MainWindow : public KXmlGuiWindow&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Poi dichiariamo il costruttore con &amp;lt;tt&amp;gt;MainWindow(QWidget *genitore=0);&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;come cacchio si traduce '''cursor auto-hiding''' ???&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
E in fine definiamo un puntatore all'oggetto che sarà il corpo del nostro programma. {{class|KTextEdit}} è un generico editor di testo ricco con qualche finezza di KDE come il &amp;quot;nascondimento&amp;quot; automatico del cursore.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent)&lt;br /&gt;
{&lt;br /&gt;
  areaDiTesto = new KTextEdit();&lt;br /&gt;
  setCentralWidget(areaDiTesto);&lt;br /&gt;
  setupGUI();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alla riga 1, ovviamente, dobbiamo includere gli headers contenenti la definizione delle classi.&lt;br /&gt;
&lt;br /&gt;
Poi sulla riga 5, inizializziamo il nostro editor di testo con un oggetto. Quindi alla sesta riga usiamo la funzione setCentralWidget() interna a KXmlGuiWindow, la quale gli dice cosa dovrebbe apparire nella sezione centrale della finestra.&lt;br /&gt;
&lt;br /&gt;
Infine viene chiamato KXmlGuiWindow::setupGUI(), il quale fa un sacco di cose dietro le quinte e crea la barra dei menu predefinita (Settings, Help).&lt;br /&gt;
&lt;br /&gt;
== Ritorno a main.cpp ==&lt;br /&gt;
&lt;br /&gt;
Affinché di fatto la finestra funzioni, abbiamo bisogno di aggiungere poche linee in main.cpp:&lt;br /&gt;
&lt;br /&gt;
=== main.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&amp;quot;tutorial2&amp;quot;, 0,&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 2&amp;quot;), &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         ki18n(&amp;quot;Una semplice area di testo.&amp;quot;),&lt;br /&gt;
                         KAboutData::License_GPL, ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
&lt;br /&gt;
    MainWindow* window = new MainWindow();&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le sole righe nuove qui (comparate con il primo tutorial) sono 6, 21 e 22. Alla riga 21, creiamo il nostro oggetto MainWindow e alla 22 lo visualizziamo.&lt;br /&gt;
&lt;br /&gt;
== CMake ==&lt;br /&gt;
&lt;br /&gt;
Il miglior modo di compilare il programma è usando CMake. Ciò che cambia dal primo tutorial consiste nell'aggiunta di &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; nella lista dei sorgenti e tutti i &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; diventano &amp;lt;tt&amp;gt;tutorial2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial2)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial2_SRCS &lt;br /&gt;
  main.cpp&lt;br /&gt;
  mainwindow.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial2 ${tutorial2_SRCS})&lt;br /&gt;
target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compilazione ===&lt;br /&gt;
&lt;br /&gt;
Per la compilazione, il link e l'esecuzione, usa:&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake ..&lt;br /&gt;
 make&lt;br /&gt;
 ./tutorial2&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KActions|usare KActions]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)</id>
		<title>Development/Tutorials/Using KXmlGuiWindow (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)"/>
				<updated>2008-11-29T11:15:54Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Numeri di riga&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KXmlGuiWindow}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Come usare KXmlGuiWindow|&lt;br /&gt;
&lt;br /&gt;
pre=[[Development/Tutorials/First_program_(it)|Tutorial 1 - Ciao Mondo]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions and XMLGUI]]| &lt;br /&gt;
&lt;br /&gt;
reading={{class|KXmlGuiWindow}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial parte dal precedente [[Development/Tutorials/First_program_(it)|Ciao Mondo]] ed introdurrà la classe {{class|KXmlGuiWindow}}.&lt;br /&gt;
&lt;br /&gt;
Nel precedente tutorial il programma faceva apparire una finestra di dialogo, ma ora faremo dei passi verso un programma funzionale.&lt;br /&gt;
&lt;br /&gt;
[[Image:Kxmlguiwindow_tutorial2.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
== KXmlGuiWindow ==&lt;br /&gt;
&lt;br /&gt;
{{class|KXmlGuiWindow}} offre una finestra completa con la barra dei menu, una degli strumenti, la barra di stato ed un'area principale al centro per un grande widget. Molte applicazioni KDE deriveranno da questa classe perché fornisce un modo semplice per la disposizione di questi elementi tramite un file XML (questa tecnologia è chiamata XMLGUI). Anche se non useremo XMLGUI in ''questo'' tutorial, la useremo nel prossimo.&lt;br /&gt;
&lt;br /&gt;
Per poter avere un KXmlGuiWindow utile, dobbiamo ereditarlo. Creeremo due files, un &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; ed un &amp;lt;tt&amp;gt;mainwindow.h&amp;lt;/tt&amp;gt; i quali conterranno il nostro codice.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#ifndef MAINWINDOW_H&lt;br /&gt;
#define MAINWINDOW_H&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;KXmlGuiWindow&amp;gt;&lt;br /&gt;
#include &amp;lt;KTextEdit&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class MainWindow : public KXmlGuiWindow&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    MainWindow(QWidget *genitore=0);&lt;br /&gt;
		&lt;br /&gt;
  private:&lt;br /&gt;
    KTextEdit* areaDiTesto;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto creiamo una sotto-classe di KXmlGuiWindow alla linea 7 con &amp;lt;tt&amp;gt;class MainWindow : public KXmlGuiWindow&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Poi dichiariamo il costruttore con &amp;lt;tt&amp;gt;MainWindow(QWidget *genitore=0);&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;come cacchio si traduce '''cursor auto-hiding''' ???&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
E in fine definiamo un puntatore all'oggetto che sarà il corpo del nostro programma. {{class|KTextEdit}} è un generico editor di testo ricco con qualche finezza di KDE come il &amp;quot;nascondimento&amp;quot; automatico del cursore.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent)&lt;br /&gt;
{&lt;br /&gt;
  areaDiTesto = new KTextEdit();&lt;br /&gt;
  setCentralWidget(areaDiTesto);&lt;br /&gt;
  setupGUI();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alla riga 1, ovviamente, dobbiamo includere gli headers contenenti la definizione delle classi.&lt;br /&gt;
&lt;br /&gt;
Poi sulla riga 5, inizializziamo il nostro editor di testo con un oggetto. Quindi alla sesta riga usiamo la funzione setCentralWidget() interna a KXmlGuiWindow, la quale gli dice cosa dovrebbe apparire nella sezione centrale della finestra.&lt;br /&gt;
&lt;br /&gt;
Infine viene chiamato KXmlGuiWindow::setupGUI(), il quale fa un sacco di cose dietro le quinte e crea la barra dei menu predefinita (Settings, Help).&lt;br /&gt;
&lt;br /&gt;
== Ritorno a main.cpp ==&lt;br /&gt;
&lt;br /&gt;
Affinché di fatto la finestra funzioni, abbiamo bisogno di aggiungere poche linee in main.cpp:&lt;br /&gt;
&lt;br /&gt;
=== main.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&amp;quot;tutorial2&amp;quot;, 0,&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 2&amp;quot;), &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         ki18n(&amp;quot;Una semplice area di testo.&amp;quot;),&lt;br /&gt;
                         KAboutData::License_GPL, ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
&lt;br /&gt;
    MainWindow* window = new MainWindow();&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The only new lines here (compared to Tutorial 1) are 5, 18 and 19. On line 18, we create our MainWindow object and then on line 19, we display it.&lt;br /&gt;
&lt;br /&gt;
==CMake==&lt;br /&gt;
The best way to build the program is to use CMake. All that's changed since tutorial 1 is that &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; has been added to the sources list and any &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; has become &amp;lt;tt&amp;gt;tutorial2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
===CMakeLists.txt===&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial2)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial2_SRCS &lt;br /&gt;
  main.cpp&lt;br /&gt;
  mainwindow.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial2 ${tutorial2_SRCS})&lt;br /&gt;
target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Compile it===&lt;br /&gt;
To compile, link and run it, use:&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake ..&lt;br /&gt;
 make&lt;br /&gt;
 ./tutorial2&lt;br /&gt;
&lt;br /&gt;
==Moving On==&lt;br /&gt;
Now you can move on to [[Development/Tutorials/Using_KActions|using KActions]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)</id>
		<title>Development/Tutorials/Using KXmlGuiWindow (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)"/>
				<updated>2008-11-29T11:13:25Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Traduzione fino al codice di main.cpp&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KXmlGuiWindow}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Come usare KXmlGuiWindow|&lt;br /&gt;
&lt;br /&gt;
pre=[[Development/Tutorials/First_program_(it)|Tutorial 1 - Ciao Mondo]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions and XMLGUI]]| &lt;br /&gt;
&lt;br /&gt;
reading={{class|KXmlGuiWindow}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial parte dal precedente [[Development/Tutorials/First_program_(it)|Ciao Mondo]] ed introdurrà la classe {{class|KXmlGuiWindow}}.&lt;br /&gt;
&lt;br /&gt;
Nel precedente tutorial il programma faceva apparire una finestra di dialogo, ma ora faremo dei passi verso un programma funzionale.&lt;br /&gt;
&lt;br /&gt;
[[Image:Kxmlguiwindow_tutorial2.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
== KXmlGuiWindow ==&lt;br /&gt;
&lt;br /&gt;
{{class|KXmlGuiWindow}} offre una finestra completa con la barra dei menu, una degli strumenti, la barra di stato ed un'area principale al centro per un grande widget. Molte applicazioni KDE deriveranno da questa classe perché fornisce un modo semplice per la disposizione di questi elementi tramite un file XML (questa tecnologia è chiamata XMLGUI). Anche se non useremo XMLGUI in ''questo'' tutorial, la useremo nel prossimo.&lt;br /&gt;
&lt;br /&gt;
Per poter avere un KXmlGuiWindow utile, dobbiamo ereditarlo. Creeremo due files, un &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; ed un &amp;lt;tt&amp;gt;mainwindow.h&amp;lt;/tt&amp;gt; i quali conterranno il nostro codice.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#ifndef MAINWINDOW_H&lt;br /&gt;
#define MAINWINDOW_H&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;KXmlGuiWindow&amp;gt;&lt;br /&gt;
#include &amp;lt;KTextEdit&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class MainWindow : public KXmlGuiWindow&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    MainWindow(QWidget *genitore=0);&lt;br /&gt;
		&lt;br /&gt;
  private:&lt;br /&gt;
    KTextEdit* areaDiTesto;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto creiamo una sotto-classe di KXmlGuiWindow alla linea 7 con &amp;lt;tt&amp;gt;class MainWindow : public KXmlGuiWindow&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Poi dichiariamo il costruttore con &amp;lt;tt&amp;gt;MainWindow(QWidget *genitore=0);&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;come cacchio si traduce '''cursor auto-hiding''' ???&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
E in fine definiamo un puntatore all'oggetto che sarà il corpo del nostro programma. {{class|KTextEdit}} è un generico editor di testo ricco con qualche finezza di KDE come il &amp;quot;nascondimento&amp;quot; automatico del cursore.&lt;br /&gt;
&lt;br /&gt;
=== mainwindow.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent)&lt;br /&gt;
{&lt;br /&gt;
  areaDiTesto = new KTextEdit();&lt;br /&gt;
  setCentralWidget(areaDiTesto);&lt;br /&gt;
  setupGUI();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alla riga 1, ovviamente, dobbiamo includere gli headers contenenti la definizione delle classi.&lt;br /&gt;
&lt;br /&gt;
Poi sulla riga 5, inizializziamo il nostro editor di testo con un oggetto. Quindi alla sesta riga usiamo la funzione setCentralWidget() interna a KXmlGuiWindow, la quale gli dice cosa dovrebbe apparire nella sezione centrale della finestra.&lt;br /&gt;
&lt;br /&gt;
Infine viene chiamato KXmlGuiWindow::setupGUI(), il quale fa un sacco di cose dietro le quinte e crea la barra dei menu predefinita (Settings, Help).&lt;br /&gt;
&lt;br /&gt;
== Ritorno a main.cpp ==&lt;br /&gt;
&lt;br /&gt;
Affinché di fatto la finestra funzioni, abbiamo bisogno di aggiungere poche linee in main.cpp:&lt;br /&gt;
&lt;br /&gt;
=== main.cpp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&amp;quot;tutorial2&amp;quot;, 0,&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 2&amp;quot;), &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         ki18n(&amp;quot;Una semplice area di testo.&amp;quot;),&lt;br /&gt;
                         KAboutData::License_GPL, ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
&lt;br /&gt;
    MainWindow* window = new MainWindow();&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The only new lines here (compared to Tutorial 1) are 5, 18 and 19. On line 18, we create our MainWindow object and then on line 19, we display it.&lt;br /&gt;
&lt;br /&gt;
==CMake==&lt;br /&gt;
The best way to build the program is to use CMake. All that's changed since tutorial 1 is that &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; has been added to the sources list and any &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; has become &amp;lt;tt&amp;gt;tutorial2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
===CMakeLists.txt===&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial2)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial2_SRCS &lt;br /&gt;
  main.cpp&lt;br /&gt;
  mainwindow.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial2 ${tutorial2_SRCS})&lt;br /&gt;
target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Compile it===&lt;br /&gt;
To compile, link and run it, use:&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake ..&lt;br /&gt;
 make&lt;br /&gt;
 ./tutorial2&lt;br /&gt;
&lt;br /&gt;
==Moving On==&lt;br /&gt;
Now you can move on to [[Development/Tutorials/Using_KActions|using KActions]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)</id>
		<title>Development/Tutorials/Using KXmlGuiWindow (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)"/>
				<updated>2008-11-29T10:11:26Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Introduzione + immagine&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KXmlGuiWindow}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Come usare KXmlGuiWindow|&lt;br /&gt;
&lt;br /&gt;
pre=[[Development/Tutorials/First_program_(it)|Tutorial 1 - Ciao Mondo]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions and XMLGUI]]| &lt;br /&gt;
&lt;br /&gt;
reading={{class|KXmlGuiWindow}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial parte dal precedente [[Development/Tutorials/First_program_(it)|Ciao Mondo]] ed introdurrà la classe {{class|KXmlGuiWindow}}.&lt;br /&gt;
&lt;br /&gt;
Nel precedente tutorial il programma faceva apparire una finestra di dialogo, ma ora faremo dei passi verso un programma funzionale.&lt;br /&gt;
&lt;br /&gt;
[[Image:Kxmlguiwindow_tutorial2.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
==KXmlGuiWindow==&lt;br /&gt;
&lt;br /&gt;
{{class|KXmlGuiWindow}} provides a full main window view with menubars, toolbars, a statusbar and a main area in the centre for a large widget. Most KDE applications will derive from this class as it provides an easy way to define menu and toolbar layouts through XML files (this technology is called XMLGUI). While we will not be using XMLGUI in ''this'' tutorial, we will use it in the next.&lt;br /&gt;
&lt;br /&gt;
In order to have a useful KXmlGuiWindow, we must subclass it. So we create two files, a &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; and a &amp;lt;tt&amp;gt;mainwindow.h&amp;lt;/tt&amp;gt; which will contain our code.&lt;br /&gt;
&lt;br /&gt;
===mainwindow.h===&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#ifndef MAINWINDOW_H&lt;br /&gt;
#define MAINWINDOW_H&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;KXmlGuiWindow&amp;gt;&lt;br /&gt;
#include &amp;lt;KTextEdit&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class MainWindow : public KXmlGuiWindow&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    MainWindow(QWidget *parent=0);&lt;br /&gt;
		&lt;br /&gt;
  private:&lt;br /&gt;
    KTextEdit* textArea;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
First we Subclass KXmlGuiWindow on line 7 with &amp;lt;tt&amp;gt;class MainWindow : public KXmlGuiWindow&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Then we declare the constructor with &amp;lt;tt&amp;gt;MainWindow(QWidget *parent=0);&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And finally we declare a pointer to the object that will make up the bulk of our program. {{class|KTextEdit}} is a generic richtext editor with some KDE niceties like cursor auto-hiding.&lt;br /&gt;
&lt;br /&gt;
===mainwindow.cpp===&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent)&lt;br /&gt;
{&lt;br /&gt;
  textArea = new KTextEdit();&lt;br /&gt;
  setCentralWidget(textArea);&lt;br /&gt;
  setupGUI();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
First, of course, on line 1 we have to include the header file containing the class declaration.&lt;br /&gt;
&lt;br /&gt;
On line 5, we initialise our text editor with an object. Then on line 6 we use KXmlGuiWindow's built-in setCentralWidget() function which tells the KXmlGuiWindow what should appear in the central section of the window.&lt;br /&gt;
&lt;br /&gt;
Finally, KXmlGuiWindow::setupGUI() is called which does a lot of behind-the-scenes stuff and creates the default menu bars (Settings, Help).&lt;br /&gt;
&lt;br /&gt;
==Back to main.cpp==&lt;br /&gt;
In order to actually run this window, we need to add a few lines in main.cpp:&lt;br /&gt;
===main.cpp===&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  KAboutData aboutData( &amp;quot;tutorial2&amp;quot;, 0,&lt;br /&gt;
      ki18n(&amp;quot;Tutorial 2&amp;quot;), &amp;quot;1.0&amp;quot;,&lt;br /&gt;
      ki18n(&amp;quot;A simple text area&amp;quot;),&lt;br /&gt;
      KAboutData::License_GPL,&lt;br /&gt;
      ki18n(&amp;quot;Copyright (c) 2007 Developer&amp;quot;) );&lt;br /&gt;
  KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
  &lt;br /&gt;
  KApplication app;&lt;br /&gt;
 &lt;br /&gt;
  MainWindow* window = new MainWindow();&lt;br /&gt;
  window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
  return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
The only new lines here (compared to Tutorial 1) are 5, 18 and 19. On line 18, we create our MainWindow object and then on line 19, we display it.&lt;br /&gt;
&lt;br /&gt;
==CMake==&lt;br /&gt;
The best way to build the program is to use CMake. All that's changed since tutorial 1 is that &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; has been added to the sources list and any &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; has become &amp;lt;tt&amp;gt;tutorial2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
===CMakeLists.txt===&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial2)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial2_SRCS &lt;br /&gt;
  main.cpp&lt;br /&gt;
  mainwindow.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial2 ${tutorial2_SRCS})&lt;br /&gt;
target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Compile it===&lt;br /&gt;
To compile, link and run it, use:&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake ..&lt;br /&gt;
 make&lt;br /&gt;
 ./tutorial2&lt;br /&gt;
&lt;br /&gt;
==Moving On==&lt;br /&gt;
Now you can move on to [[Development/Tutorials/Using_KActions|using KActions]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/File:Kxmlguiwindow_tutorial2.png</id>
		<title>File:Kxmlguiwindow tutorial2.png</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/File:Kxmlguiwindow_tutorial2.png"/>
				<updated>2008-11-29T10:05:27Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)</id>
		<title>Development/Tutorials/Using KXmlGuiWindow (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(it)"/>
				<updated>2008-11-29T09:58:13Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Inizio - tradotto il tutorial browser&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KXmlGuiWindow}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=How To Use KXmlGuiWindow|&lt;br /&gt;
&lt;br /&gt;
pre=[[Development/Tutorials/First_program_(it)|Tutorial 1 - Ciao Mondo]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions and XMLGUI]]| &lt;br /&gt;
&lt;br /&gt;
reading={{class|KXmlGuiWindow}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Abstract==&lt;br /&gt;
This tutorial carries on from [[Development/Tutorials/First_program|First Program Tutorial]] and will introduce the {{class|KXmlGuiWindow}} class.&lt;br /&gt;
&lt;br /&gt;
In the previous tutorial, the program caused a dialog box to pop up but we're going to take steps towards a functioning application.&lt;br /&gt;
&lt;br /&gt;
[[image:introtokdetutorial2.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
==KXmlGuiWindow==&lt;br /&gt;
&lt;br /&gt;
{{class|KXmlGuiWindow}} provides a full main window view with menubars, toolbars, a statusbar and a main area in the centre for a large widget. Most KDE applications will derive from this class as it provides an easy way to define menu and toolbar layouts through XML files (this technology is called XMLGUI). While we will not be using XMLGUI in ''this'' tutorial, we will use it in the next.&lt;br /&gt;
&lt;br /&gt;
In order to have a useful KXmlGuiWindow, we must subclass it. So we create two files, a &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; and a &amp;lt;tt&amp;gt;mainwindow.h&amp;lt;/tt&amp;gt; which will contain our code.&lt;br /&gt;
&lt;br /&gt;
===mainwindow.h===&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#ifndef MAINWINDOW_H&lt;br /&gt;
#define MAINWINDOW_H&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;KXmlGuiWindow&amp;gt;&lt;br /&gt;
#include &amp;lt;KTextEdit&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class MainWindow : public KXmlGuiWindow&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    MainWindow(QWidget *parent=0);&lt;br /&gt;
		&lt;br /&gt;
  private:&lt;br /&gt;
    KTextEdit* textArea;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
First we Subclass KXmlGuiWindow on line 7 with &amp;lt;tt&amp;gt;class MainWindow : public KXmlGuiWindow&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Then we declare the constructor with &amp;lt;tt&amp;gt;MainWindow(QWidget *parent=0);&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And finally we declare a pointer to the object that will make up the bulk of our program. {{class|KTextEdit}} is a generic richtext editor with some KDE niceties like cursor auto-hiding.&lt;br /&gt;
&lt;br /&gt;
===mainwindow.cpp===&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent)&lt;br /&gt;
{&lt;br /&gt;
  textArea = new KTextEdit();&lt;br /&gt;
  setCentralWidget(textArea);&lt;br /&gt;
  setupGUI();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
First, of course, on line 1 we have to include the header file containing the class declaration.&lt;br /&gt;
&lt;br /&gt;
On line 5, we initialise our text editor with an object. Then on line 6 we use KXmlGuiWindow's built-in setCentralWidget() function which tells the KXmlGuiWindow what should appear in the central section of the window.&lt;br /&gt;
&lt;br /&gt;
Finally, KXmlGuiWindow::setupGUI() is called which does a lot of behind-the-scenes stuff and creates the default menu bars (Settings, Help).&lt;br /&gt;
&lt;br /&gt;
==Back to main.cpp==&lt;br /&gt;
In order to actually run this window, we need to add a few lines in main.cpp:&lt;br /&gt;
===main.cpp===&lt;br /&gt;
&amp;lt;code cppqt n&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;mainwindow.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  KAboutData aboutData( &amp;quot;tutorial2&amp;quot;, 0,&lt;br /&gt;
      ki18n(&amp;quot;Tutorial 2&amp;quot;), &amp;quot;1.0&amp;quot;,&lt;br /&gt;
      ki18n(&amp;quot;A simple text area&amp;quot;),&lt;br /&gt;
      KAboutData::License_GPL,&lt;br /&gt;
      ki18n(&amp;quot;Copyright (c) 2007 Developer&amp;quot;) );&lt;br /&gt;
  KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
  &lt;br /&gt;
  KApplication app;&lt;br /&gt;
 &lt;br /&gt;
  MainWindow* window = new MainWindow();&lt;br /&gt;
  window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
  return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
The only new lines here (compared to Tutorial 1) are 5, 18 and 19. On line 18, we create our MainWindow object and then on line 19, we display it.&lt;br /&gt;
&lt;br /&gt;
==CMake==&lt;br /&gt;
The best way to build the program is to use CMake. All that's changed since tutorial 1 is that &amp;lt;tt&amp;gt;mainwindow.cpp&amp;lt;/tt&amp;gt; has been added to the sources list and any &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; has become &amp;lt;tt&amp;gt;tutorial2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
===CMakeLists.txt===&lt;br /&gt;
&amp;lt;code ini n&amp;gt;&lt;br /&gt;
project (tutorial2)&lt;br /&gt;
&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
&lt;br /&gt;
set(tutorial2_SRCS &lt;br /&gt;
  main.cpp&lt;br /&gt;
  mainwindow.cpp&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
kde4_add_executable(tutorial2 ${tutorial2_SRCS})&lt;br /&gt;
target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Compile it===&lt;br /&gt;
To compile, link and run it, use:&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake ..&lt;br /&gt;
 make&lt;br /&gt;
 ./tutorial2&lt;br /&gt;
&lt;br /&gt;
==Moving On==&lt;br /&gt;
Now you can move on to [[Development/Tutorials/Using_KActions|using KActions]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-29T09:56:26Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In order to learn how to develop cool KDE application I've start from the beginning: I aim to translate all the tutorial that I'm facing with.&lt;br /&gt;
&lt;br /&gt;
== Done ==&lt;br /&gt;
&lt;br /&gt;
* [[Template:TutorialBrowser_(it)]]&lt;br /&gt;
* [[Development/Tutorials/First_program_(it)]]&lt;br /&gt;
&lt;br /&gt;
== Work in Progress ==&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Using_KXmlGuiWindow_(it)]]&lt;br /&gt;
&lt;br /&gt;
=== to recheck ===&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it) | Development/Tutorials/Common_Programming_Mistakes_(it)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Who's Next ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/Using_KActions]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-29T09:42:35Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In order to learn how to develop cool KDE application I've start from the beginning: I aim to translate all the tutorial that I'm facing with.&lt;br /&gt;
&lt;br /&gt;
== Done ==&lt;br /&gt;
&lt;br /&gt;
* [[Template:TutorialBrowser_(it)]]&lt;br /&gt;
* [[Development/Tutorials/First_program_(it)]]&lt;br /&gt;
&lt;br /&gt;
== Work in Progress ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== to recheck ===&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it) | Development/Tutorials/Common_Programming_Mistakes_(it)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Who's Next ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/Using_KXmlGuiWindow | Using KXmlGuiWindow]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-29T09:41:52Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In order to learn how to develop cool KDE application I've start from the beginning: I aim to translate all the tutorial that I'm facing with.&lt;br /&gt;
&lt;br /&gt;
== Done ==&lt;br /&gt;
&lt;br /&gt;
* [[Template:TutorialBrowser_(it)]]&lt;br /&gt;
* [[Development/Tutorials/First_program | Tutorial Hello World]]&lt;br /&gt;
&lt;br /&gt;
== Work in Progress ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== to recheck ===&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it) | Development/Tutorials/Common_Programming_Mistakes_(it)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Who's Next ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/Using_KXmlGuiWindow | Using KXmlGuiWindow]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/First_program_(it)</id>
		<title>Development/Tutorials/First program (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/First_program_(it)"/>
				<updated>2008-11-28T16:13:20Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: User:Fresbeeplayer/Development/Tutorials/First program (it) moved to Development/Tutorials/First program (it): Traduzione completa, programma funzionante&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/First_program}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Hello World|&lt;br /&gt;
&lt;br /&gt;
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KXmlGuiWindow|Tutorial 2 - KXmlGuiWindow]]| &lt;br /&gt;
&lt;br /&gt;
reading=[[Development/Tutorials/CMake|CMake]]&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Il tuo primo programma dovrebbe salutare il mondo con un amichevole &amp;quot;Ciao Mondo&amp;quot;, giusto? Per farlo, useremo un {{class|KMessageBox}} personalizzando uno dei pulsanti.&lt;br /&gt;
&lt;br /&gt;
[[image:Ciaomondo_tutorial1.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
{{tip|Per avere più informazioni su qualunque classe che incontri, Konqueror offre una veloce scorciatoia. Per cercare informazioni circa KMessageBox, basta digitare &amp;quot;kde:kmessagebox&amp;quot; in Konqueror e sarai portato alla documentazione.}}&lt;br /&gt;
&lt;br /&gt;
{{tip|Vorresti poter usare KDevelop per i tuoi progetti, il quale fa molte cose carine come il completamento del codice, facile accesso alla documentazione delle API, supporto per il debugging.&lt;br /&gt;
Leggi [[Getting_Started/Set_up_KDE_4_for_development#KDevelop|questo tutorial]] per configurare KDevelop correttamente. Puoi controllare se il setup è andato a buon fine aprendo un'applicazione KDE4 esistente con KDevelop.&lt;br /&gt;
Tuttavia hai ancora bisogno di editare a mano i files di CMake.}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Il Codice ==&lt;br /&gt;
&lt;br /&gt;
Tutto il codice di cui abbiamo bisogno starà in un file, &amp;lt;tt&amp;gt;main.cpp&amp;lt;/tt&amp;gt;. Crealo con il codice qua sotto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;KMessageBox&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&lt;br /&gt;
                         // Il nome del programma, usato internamente.&lt;br /&gt;
                         &amp;quot;tutorial1&amp;quot;,&lt;br /&gt;
                         // Il nome nel catalogo messaggi.&lt;br /&gt;
                         // Se nullo, verrà usato il nome del programma.&lt;br /&gt;
                         0,&lt;br /&gt;
                         // Una stringa con il nome del programma.&lt;br /&gt;
                         // Quella che verrà effettivamente visualizzata.&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 1&amp;quot;),&lt;br /&gt;
                         // La stringa con la versione del programma.&lt;br /&gt;
                         &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         // Piccola descrizione su cosa fa il programma.&lt;br /&gt;
                         ki18n(&amp;quot;Visualizza una finestra KMessageBox.&amp;quot;),&lt;br /&gt;
                         // La licenza con la quale il codice è rilasciato.&lt;br /&gt;
                         KAboutData::License_GPL,&lt;br /&gt;
                         // Copyright&lt;br /&gt;
                         ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         // Testo opzionale mostrato in &amp;quot;Informazioni su&amp;quot;.&lt;br /&gt;
                         // Può contenere qualunque informazione desiderata.&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         // La stringa con la homepage del programma.&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         // L'indirizzo email per la segnalazione di bug.&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
    KGuiItem yesButton( i18n( &amp;quot;Ciao&amp;quot; ), QString(),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un tooltip&amp;quot; ),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un testo di aiuto CheCos'è?.&amp;quot; ) );&lt;br /&gt;
    KMessageBox::questionYesNo( 0, i18n( &amp;quot;Ciao Mondo&amp;quot; ),&lt;br /&gt;
                                i18n( &amp;quot;Ciao&amp;quot; ), yesButton );&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Il primo codice specifico di KDE che incontriamo in questo programma è {{class|KAboutData}}. Questa è la classe utilizzata per immagazzinare le informazioni sul programma, come ad esempio una piccola descrizione, gli autori, o le informazioni sulla licenza. Praticamente ogni applicazione KDE dovrebbe usare questa classe.&lt;br /&gt;
&lt;br /&gt;
Poi arriviamo a {{class|KCmdLineArgs}}. Questa è la classe che si usa quando si vogliono specificare istruzioni da riga di comando per, ad esempio, aprire il programma con uno specifico file. Comunque, in questo tutorial, semplicemente lo inizializziamo con l'oggetto {{class|KAboutData}} che abbiamo appena creato, in modo da poter usare le opzioni &amp;lt;tt&amp;gt;--version&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;--author&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quindi adiamo a creare l'oggetto {{class|KApplication}}. Ciò bisogna farlo esattamente una volta in ogni programma, visto che è necessario per cose come l'[[Development/Tutorials/Localization/i18n|i18n]].&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo finito con le operazioni indispensabili di KDE, possiamo cominciare a fare cose interessanti con la nostra applicazione. Creeremo una finestra pop-up, ma personalizzeremo uno dei pulsanti. Per la personalizzazione avremo bisogno di un oggetto {{class|KGuiItem}}. Il primo argomento del costruttore di {{class|KGuiItem}} è il testo che apparirà sull'oggetto (nel nostro caso, un pulsante). Quindi ci sarà un opzione per inserire un'icona, ma non ne vogliamo una così gli mettiamo &amp;lt;tt&amp;gt;QString&amp;lt;/tt&amp;gt;. Impostiamo quindi il tooltip (quello che appare quando fermi il puntatore su un elemento) ed infine il testo &amp;quot;Che Cos'è?&amp;quot; (accessibile tramite click col tasto destro, con Shift-F1 oppure cliccando su &amp;quot;?&amp;quot; nella barra del titolo e poi sul pulsante &amp;quot;Ciao&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo il nostro pulsante, possiamo creare il nostro pop-up. Chiamiamo la funzione &amp;lt;tt&amp;gt;{{class|KMessageBox}}::questionYesNo()&amp;lt;/tt&amp;gt; la quale, in modo predefinito, crea una finestra con due pulsanti &amp;quot;Si&amp;quot; e &amp;quot;No&amp;quot;. Il secondo argomento compone il testo mostrato nella finestra sopra i due pulsanti. Il terzo è l'intestazione della finestra; e infine impostiamo il KGuiItem di (quello che normalmente è) il pulsante &amp;quot;Si&amp;quot; con il nostro &amp;lt;tt&amp;gt;KGuiItem yesButton&amp;lt;/tt&amp;gt; appena creato.&lt;br /&gt;
&lt;br /&gt;
Notare come tutto il testo visibile all'utente passa attraverso la funzione i18n(); questo è necessario per permettere la traduzione della UI. Più informazioni sulla localizzazione possono essere trovate nel [[Development/Tutorials/Localization/i18n|tutorial sulla localizzazione]].&lt;br /&gt;
&lt;br /&gt;
Abbiamo finito per quanto riguarda il codice. Ora la compilazione e la prova.&lt;br /&gt;
&lt;br /&gt;
== Compilazione ==&lt;br /&gt;
&lt;br /&gt;
Vorrai usare [[Development/Tutorials/CMake|CMake]] come ambiente di compilazione. Devi fornire un file chiamato CMakeLists.txt, cmake usa questo file per generare tutti i Makefiles.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
Crea un file di nome CMakeLists.txt nella stessa cartella di main.cpp con questo contenuto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
project (tutorial1)&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include (KDE4Defaults)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
set(tutorial1_SRCS main.cpp)&lt;br /&gt;
kde4_add_executable(tutorial1 ${tutorial1_SRCS})&lt;br /&gt;
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial1  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
La funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; trova tutti i pacchetti che hai richiesto (in questo caso KDE4) ed imposta alcune variabili che descrivono la posizione degli header e delle librerie del pacchetto. In questo caso useremo la variabile &amp;lt;tt&amp;gt;KDE4_INCLUDES&amp;lt;/tt&amp;gt; contenente il percorso ai files header di KDE4.&lt;br /&gt;
&lt;br /&gt;
Per permettere al compilatore di trovare questi files, passiamo quella variabile alla funzione &amp;lt;tt&amp;gt;include_directories()&amp;lt;/tt&amp;gt; la quale aggiunge gli header di KDE4 al percorso di ricerca degli headr.&lt;br /&gt;
&lt;br /&gt;
Poi creiamo una variabile chiamata &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; con la funzione &amp;lt;tt&amp;gt;set()&amp;lt;/tt&amp;gt;. In questo caso la impostiamo con il nome del nostro unico file sorgente.&lt;br /&gt;
&lt;br /&gt;
Quindi usiamo &amp;lt;tt&amp;gt;kde4_add_executable()&amp;lt;/tt&amp;gt; per creare un eseguibile chiamato &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; dai sorgenti elencati nella nostra variabile &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt;. In seguito, facciamo il link del nostro eseguibile alle librerie kdeui di KDE4 con &amp;lt;tt&amp;gt;target_link_libraries()&amp;lt;/tt&amp;gt; e la variabile &amp;lt;tt&amp;gt;KDE4_KDEUI_LIBS&amp;lt;/tt&amp;gt; precedentemente riempita dalla funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt;. La riga che comincia con &amp;lt;tt&amp;gt;install&amp;lt;/tt&amp;gt; scrive un target di default &amp;quot;install&amp;quot; dentro al Makefile.&lt;br /&gt;
&lt;br /&gt;
=== Make ed Esecuzione ===&lt;br /&gt;
&lt;br /&gt;
Puoi invocare CMake e make manualmente:&lt;br /&gt;
&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake .. # Nota questi due punti - non è una mancanza,&lt;br /&gt;
          # stanno per &amp;quot;cartella superiore&amp;quot;.&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
Oppure, se hai preparato il tuo ambiente come descritto in [[Getting_Started/Build/KDE4|Getting Started/Build/KDE4]], puoi compilare questo codice con:&lt;br /&gt;
 cmakekde&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Quindi lancialo con:&lt;br /&gt;
 ./tutorial1&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KXmlGuiWindow|usare KXmlGuiWindow]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/First_program_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/First program (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/First_program_(it)"/>
				<updated>2008-11-28T16:13:20Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: User:Fresbeeplayer/Development/Tutorials/First program (it) moved to Development/Tutorials/First program (it): Traduzione completa, programma funzionante&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Development/Tutorials/First program (it)]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Talk:Development/Tutorials/First_program</id>
		<title>Talk:Development/Tutorials/First program</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Talk:Development/Tutorials/First_program"/>
				<updated>2008-11-28T16:03:44Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Adding authors to wiki pages seems counterintuitive to me. The wiki is editable by everybody. Why does it matter who the author of a tutorial is? IMHO the only thing that matters is that the tutorial is kept up to date. --[[User:Mattr|Mattr]] 03:31, 4 January 2007 (CET)&lt;br /&gt;
&lt;br /&gt;
: I agree with Matt. I usually add a ''Initial Author:'' at the bottom for pages from the old developer wiki code, because they wrote it with often no explicit copyright information. See also [[Talk:Development/Architecture|this page]] --[[User:Dhaumann|Dhaumann]] 17:36, 4 January 2007 (CET)&lt;br /&gt;
&lt;br /&gt;
== ‘ki18n’ was not declared in this scope ==&lt;br /&gt;
&lt;br /&gt;
I don't know if this is the place to post this, but I tried to follow this tutorial and get an &amp;quot;error: ‘ki18n’ was not declared in this scope&amp;quot;, when trying to compile using the big command gcc main.cpp ...&lt;br /&gt;
&lt;br /&gt;
:Well I change a little the code so it now compiles under KDE4, it seems that the constructor of KAboutDate was changed...&lt;br /&gt;
&lt;br /&gt;
== Build ==&lt;br /&gt;
&lt;br /&gt;
Does the g++ command work for you as described?&lt;br /&gt;
I needed to add -lQtXml, lQtSvg and lQtNetwork.&lt;br /&gt;
&lt;br /&gt;
: I'm getting a 'huge' string of error messages in the underlying include/ files, things like&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
include/KDE/../kmessagebox.h:1128: error: expected constructor, destructor, or type conversion before ‘(’ token&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
but hundreds, possibly thousands of errors apparently affecting nearly every header.&lt;br /&gt;
::I personally haven't tried the g++ method, that was added by someone else. Does linking against those libraries fix the errors? Does it work for you with the cmake method? --[[User:Milliams|milliams]] 20:47, 25 January 2008 (CET)&lt;br /&gt;
:::Thanks you, Milliams, it complies with cmake.  I copied the g++ instructions, including the additional options, straight from the page and still no joy.&lt;br /&gt;
&lt;br /&gt;
== Make and run before explaining the code ==&lt;br /&gt;
&lt;br /&gt;
As far as I can see, &lt;br /&gt;
 it presents the code,&lt;br /&gt;
 explains it, &lt;br /&gt;
 shows how to build with g++ (without cmake), &lt;br /&gt;
 building with cmake, &lt;br /&gt;
 finally running it&lt;br /&gt;
&lt;br /&gt;
Well, I would prefer it to present code and run it (with no explaining), like to a monkey. After showing it works, explaining why it works or how it works.&lt;br /&gt;
I would find that much more fun this way.&lt;br /&gt;
Got any ideas why it should not be that way?&lt;br /&gt;
--Bogdan Bivolaru&lt;br /&gt;
&lt;br /&gt;
== The constructor of KAboutData has changed ==&lt;br /&gt;
&lt;br /&gt;
...or maybe not, I don't know, but in the API doc it is defined with the last two arguments as QByteArray datatype.&lt;br /&gt;
I was translating this tutorial in italian and, in order to solve this problem, I'd added QByteArray(&amp;quot;string...&amp;quot;) to the homepage and bugs mail arguments in the main.cpp code (with the right #include of course).&lt;br /&gt;
Now it build and run like a charm. Maybe it is needed to propagate this little mod over all the tutorial serie? --[[User:Fresbeeplayer|Fresbeeplayer]] 12:18, 28 November 2008 (UTC)&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Talk:Development/Tutorials/First_program</id>
		<title>Talk:Development/Tutorials/First program</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Talk:Development/Tutorials/First_program"/>
				<updated>2008-11-28T12:19:25Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Adding authors to wiki pages seems counterintuitive to me. The wiki is editable by everybody. Why does it matter who the author of a tutorial is? IMHO the only thing that matters is that the tutorial is kept up to date. --[[User:Mattr|Mattr]] 03:31, 4 January 2007 (CET)&lt;br /&gt;
&lt;br /&gt;
: I agree with Matt. I usually add a ''Initial Author:'' at the bottom for pages from the old developer wiki code, because they wrote it with often no explicit copyright information. See also [[Talk:Development/Architecture|this page]] --[[User:Dhaumann|Dhaumann]] 17:36, 4 January 2007 (CET)&lt;br /&gt;
&lt;br /&gt;
== ‘ki18n’ was not declared in this scope ==&lt;br /&gt;
&lt;br /&gt;
I don't know if this is the place to post this, but I tried to follow this tutorial and get an &amp;quot;error: ‘ki18n’ was not declared in this scope&amp;quot;, when trying to compile using the big command gcc main.cpp ...&lt;br /&gt;
&lt;br /&gt;
:Well I change a little the code so it now compiles under KDE4, it seems that the constructor of KAboutDate was changed...&lt;br /&gt;
&lt;br /&gt;
== Build ==&lt;br /&gt;
&lt;br /&gt;
Does the g++ command work for you as described?&lt;br /&gt;
I needed to add -lQtXml, lQtSvg and lQtNetwork.&lt;br /&gt;
&lt;br /&gt;
: I'm getting a 'huge' string of error messages in the underlying include/ files, things like&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
include/KDE/../kmessagebox.h:1128: error: expected constructor, destructor, or type conversion before ‘(’ token&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
but hundreds, possibly thousands of errors apparently affecting nearly every header.&lt;br /&gt;
::I personally haven't tried the g++ method, that was added by someone else. Does linking against those libraries fix the errors? Does it work for you with the cmake method? --[[User:Milliams|milliams]] 20:47, 25 January 2008 (CET)&lt;br /&gt;
:::Thanks you, Milliams, it complies with cmake.  I copied the g++ instructions, including the additional options, straight from the page and still no joy.&lt;br /&gt;
&lt;br /&gt;
== Make and run before explaining the code ==&lt;br /&gt;
&lt;br /&gt;
As far as I can see, &lt;br /&gt;
 it presents the code,&lt;br /&gt;
 explains it, &lt;br /&gt;
 shows how to build with g++ (without cmake), &lt;br /&gt;
 building with cmake, &lt;br /&gt;
 finally running it&lt;br /&gt;
&lt;br /&gt;
Well, I would prefer it to present code and run it (with no explaining), like to a monkey. After showing it works, explaining why it works or how it works.&lt;br /&gt;
I would find that much more fun this way.&lt;br /&gt;
Got any ideas why it should not be that way?&lt;br /&gt;
--Bogdan Bivolaru&lt;br /&gt;
&lt;br /&gt;
== The constructor of KAboutData has changed ==&lt;br /&gt;
&lt;br /&gt;
...or maybe not, I don't know, but in the API doc it is defined with the last two arguments of QByteArray datatype.&lt;br /&gt;
I was translating this tutorial in italian and, in order to solve this problem, I'd added QByteArray(&amp;quot;string...&amp;quot;) to the homepage and bugs mail in the main.cpp code (and the right #include of course).&lt;br /&gt;
Now it build and run like a charm. Maybe it is needed to spread this little mod over all the tutorial serie? --[[User:Fresbeeplayer|Fresbeeplayer]] 12:18, 28 November 2008 (UTC)&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Talk:Development/Tutorials/First_program</id>
		<title>Talk:Development/Tutorials/First program</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Talk:Development/Tutorials/First_program"/>
				<updated>2008-11-28T12:18:58Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: /* The constructor of KAboutData has changed */ new section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Adding authors to wiki pages seems counterintuitive to me. The wiki is editable by everybody. Why does it matter who the author of a tutorial is? IMHO the only thing that matters is that the tutorial is kept up to date. --[[User:Mattr|Mattr]] 03:31, 4 January 2007 (CET)&lt;br /&gt;
&lt;br /&gt;
: I agree with Matt. I usually add a ''Initial Author:'' at the bottom for pages from the old developer wiki code, because they wrote it with often no explicit copyright information. See also [[Talk:Development/Architecture|this page]] --[[User:Dhaumann|Dhaumann]] 17:36, 4 January 2007 (CET)&lt;br /&gt;
&lt;br /&gt;
== ‘ki18n’ was not declared in this scope ==&lt;br /&gt;
&lt;br /&gt;
I don't know if this is the place to post this, but I tried to follow this tutorial and get an &amp;quot;error: ‘ki18n’ was not declared in this scope&amp;quot;, when trying to compile using the big command gcc main.cpp ...&lt;br /&gt;
&lt;br /&gt;
:Well I change a little the code so it now compiles under KDE4, it seems that the constructor of KAboutDate was changed...&lt;br /&gt;
&lt;br /&gt;
== Build ==&lt;br /&gt;
&lt;br /&gt;
Does the g++ command work for you as described?&lt;br /&gt;
I needed to add -lQtXml, lQtSvg and lQtNetwork.&lt;br /&gt;
&lt;br /&gt;
: I'm getting a 'huge' string of error messages in the underlying include/ files, things like&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
include/KDE/../kmessagebox.h:1128: error: expected constructor, destructor, or type conversion before ‘(’ token&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
but hundreds, possibly thousands of errors apparently affecting nearly every header.&lt;br /&gt;
::I personally haven't tried the g++ method, that was added by someone else. Does linking against those libraries fix the errors? Does it work for you with the cmake method? --[[User:Milliams|milliams]] 20:47, 25 January 2008 (CET)&lt;br /&gt;
:::Thanks you, Milliams, it complies with cmake.  I copied the g++ instructions, including the additional options, straight from the page and still no joy.&lt;br /&gt;
&lt;br /&gt;
== Make and run before explaining the code ==&lt;br /&gt;
&lt;br /&gt;
As far as I can see, &lt;br /&gt;
 it presents the code,&lt;br /&gt;
 explains it, &lt;br /&gt;
 shows how to build with g++ (without cmake), &lt;br /&gt;
 building with cmake, &lt;br /&gt;
 finally running it&lt;br /&gt;
&lt;br /&gt;
Well, I would prefer it to present code and run it (with no explaining), like to a monkey. After showing it works, explaining why it works or how it works.&lt;br /&gt;
I would find that much more fun this way.&lt;br /&gt;
Got any ideas why it should not be that way?&lt;br /&gt;
--Bogdan Bivolaru&lt;br /&gt;
&lt;br /&gt;
== The constructor of KAboutData has changed ==&lt;br /&gt;
&lt;br /&gt;
...or maybe not, I don't know, but in the API doc it is defined with the last two arguments of QByteArray datatype.&lt;br /&gt;
I was translating this tutorial in italian and, in order to solve this problem, I'd added QByteArray(&amp;quot;string...&amp;quot;) to the homepage and bugs mail in the main.cpp code (and the right #include of course).&lt;br /&gt;
Now it build and run like a charm. Maybe it is needed to spread this little mod over all the tutorial serie?&amp;lt;br/&amp;gt;&lt;br /&gt;
     [[User:Fresbeeplayer|Fresbeeplayer]] 12:18, 28 November 2008 (UTC)&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/First_program_(it)</id>
		<title>Development/Tutorials/First program (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/First_program_(it)"/>
				<updated>2008-11-28T12:02:29Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Sistemata l'inizializzazione di KAboutData - QByteArray&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/First_program}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Hello World|&lt;br /&gt;
&lt;br /&gt;
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KXmlGuiWindow|Tutorial 2 - KXmlGuiWindow]]| &lt;br /&gt;
&lt;br /&gt;
reading=[[Development/Tutorials/CMake|CMake]]&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Il tuo primo programma dovrebbe salutare il mondo con un amichevole &amp;quot;Ciao Mondo&amp;quot;, giusto? Per farlo, useremo un {{class|KMessageBox}} personalizzando uno dei pulsanti.&lt;br /&gt;
&lt;br /&gt;
[[image:Ciaomondo_tutorial1.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
{{tip|Per avere più informazioni su qualunque classe che incontri, Konqueror offre una veloce scorciatoia. Per cercare informazioni circa KMessageBox, basta digitare &amp;quot;kde:kmessagebox&amp;quot; in Konqueror e sarai portato alla documentazione.}}&lt;br /&gt;
&lt;br /&gt;
{{tip|Vorresti poter usare KDevelop per i tuoi progetti, il quale fa molte cose carine come il completamento del codice, facile accesso alla documentazione delle API, supporto per il debugging.&lt;br /&gt;
Leggi [[Getting_Started/Set_up_KDE_4_for_development#KDevelop|questo tutorial]] per configurare KDevelop correttamente. Puoi controllare se il setup è andato a buon fine aprendo un'applicazione KDE4 esistente con KDevelop.&lt;br /&gt;
Tuttavia hai ancora bisogno di editare a mano i files di CMake.}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Il Codice ==&lt;br /&gt;
&lt;br /&gt;
Tutto il codice di cui abbiamo bisogno starà in un file, &amp;lt;tt&amp;gt;main.cpp&amp;lt;/tt&amp;gt;. Crealo con il codice qua sotto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;KMessageBox&amp;gt;&lt;br /&gt;
#include &amp;lt;QByteArray&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&lt;br /&gt;
                         // Il nome del programma, usato internamente.&lt;br /&gt;
                         &amp;quot;tutorial1&amp;quot;,&lt;br /&gt;
                         // Il nome nel catalogo messaggi.&lt;br /&gt;
                         // Se nullo, verrà usato il nome del programma.&lt;br /&gt;
                         0,&lt;br /&gt;
                         // Una stringa con il nome del programma.&lt;br /&gt;
                         // Quella che verrà effettivamente visualizzata.&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 1&amp;quot;),&lt;br /&gt;
                         // La stringa con la versione del programma.&lt;br /&gt;
                         &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         // Piccola descrizione su cosa fa il programma.&lt;br /&gt;
                         ki18n(&amp;quot;Visualizza una finestra KMessageBox.&amp;quot;),&lt;br /&gt;
                         // La licenza con la quale il codice è rilasciato.&lt;br /&gt;
                         KAboutData::License_GPL,&lt;br /&gt;
                         // Copyright&lt;br /&gt;
                         ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         // Testo opzionale mostrato in &amp;quot;Informazioni su&amp;quot;.&lt;br /&gt;
                         // Può contenere qualunque informazione desiderata.&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         // La stringa con la homepage del programma.&lt;br /&gt;
                         QByteArray(&amp;quot;http://tutorial.com/&amp;quot;),&lt;br /&gt;
                         // L'indirizzo email per la segnalazione di bug.&lt;br /&gt;
                         QByteArray(&amp;quot;submit@bugs.kde.org&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
    KGuiItem yesButton( i18n( &amp;quot;Ciao&amp;quot; ), QString(),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un tooltip&amp;quot; ),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un testo di aiuto CheCos'è?.&amp;quot; ) );&lt;br /&gt;
    KMessageBox::questionYesNo( 0, i18n( &amp;quot;Ciao Mondo&amp;quot; ),&lt;br /&gt;
                                i18n( &amp;quot;Ciao&amp;quot; ), yesButton );&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Il primo codice specifico di KDE che incontriamo in questo programma è {{class|KAboutData}}. Questa è la classe utilizzata per immagazzinare le informazioni sul programma, come ad esempio una piccola descrizione, gli autori, o le informazioni sulla licenza. Praticamente ogni applicazione KDE dovrebbe usare questa classe.&lt;br /&gt;
&lt;br /&gt;
Poi arriviamo a {{class|KCmdLineArgs}}. Questa è la classe che si usa quando si vogliono specificare istruzioni da riga di comando per, ad esempio, aprire il programma con uno specifico file. Comunque, in questo tutorial, semplicemente lo inizializziamo con l'oggetto {{class|KAboutData}} che abbiamo appena creato, in modo da poter usare le opzioni &amp;lt;tt&amp;gt;--version&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;--author&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quindi adiamo a creare l'oggetto {{class|KApplication}}. Ciò bisogna farlo esattamente una volta in ogni programma, visto che è necessario per cose come l'[[Development/Tutorials/Localization/i18n|i18n]].&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo finito con le operazioni indispensabili di KDE, possiamo cominciare a fare cose interessanti con la nostra applicazione. Creeremo una finestra pop-up, ma personalizzeremo uno dei pulsanti. Per la personalizzazione avremo bisogno di un oggetto {{class|KGuiItem}}. Il primo argomento del costruttore di {{class|KGuiItem}} è il testo che apparirà sull'oggetto (nel nostro caso, un pulsante). Quindi ci sarà un opzione per inserire un'icona, ma non ne vogliamo una così gli mettiamo &amp;lt;tt&amp;gt;QString&amp;lt;/tt&amp;gt;. Impostiamo quindi il tooltip (quello che appare quando fermi il puntatore su un elemento) ed infine il testo &amp;quot;Che Cos'è?&amp;quot; (accessibile tramite click col tasto destro, con Shift-F1 oppure cliccando su &amp;quot;?&amp;quot; nella barra del titolo e poi sul pulsante &amp;quot;Ciao&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo il nostro pulsante, possiamo creare il nostro pop-up. Chiamiamo la funzione &amp;lt;tt&amp;gt;{{class|KMessageBox}}::questionYesNo()&amp;lt;/tt&amp;gt; la quale, in modo predefinito, crea una finestra con due pulsanti &amp;quot;Si&amp;quot; e &amp;quot;No&amp;quot;. Il secondo argomento compone il testo mostrato nella finestra sopra i due pulsanti. Il terzo è l'intestazione della finestra; e infine impostiamo il KGuiItem di (quello che normalmente è) il pulsante &amp;quot;Si&amp;quot; con il nostro &amp;lt;tt&amp;gt;KGuiItem yesButton&amp;lt;/tt&amp;gt; appena creato.&lt;br /&gt;
&lt;br /&gt;
Notare come tutto il testo visibile all'utente passa attraverso la funzione i18n(); questo è necessario per permettere la traduzione della UI. Più informazioni sulla localizzazione possono essere trovate nel [[Development/Tutorials/Localization/i18n|tutorial sulla localizzazione]].&lt;br /&gt;
&lt;br /&gt;
Abbiamo finito per quanto riguarda il codice. Ora la compilazione e la prova.&lt;br /&gt;
&lt;br /&gt;
== Compilazione ==&lt;br /&gt;
&lt;br /&gt;
Vorrai usare [[Development/Tutorials/CMake|CMake]] come ambiente di compilazione. Devi fornire un file chiamato CMakeLists.txt, cmake usa questo file per generare tutti i Makefiles.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
Crea un file di nome CMakeLists.txt nella stessa cartella di main.cpp con questo contenuto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
project (tutorial1)&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include (KDE4Defaults)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
set(tutorial1_SRCS main.cpp)&lt;br /&gt;
kde4_add_executable(tutorial1 ${tutorial1_SRCS})&lt;br /&gt;
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial1  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
La funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; trova tutti i pacchetti che hai richiesto (in questo caso KDE4) ed imposta alcune variabili che descrivono la posizione degli header e delle librerie del pacchetto. In questo caso useremo la variabile &amp;lt;tt&amp;gt;KDE4_INCLUDES&amp;lt;/tt&amp;gt; contenente il percorso ai files header di KDE4.&lt;br /&gt;
&lt;br /&gt;
Per permettere al compilatore di trovare questi files, passiamo quella variabile alla funzione &amp;lt;tt&amp;gt;include_directories()&amp;lt;/tt&amp;gt; la quale aggiunge gli header di KDE4 al percorso di ricerca degli headr.&lt;br /&gt;
&lt;br /&gt;
Poi creiamo una variabile chiamata &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; con la funzione &amp;lt;tt&amp;gt;set()&amp;lt;/tt&amp;gt;. In questo caso la impostiamo con il nome del nostro unico file sorgente.&lt;br /&gt;
&lt;br /&gt;
Quindi usiamo &amp;lt;tt&amp;gt;kde4_add_executable()&amp;lt;/tt&amp;gt; per creare un eseguibile chiamato &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; dai sorgenti elencati nella nostra variabile &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt;. In seguito, facciamo il link del nostro eseguibile alle librerie kdeui di KDE4 con &amp;lt;tt&amp;gt;target_link_libraries()&amp;lt;/tt&amp;gt; e la variabile &amp;lt;tt&amp;gt;KDE4_KDEUI_LIBS&amp;lt;/tt&amp;gt; precedentemente riempita dalla funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt;. La riga che comincia con &amp;lt;tt&amp;gt;install&amp;lt;/tt&amp;gt; scrive un target di default &amp;quot;install&amp;quot; dentro al Makefile.&lt;br /&gt;
&lt;br /&gt;
=== Make ed Esecuzione ===&lt;br /&gt;
&lt;br /&gt;
Puoi invocare CMake e make manualmente:&lt;br /&gt;
&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake .. # Nota questi due punti - non è una mancanza,&lt;br /&gt;
          # stanno per &amp;quot;cartella superiore&amp;quot;.&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
Oppure, se hai preparato il tuo ambiente come descritto in [[Getting_Started/Build/KDE4|Getting Started/Build/KDE4]], puoi compilare questo codice con:&lt;br /&gt;
 cmakekde&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Quindi lancialo con:&lt;br /&gt;
 ./tutorial1&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KXmlGuiWindow|usare KXmlGuiWindow]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/First_program_(it)</id>
		<title>Development/Tutorials/First program (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/First_program_(it)"/>
				<updated>2008-11-28T11:42:08Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Sistemati i codici + altri dettagli&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/First_program}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Hello World|&lt;br /&gt;
&lt;br /&gt;
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KXmlGuiWindow|Tutorial 2 - KXmlGuiWindow]]| &lt;br /&gt;
&lt;br /&gt;
reading=[[Development/Tutorials/CMake|CMake]]&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Il tuo primo programma dovrebbe salutare il mondo con un amichevole &amp;quot;Ciao Mondo&amp;quot;, giusto? Per farlo, useremo un {{class|KMessageBox}} personalizzando uno dei pulsanti.&lt;br /&gt;
&lt;br /&gt;
[[image:Ciaomondo_tutorial1.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
{{tip|Per avere più informazioni su qualunque classe che incontri, Konqueror offre una veloce scorciatoia. Per cercare informazioni circa KMessageBox, basta digitare &amp;quot;kde:kmessagebox&amp;quot; in Konqueror e sarai portato alla documentazione.}}&lt;br /&gt;
&lt;br /&gt;
{{tip|Vorresti poter usare KDevelop per i tuoi progetti, il quale fa molte cose carine come il completamento del codice, facile accesso alla documentazione delle API, supporto per il debugging.&lt;br /&gt;
Leggi [[Getting_Started/Set_up_KDE_4_for_development#KDevelop|questo tutorial]] per configurare KDevelop correttamente. Puoi controllare se il setup è andato a buon fine aprendo un'applicazione KDE4 esistente con KDevelop.&lt;br /&gt;
Tuttavia hai ancora bisogno di editare a mano i files di CMake.}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Il Codice ==&lt;br /&gt;
&lt;br /&gt;
Tutto il codice di cui abbiamo bisogno starà in un file, &amp;lt;tt&amp;gt;main.cpp&amp;lt;/tt&amp;gt;. Crealo con il codice qua sotto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;KMessageBox&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&lt;br /&gt;
                         // Il nome del programma, usato internamente.&lt;br /&gt;
                         &amp;quot;tutorial1&amp;quot;,&lt;br /&gt;
                         // Il nome nel catalogo messaggi.&lt;br /&gt;
                         // Se nullo, verrà usato il nome del programma.&lt;br /&gt;
                         0,&lt;br /&gt;
                         // Una stringa con il nome del programma.&lt;br /&gt;
                         // Quella che verrà effettivamente visualizzata.&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 1&amp;quot;),&lt;br /&gt;
                         // La stringa con la versione del programma.&lt;br /&gt;
                         &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         // Piccola descrizione su cosa fa il programma.&lt;br /&gt;
                         ki18n(&amp;quot;Visualizza una finestra KMessageBox.&amp;quot;),&lt;br /&gt;
                         // La licenza con la quale il codice è rilasciato.&lt;br /&gt;
                         KAboutData::License_GPL,&lt;br /&gt;
                         // Copyright&lt;br /&gt;
                         ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         // Testo opzionale mostrato in &amp;quot;Informazioni su&amp;quot;.&lt;br /&gt;
                         // Può contenere qualunque informazione desiderata.&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         // La stringa con la homepage del programma.&lt;br /&gt;
                         &amp;quot;http://tutorial.com/&amp;quot;,&lt;br /&gt;
                         // L'indirizzo email per la segnalazione di bug.&lt;br /&gt;
                         &amp;quot;submit@bugs.kde.org&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
    KGuiItem yesButton( i18n( &amp;quot;Ciao&amp;quot; ), QString(),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un tooltip&amp;quot; ),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un testo di aiuto CheCos'è?.&amp;quot; ) );&lt;br /&gt;
    KMessageBox::questionYesNo( 0, i18n( &amp;quot;Ciao Mondo&amp;quot; ),&lt;br /&gt;
                                i18n( &amp;quot;Ciao&amp;quot; ), yesButton );&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Il primo codice specifico di KDE che incontriamo in questo programma è {{class|KAboutData}}. Questa è la classe utilizzata per immagazzinare le informazioni sul programma, come ad esempio una piccola descrizione, gli autori, o le informazioni sulla licenza. Praticamente ogni applicazione KDE dovrebbe usare questa classe.&lt;br /&gt;
&lt;br /&gt;
Poi arriviamo a {{class|KCmdLineArgs}}. Questa è la classe che si usa quando si vogliono specificare istruzioni da riga di comando per, ad esempio, aprire il programma con uno specifico file. Comunque, in questo tutorial, semplicemente lo inizializziamo con l'oggetto {{class|KAboutData}} che abbiamo appena creato, in modo da poter usare le opzioni &amp;lt;tt&amp;gt;--version&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;--author&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quindi adiamo a creare l'oggetto {{class|KApplication}}. Ciò bisogna farlo esattamente una volta in ogni programma, visto che è necessario per cose come l'[[Development/Tutorials/Localization/i18n|i18n]].&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo finito con le operazioni indispensabili di KDE, possiamo cominciare a fare cose interessanti con la nostra applicazione. Creeremo una finestra pop-up, ma personalizzeremo uno dei pulsanti. Per la personalizzazione avremo bisogno di un oggetto {{class|KGuiItem}}. Il primo argomento del costruttore di {{class|KGuiItem}} è il testo che apparirà sull'oggetto (nel nostro caso, un pulsante). Quindi ci sarà un opzione per inserire un'icona, ma non ne vogliamo una così gli mettiamo &amp;lt;tt&amp;gt;QString&amp;lt;/tt&amp;gt;. Impostiamo quindi il tooltip (quello che appare quando fermi il puntatore su un elemento) ed infine il testo &amp;quot;Che Cos'è?&amp;quot; (accessibile tramite click col tasto destro, con Shift-F1 oppure cliccando su &amp;quot;?&amp;quot; nella barra del titolo e poi sul pulsante &amp;quot;Ciao&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo il nostro pulsante, possiamo creare il nostro pop-up. Chiamiamo la funzione &amp;lt;tt&amp;gt;{{class|KMessageBox}}::questionYesNo()&amp;lt;/tt&amp;gt; la quale, in modo predefinito, crea una finestra con due pulsanti &amp;quot;Si&amp;quot; e &amp;quot;No&amp;quot;. Il secondo argomento compone il testo mostrato nella finestra sopra i due pulsanti. Il terzo è l'intestazione della finestra; e infine impostiamo il KGuiItem di (quello che normalmente è) il pulsante &amp;quot;Si&amp;quot; con il nostro &amp;lt;tt&amp;gt;KGuiItem yesButton&amp;lt;/tt&amp;gt; appena creato.&lt;br /&gt;
&lt;br /&gt;
Notare come tutto il testo visibile all'utente passa attraverso la funzione i18n(); questo è necessario per permettere la traduzione della UI. Più informazioni sulla localizzazione possono essere trovate nel [[Development/Tutorials/Localization/i18n|tutorial sulla localizzazione]].&lt;br /&gt;
&lt;br /&gt;
Abbiamo finito per quanto riguarda il codice. Ora la compilazione e la prova.&lt;br /&gt;
&lt;br /&gt;
== Compilazione ==&lt;br /&gt;
&lt;br /&gt;
Vorrai usare [[Development/Tutorials/CMake|CMake]] come ambiente di compilazione. Devi fornire un file chiamato CMakeLists.txt, cmake usa questo file per generare tutti i Makefiles.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
Crea un file di nome CMakeLists.txt nella stessa cartella di main.cpp con questo contenuto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
project (tutorial1)&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include (KDE4Defaults)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
set(tutorial1_SRCS main.cpp)&lt;br /&gt;
kde4_add_executable(tutorial1 ${tutorial1_SRCS})&lt;br /&gt;
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial1  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
La funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; trova tutti i pacchetti che hai richiesto (in questo caso KDE4) ed imposta alcune variabili che descrivono la posizione degli header e delle librerie del pacchetto. In questo caso useremo la variabile &amp;lt;tt&amp;gt;KDE4_INCLUDES&amp;lt;/tt&amp;gt; contenente il percorso ai files header di KDE4.&lt;br /&gt;
&lt;br /&gt;
Per permettere al compilatore di trovare questi files, passiamo quella variabile alla funzione &amp;lt;tt&amp;gt;include_directories()&amp;lt;/tt&amp;gt; la quale aggiunge gli header di KDE4 al percorso di ricerca degli headr.&lt;br /&gt;
&lt;br /&gt;
Poi creiamo una variabile chiamata &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; con la funzione &amp;lt;tt&amp;gt;set()&amp;lt;/tt&amp;gt;. In questo caso la impostiamo con il nome del nostro unico file sorgente.&lt;br /&gt;
&lt;br /&gt;
Quindi usiamo &amp;lt;tt&amp;gt;kde4_add_executable()&amp;lt;/tt&amp;gt; per creare un eseguibile chiamato &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; dai sorgenti elencati nella nostra variabile &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt;. In seguito, facciamo il link del nostro eseguibile alle librerie kdeui di KDE4 con &amp;lt;tt&amp;gt;target_link_libraries()&amp;lt;/tt&amp;gt; e la variabile &amp;lt;tt&amp;gt;KDE4_KDEUI_LIBS&amp;lt;/tt&amp;gt; precedentemente riempita dalla funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt;. La riga che comincia con &amp;lt;tt&amp;gt;install&amp;lt;/tt&amp;gt; scrive un target di default &amp;quot;install&amp;quot; dentro al Makefile.&lt;br /&gt;
&lt;br /&gt;
=== Make ed Esecuzione ===&lt;br /&gt;
&lt;br /&gt;
Puoi invocare CMake e make manualmente:&lt;br /&gt;
&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake .. # Nota questi due punti - non è una mancanza,&lt;br /&gt;
          # stanno per &amp;quot;cartella superiore&amp;quot;.&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
Oppure, se hai preparato il tuo ambiente come descritto in [[Getting_Started/Build/KDE4|Getting Started/Build/KDE4]], puoi compilare questo codice con:&lt;br /&gt;
 cmakekde&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Quindi lancialo con:&lt;br /&gt;
 ./tutorial1&lt;br /&gt;
&lt;br /&gt;
== Continua ==&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KXmlGuiWindow|usare KXmlGuiWindow]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/First_program_(it)</id>
		<title>Development/Tutorials/First program (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/First_program_(it)"/>
				<updated>2008-11-28T11:35:33Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Traduzione completa&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/First_program}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Hello World|&lt;br /&gt;
&lt;br /&gt;
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KXmlGuiWindow|Tutorial 2 - KXmlGuiWindow]]| &lt;br /&gt;
&lt;br /&gt;
reading=[[Development/Tutorials/CMake|CMake]]&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Il tuo primo programma dovrebbe salutare il mondo con un amichevole &amp;quot;Ciao Mondo&amp;quot;, giusto? Per farlo, useremo un {{class|KMessageBox}} personalizzando uno dei pulsanti.&lt;br /&gt;
[[image:Ciaomondo_tutorial1.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
{{tip|Per avere più informazioni su qualunque classe che incontri, Konqueror offre una veloce scorciatoia. Per cercare informazioni circa KMessageBox, basta digitare &amp;quot;kde:kmessagebox&amp;quot; in Konqueror e sarai portato alla documentazione.}}&lt;br /&gt;
&lt;br /&gt;
{{tip|Vorresti poter usare KDevelop per i tuoi progetti, il quale fa molte cose carine come il completamento del codice, facile accesso alla documentazione delle API, supporto per il debugging.&lt;br /&gt;
Leggi [[Getting_Started/Set_up_KDE_4_for_development#KDevelop|questo tutorial]] per configurare KDevelop correttamente. Puoi controllare se il setup è andato a buon fine aprendo un'applicazione KDE4 esistente con KDevelop.&lt;br /&gt;
Tuttavia hai ancora bisogno di editare a mano i files di CMake.}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Il Codice ==&lt;br /&gt;
&lt;br /&gt;
Tutto il codice di cui abbiamo bisogno starà in un file, &amp;lt;tt&amp;gt;main.cpp&amp;lt;/tt&amp;gt;. Crealo con il codice qua sotto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;KMessageBox&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&lt;br /&gt;
                         // Il nome del programma, usato internamente.&lt;br /&gt;
                         &amp;quot;tutorial1&amp;quot;,&lt;br /&gt;
                         // Il nome nel catalogo messaggi.&lt;br /&gt;
                         // Se nullo, verrà usato il nome del programma.&lt;br /&gt;
                         0,&lt;br /&gt;
                         // Una stringa con il nome del programma visualizzabile.&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 1&amp;quot;),&lt;br /&gt;
                         // La stringa con la versione del programma.&lt;br /&gt;
                         &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         // Piccola descrizione su cosa fa il programma.&lt;br /&gt;
                         ki18n(&amp;quot;Visualizza una finestra KMessageBox.&amp;quot;),&lt;br /&gt;
                         // La licenza sotto la quale questo codice è rilasciato.&lt;br /&gt;
                         KAboutData::License_GPL,&lt;br /&gt;
                         // Copyright&lt;br /&gt;
                         ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         // Testo opzionale mostrato nella finestra &amp;quot;Informazioni su&amp;quot;.&lt;br /&gt;
                         // Può contenere qualunque informazione desiderata.&lt;br /&gt;
                         ki18n(&amp;quot;Un po' di testo...&amp;quot;),&lt;br /&gt;
                         // La stringa con la homepage del programma.&lt;br /&gt;
                         &amp;quot;http://tutorial.com/&amp;quot;,&lt;br /&gt;
                         // L'indirizzo email per la segnalazione di bug.&lt;br /&gt;
                         &amp;quot;submit@bugs.kde.org&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
    KGuiItem yesButton( i18n( &amp;quot;Ciao&amp;quot; ), QString(),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un tooltip&amp;quot; ),&lt;br /&gt;
                        i18n( &amp;quot;Questo è un testo di aiuto CheCose'.&amp;quot; ) );&lt;br /&gt;
    KMessageBox::questionYesNo( 0, i18n( &amp;quot;Ciao Mondo&amp;quot; ),&lt;br /&gt;
                                i18n( &amp;quot;Ciao&amp;quot; ), yesButton );&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Il primo codice specifico di KDE che incontriamo in questo programma è {{class|KAboutData}}. Questa è la classe utilizzata per immagazzinare le informazioni sul programma, come ad esempio una piccola descrizione, gli autori, o le informazioni sulla licenza. Praticamente ogni applicazione KDE dovrebbe usare questa classe.&lt;br /&gt;
&lt;br /&gt;
Poi arriviamo a {{class|KCmdLineArgs}}. Questa è la classe che si usa quando si vogliono specificare istruzioni da riga di comando per, ad esempio, aprire il programma con uno specifico file. Comunque, in questo tutorial, semplicemente lo inizializziamo con l'oggetto {{class|KAboutData}} che abbiamo appena creato, in modo da poter usare le opzioni &amp;lt;tt&amp;gt;--version&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;--author&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quindi adiamo a creare l'oggetto {{class|KApplication}}. Ciò bisogna farlo esattamente una volta in ogni programma, visto che è necessario per cose come l'[[Development/Tutorials/Localization/i18n|i18n]].&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo finito con le operazioni indispensabili di KDE, possiamo cominciare a fare cose interessanti con la nostra applicazione. Creeremo una finestra pop-up, ma personalizzeremo uno dei pulsanti. Per la personalizzazione avremo bisogno di un oggetto {{class|KGuiItem}}. Il primo argomento del costruttore di {{class|KGuiItem}} è il testo che apparirà sull'oggetto (nel nostro caso, un pulsante). Quindi ci sarà un opzione per inserire un'icona, ma non ne vogliamo una così gli mettiamo &amp;lt;tt&amp;gt;QString&amp;lt;/tt&amp;gt;. Impostiamo quindi il tooltip (quello che appare quando fermi il puntatore su un elemento) ed infine il testo &amp;quot;Che Cos'è?&amp;quot; (accessibile tramite click col tasto destro, con Shift-F1 oppure cliccando su &amp;quot;?&amp;quot; nella barra del titolo e poi sul pulsante &amp;quot;Ciao&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ora che abbiamo il nostro pulsante, possiamo creare il nostro pop-up. Chiamiamo la funzione &amp;lt;tt&amp;gt;{{class|KMessageBox}}::questionYesNo()&amp;lt;/tt&amp;gt; la quale, in modo predefinito, crea una finestra con due pulsanti &amp;quot;Si&amp;quot; e &amp;quot;No&amp;quot;. Il secondo argomento compone il testo mostrato nella finestra sopra i due pulsanti. Il terzo è l'intestazione della finestra; e infine impostiamo il KGuiItem di (quello che normalmente è) il pulsante &amp;quot;Si&amp;quot; con il nostro &amp;lt;tt&amp;gt;KGuiItem yesButton&amp;lt;/tt&amp;gt; appena creato.&lt;br /&gt;
&lt;br /&gt;
Notare come tutto il testo visibile all'utente passa attraverso la funzione i18n(); questo è necessario per permettere la traduzione della UI. Più informazioni sulla localizzazione possono essere trovate nel [[Development/Tutorials/Localization/i18n|tutorial sulla localizzazione]].&lt;br /&gt;
&lt;br /&gt;
Abbiamo finito per quanto riguarda il codice. Ora la compilazione e la prova.&lt;br /&gt;
&lt;br /&gt;
== Compilazione ==&lt;br /&gt;
&lt;br /&gt;
Vorrai usare [[Development/Tutorials/CMake|CMake]] come ambiente di compilazione. Devi fornire un file chiamato CMakeLists.txt, cmake usa questo file per generare tutti i Makefiles.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
&lt;br /&gt;
Crea un file di nome CMakeLists.txt nella stessa cartella di main.cpp con questo contenuto:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
project (tutorial1)&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include (KDE4Defaults)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
set(tutorial1_SRCS main.cpp)&lt;br /&gt;
kde4_add_executable(tutorial1 ${tutorial1_SRCS})&lt;br /&gt;
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial1  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
La funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; trova tutti i pacchetti che hai richiesto (in questo caso KDE4) ed imposta alcune variabili che descrivono la posizione degli header e delle librerie del pacchetto. In questo caso useremo la variabile &amp;lt;tt&amp;gt;KDE4_INCLUDES&amp;lt;/tt&amp;gt; contenente il percorso ai files header di KDE4.&lt;br /&gt;
&lt;br /&gt;
Per permettere al compilatore di trovare questi files, passiamo quella variabile alla funzione &amp;lt;tt&amp;gt;include_directories()&amp;lt;/tt&amp;gt; la quale aggiunge gli header di KDE4 al percorso di ricerca degli headr.&lt;br /&gt;
&lt;br /&gt;
Poi creiamo una variabile chiamata &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; con la funzione &amp;lt;tt&amp;gt;set()&amp;lt;/tt&amp;gt;. In questo caso la impostiamo con il nome del nostro unico file sorgente.&lt;br /&gt;
&lt;br /&gt;
Quindi usiamo &amp;lt;tt&amp;gt;kde4_add_executable()&amp;lt;/tt&amp;gt; per creare un eseguibile chiamato &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; dai sorgenti elencati nella nostra variabile &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt;. In seguito, facciamo il link del nostro eseguibile alle librerie kdeui di KDE4 con &amp;lt;tt&amp;gt;target_link_libraries()&amp;lt;/tt&amp;gt; e la variabile &amp;lt;tt&amp;gt;KDE4_KDEUI_LIBS&amp;lt;/tt&amp;gt; precedentemente riempita dalla funzione &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt;. La riga che comincia con &amp;lt;tt&amp;gt;install&amp;lt;/tt&amp;gt; scrive un target di default &amp;quot;install&amp;quot; dentro al Makefile.&lt;br /&gt;
&lt;br /&gt;
=== Make ed Esecuzione ===&lt;br /&gt;
&lt;br /&gt;
Puoi invocare CMake e make manualmente:&lt;br /&gt;
&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake .. # Nota questi due punti - non è una mancanza, ma sta per &amp;quot;cartella superiore&amp;quot;.&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
Oppure, se hai preparato il tuo ambiente come descritto in [[Getting_Started/Build/KDE4|Getting Started/Build/KDE4]], puoi compilare questo codice con:&lt;br /&gt;
 cmakekde&lt;br /&gt;
&lt;br /&gt;
Quindi lancialo con:&lt;br /&gt;
 ./tutorial1&lt;br /&gt;
&lt;br /&gt;
== Continuare ==&lt;br /&gt;
&lt;br /&gt;
Now you can move on to [[Development/Tutorials/Using_KXmlGuiWindow|using KXmlGuiWindow]].&lt;br /&gt;
&lt;br /&gt;
Ora puoi continuare verso [[Development/Tutorials/Using_KXmlGuiWindow|usare KXmlGuiWindow]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/First_program_(it)</id>
		<title>Development/Tutorials/First program (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/First_program_(it)"/>
				<updated>2008-11-28T10:23:22Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Prefazione + Immagine&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/First_program}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Tutorial per Principianti|&lt;br /&gt;
&lt;br /&gt;
name=Hello World|&lt;br /&gt;
&lt;br /&gt;
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KXmlGuiWindow|Tutorial 2 - KXmlGuiWindow]]| &lt;br /&gt;
&lt;br /&gt;
reading=[[Development/Tutorials/CMake|CMake]]&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Il tuo primo programma dovrebbe salutare il mondo con un amichevole &amp;quot;Ciao Mondo&amp;quot;, giusto? Per farlo, useremo un {{class|KMessageBox}} personalizzando uno dei pulsanti.&lt;br /&gt;
[[image:Ciaomondo_tutorial1.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
{{tip|To get more information about any class you come across, Konqueror offers a quick shortcut. So to look for information about KMessageBox, just type &amp;quot;kde:kmessagebox&amp;quot; into Konqueror and you'll be taken to the documentation.}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{tip|&lt;br /&gt;
You might want to use KDevelop for your projects, which does many nice things like code completion, easy access to API documentation or debugging support.&lt;br /&gt;
&lt;br /&gt;
Read [[Getting_Started/Set_up_KDE_4_for_development#KDevelop|this tutorial]] to set up KDevelop correctly for this task. You probably want to check if the setup is working by testing opening an existing KDE 4 application with KDevelop first.&lt;br /&gt;
&lt;br /&gt;
You still need to edit the CMake files by hand though.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==The Code==&lt;br /&gt;
All the code we need will be in one file, &amp;lt;tt&amp;gt;main.cpp&amp;lt;/tt&amp;gt;. Create that file with the code below:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;KMessageBox&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&lt;br /&gt;
                         // The program name used internally.&lt;br /&gt;
                         &amp;quot;tutorial1&amp;quot;,&lt;br /&gt;
                         // The message catalog name&lt;br /&gt;
                         // If null, program name is used instead.&lt;br /&gt;
                         0,&lt;br /&gt;
                         // A displayable program name string.&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 1&amp;quot;),&lt;br /&gt;
                         // The program version string.&lt;br /&gt;
                         &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         // Short description of what the app does.&lt;br /&gt;
                         ki18n(&amp;quot;Displays a KMessageBox popup&amp;quot;),&lt;br /&gt;
                         // The license this code is released under&lt;br /&gt;
                         KAboutData::License_GPL,&lt;br /&gt;
                         // Copyright Statement&lt;br /&gt;
                         ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         // Optional text shown in the About box.&lt;br /&gt;
                         // Can contain any information desired.&lt;br /&gt;
                         ki18n(&amp;quot;Some text...&amp;quot;),&lt;br /&gt;
                         // The program homepage string.&lt;br /&gt;
                         &amp;quot;http://tutorial.com/&amp;quot;,&lt;br /&gt;
                         // The bug report email address&lt;br /&gt;
                         &amp;quot;submit@bugs.kde.org&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
    KGuiItem yesButton( i18n( &amp;quot;Hello&amp;quot; ), QString(),&lt;br /&gt;
                        i18n( &amp;quot;This is a tooltip&amp;quot; ),&lt;br /&gt;
                        i18n( &amp;quot;This is a WhatsThis help text.&amp;quot; ) );&lt;br /&gt;
    KMessageBox::questionYesNo( 0, i18n( &amp;quot;Hello World&amp;quot; ),&lt;br /&gt;
                                i18n( &amp;quot;Hello&amp;quot; ), yesButton );&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
The first KDE specific code we come across in this program is {{class|KAboutData}}. This is the class used to store information about the program such as a short description, authors or license information. Pretty much every KDE application should use this class.&lt;br /&gt;
&lt;br /&gt;
Then we come to {{class|KCmdLineArgs}}. This is the class one would use to specify command line switches to, for example, open the program with a specific file. However, in this tutorial, we simply initialise it with the {{class|KAboutData}} object we created so we can use the &amp;lt;tt&amp;gt;--version&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;--author&amp;lt;/tt&amp;gt; switches.&lt;br /&gt;
&lt;br /&gt;
Then we create a {{class|KApplication}} object. This needs to be done exactly once in each program since it is needed for things such as [[Development/Tutorials/Localization/i18n|i18n]].&lt;br /&gt;
&lt;br /&gt;
Now we've done all the necessary KDE setup, we can move on to doing interesting things with our application. We're going to create a popup box but we're going to customise one of the buttons. To do this customisation, we need to use a {{class|KGuiItem}} object. The first argument in the {{class|KGuiItem}} constructor is the text that will appear on the item (in our case, a button). Then we have an option of setting an icon for the button but we don't want one so we just give it &amp;lt;tt&amp;gt;QString()&amp;lt;/tt&amp;gt;. We then set the tooltip (what appears when you hover over an item) and finally the &amp;quot;What's This?&amp;quot; (accessed through right-clicking or Shift-F1) text.&lt;br /&gt;
&lt;br /&gt;
Now we have our item, we can create our popup. We call the &amp;lt;tt&amp;gt;{{class|KMessageBox}}::questionYesNo()&amp;lt;/tt&amp;gt; function which, by default, creates a message box with a &amp;quot;Yes&amp;quot; and a &amp;quot;No&amp;quot; button. The second argument is the text that will appear in the message box above the buttons. The third is the caption the window will have and finally we set the KGuiItem for (what would normally be) the &amp;quot;Yes&amp;quot; button to the &amp;lt;tt&amp;gt;KGuiItem guiItem&amp;lt;/tt&amp;gt; we created.&lt;br /&gt;
&lt;br /&gt;
Note that all user-visible text is passed through the i18n() function; this is necessary for the UI to be translatable. More information on localization can be found in the [[Development/Tutorials/Localization/i18n|localization tutorial]].&lt;br /&gt;
&lt;br /&gt;
We're all done as far as the code is concerned. Now to build it and try it out.&lt;br /&gt;
&lt;br /&gt;
== Build ==&lt;br /&gt;
You want to [[Development/Tutorials/CMake|use CMake]] for your build environment. You provide a file CMakeLists.txt, cmake uses this file to generate all Makefiles out of it.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
Create a file named CMakeLists.txt in the same directory as main.cpp with this content:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
project (tutorial1)&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include (KDE4Defaults)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
set(tutorial1_SRCS main.cpp)&lt;br /&gt;
kde4_add_executable(tutorial1 ${tutorial1_SRCS})&lt;br /&gt;
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial1  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; function locates the package that you ask it for (in this case KDE4) and sets some variables describing the location of the package's headers and libraries. In this case we will use the &amp;lt;tt&amp;gt;KDE4_INCLUDES&amp;lt;/tt&amp;gt; variable which contains the path to the KDE4 header files.&lt;br /&gt;
&lt;br /&gt;
In order to allow the compiler to find these files, we pass that variable to the &amp;lt;tt&amp;gt;include_directories()&amp;lt;/tt&amp;gt; function which adds the KDE4 headers to the header search path.&lt;br /&gt;
&lt;br /&gt;
Next we create a variable called &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; using the &amp;lt;tt&amp;gt;set()&amp;lt;/tt&amp;gt; function. In this case we simply set it to the name of our only source file.&lt;br /&gt;
&lt;br /&gt;
Then we use &amp;lt;tt&amp;gt;kde4_add_executable()&amp;lt;/tt&amp;gt; to create an executable called &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; from the source files listed in our &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; variable. Afterwards, we link our executable to the KDE4 kdeui library using &amp;lt;tt&amp;gt;target_link_libraries()&amp;lt;/tt&amp;gt; and the &amp;lt;tt&amp;gt;KDE4_KDEUI_LIBS&amp;lt;/tt&amp;gt; variable which was set by the &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; function. The line starting with &amp;lt;tt&amp;gt;install&amp;lt;/tt&amp;gt; writes a default &amp;quot;install&amp;quot; target into the Makefile.&lt;br /&gt;
&lt;br /&gt;
=== Make And Run ===&lt;br /&gt;
You can invoke CMake and make manually:&lt;br /&gt;
&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake .. # Note the two dots - this is no ellipsis, but &amp;quot;parent directory&amp;quot;.&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
Or, if you set up your environment as described in [[Getting_Started/Build/KDE4|Getting Started/Build/KDE4]], you can compile this code with:&lt;br /&gt;
 cmakekde&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And launch it with:&lt;br /&gt;
 ./tutorial1&lt;br /&gt;
&lt;br /&gt;
==Moving On==&lt;br /&gt;
Now you can move on to [[Development/Tutorials/Using_KXmlGuiWindow|using KXmlGuiWindow]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/File:Ciaomondo_tutorial1.png</id>
		<title>File:Ciaomondo tutorial1.png</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/File:Ciaomondo_tutorial1.png"/>
				<updated>2008-11-28T10:22:03Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/First_program_(it)</id>
		<title>Development/Tutorials/First program (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/First_program_(it)"/>
				<updated>2008-11-28T10:02:26Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Inizio -&amp;gt; creazione + tutorial browser in italiano&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/First_program}}&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Beginner Tutorial|&lt;br /&gt;
&lt;br /&gt;
name=Hello World|&lt;br /&gt;
&lt;br /&gt;
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]]|&lt;br /&gt;
&lt;br /&gt;
next=[[Development/Tutorials/Using_KXmlGuiWindow|Tutorial 2 - KXmlGuiWindow]]| &lt;br /&gt;
&lt;br /&gt;
reading=[[Development/Tutorials/CMake|CMake]]&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Abstract==&lt;br /&gt;
Your first program shall greet the world with a friendly &amp;quot;Hello World&amp;quot;, what else? For that, we will use a {{class|KMessageBox}} and customise one of the buttons.&lt;br /&gt;
[[image:introtokdetutorial1.png|frame|center]]&lt;br /&gt;
&lt;br /&gt;
{{tip|To get more information about any class you come across, Konqueror offers a quick shortcut. So to look for information about KMessageBox, just type &amp;quot;kde:kmessagebox&amp;quot; into Konqueror and you'll be taken to the documentation.}}&lt;br /&gt;
&lt;br /&gt;
{{tip|&lt;br /&gt;
You might want to use KDevelop for your projects, which does many nice things like code completion, easy access to API documentation or debugging support.&lt;br /&gt;
&lt;br /&gt;
Read [[Getting_Started/Set_up_KDE_4_for_development#KDevelop|this tutorial]] to set up KDevelop correctly for this task. You probably want to check if the setup is working by testing opening an existing KDE 4 application with KDevelop first.&lt;br /&gt;
&lt;br /&gt;
You still need to edit the CMake files by hand though.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==The Code==&lt;br /&gt;
All the code we need will be in one file, &amp;lt;tt&amp;gt;main.cpp&amp;lt;/tt&amp;gt;. Create that file with the code below:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;KApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;KAboutData&amp;gt;&lt;br /&gt;
#include &amp;lt;KCmdLineArgs&amp;gt;&lt;br /&gt;
#include &amp;lt;KMessageBox&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    KAboutData aboutData(&lt;br /&gt;
                         // The program name used internally.&lt;br /&gt;
                         &amp;quot;tutorial1&amp;quot;,&lt;br /&gt;
                         // The message catalog name&lt;br /&gt;
                         // If null, program name is used instead.&lt;br /&gt;
                         0,&lt;br /&gt;
                         // A displayable program name string.&lt;br /&gt;
                         ki18n(&amp;quot;Tutorial 1&amp;quot;),&lt;br /&gt;
                         // The program version string.&lt;br /&gt;
                         &amp;quot;1.0&amp;quot;,&lt;br /&gt;
                         // Short description of what the app does.&lt;br /&gt;
                         ki18n(&amp;quot;Displays a KMessageBox popup&amp;quot;),&lt;br /&gt;
                         // The license this code is released under&lt;br /&gt;
                         KAboutData::License_GPL,&lt;br /&gt;
                         // Copyright Statement&lt;br /&gt;
                         ki18n(&amp;quot;(c) 2007&amp;quot;),&lt;br /&gt;
                         // Optional text shown in the About box.&lt;br /&gt;
                         // Can contain any information desired.&lt;br /&gt;
                         ki18n(&amp;quot;Some text...&amp;quot;),&lt;br /&gt;
                         // The program homepage string.&lt;br /&gt;
                         &amp;quot;http://tutorial.com/&amp;quot;,&lt;br /&gt;
                         // The bug report email address&lt;br /&gt;
                         &amp;quot;submit@bugs.kde.org&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs::init( argc, argv, &amp;amp;aboutData );&lt;br /&gt;
    KApplication app;&lt;br /&gt;
    KGuiItem yesButton( i18n( &amp;quot;Hello&amp;quot; ), QString(),&lt;br /&gt;
                        i18n( &amp;quot;This is a tooltip&amp;quot; ),&lt;br /&gt;
                        i18n( &amp;quot;This is a WhatsThis help text.&amp;quot; ) );&lt;br /&gt;
    KMessageBox::questionYesNo( 0, i18n( &amp;quot;Hello World&amp;quot; ),&lt;br /&gt;
                                i18n( &amp;quot;Hello&amp;quot; ), yesButton );&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
The first KDE specific code we come across in this program is {{class|KAboutData}}. This is the class used to store information about the program such as a short description, authors or license information. Pretty much every KDE application should use this class.&lt;br /&gt;
&lt;br /&gt;
Then we come to {{class|KCmdLineArgs}}. This is the class one would use to specify command line switches to, for example, open the program with a specific file. However, in this tutorial, we simply initialise it with the {{class|KAboutData}} object we created so we can use the &amp;lt;tt&amp;gt;--version&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;--author&amp;lt;/tt&amp;gt; switches.&lt;br /&gt;
&lt;br /&gt;
Then we create a {{class|KApplication}} object. This needs to be done exactly once in each program since it is needed for things such as [[Development/Tutorials/Localization/i18n|i18n]].&lt;br /&gt;
&lt;br /&gt;
Now we've done all the necessary KDE setup, we can move on to doing interesting things with our application. We're going to create a popup box but we're going to customise one of the buttons. To do this customisation, we need to use a {{class|KGuiItem}} object. The first argument in the {{class|KGuiItem}} constructor is the text that will appear on the item (in our case, a button). Then we have an option of setting an icon for the button but we don't want one so we just give it &amp;lt;tt&amp;gt;QString()&amp;lt;/tt&amp;gt;. We then set the tooltip (what appears when you hover over an item) and finally the &amp;quot;What's This?&amp;quot; (accessed through right-clicking or Shift-F1) text.&lt;br /&gt;
&lt;br /&gt;
Now we have our item, we can create our popup. We call the &amp;lt;tt&amp;gt;{{class|KMessageBox}}::questionYesNo()&amp;lt;/tt&amp;gt; function which, by default, creates a message box with a &amp;quot;Yes&amp;quot; and a &amp;quot;No&amp;quot; button. The second argument is the text that will appear in the message box above the buttons. The third is the caption the window will have and finally we set the KGuiItem for (what would normally be) the &amp;quot;Yes&amp;quot; button to the &amp;lt;tt&amp;gt;KGuiItem guiItem&amp;lt;/tt&amp;gt; we created.&lt;br /&gt;
&lt;br /&gt;
Note that all user-visible text is passed through the i18n() function; this is necessary for the UI to be translatable. More information on localization can be found in the [[Development/Tutorials/Localization/i18n|localization tutorial]].&lt;br /&gt;
&lt;br /&gt;
We're all done as far as the code is concerned. Now to build it and try it out.&lt;br /&gt;
&lt;br /&gt;
== Build ==&lt;br /&gt;
You want to [[Development/Tutorials/CMake|use CMake]] for your build environment. You provide a file CMakeLists.txt, cmake uses this file to generate all Makefiles out of it.&lt;br /&gt;
&lt;br /&gt;
=== CMakeLists.txt ===&lt;br /&gt;
Create a file named CMakeLists.txt in the same directory as main.cpp with this content:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
project (tutorial1)&lt;br /&gt;
find_package(KDE4 REQUIRED)&lt;br /&gt;
include (KDE4Defaults)&lt;br /&gt;
include_directories(${KDE4_INCLUDES})&lt;br /&gt;
set(tutorial1_SRCS main.cpp)&lt;br /&gt;
kde4_add_executable(tutorial1 ${tutorial1_SRCS})&lt;br /&gt;
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})&lt;br /&gt;
install(TARGETS tutorial1  ${INSTALL_TARGETS_DEFAULT_ARGS})&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; function locates the package that you ask it for (in this case KDE4) and sets some variables describing the location of the package's headers and libraries. In this case we will use the &amp;lt;tt&amp;gt;KDE4_INCLUDES&amp;lt;/tt&amp;gt; variable which contains the path to the KDE4 header files.&lt;br /&gt;
&lt;br /&gt;
In order to allow the compiler to find these files, we pass that variable to the &amp;lt;tt&amp;gt;include_directories()&amp;lt;/tt&amp;gt; function which adds the KDE4 headers to the header search path.&lt;br /&gt;
&lt;br /&gt;
Next we create a variable called &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; using the &amp;lt;tt&amp;gt;set()&amp;lt;/tt&amp;gt; function. In this case we simply set it to the name of our only source file.&lt;br /&gt;
&lt;br /&gt;
Then we use &amp;lt;tt&amp;gt;kde4_add_executable()&amp;lt;/tt&amp;gt; to create an executable called &amp;lt;tt&amp;gt;tutorial1&amp;lt;/tt&amp;gt; from the source files listed in our &amp;lt;tt&amp;gt;tutorial1_SRCS&amp;lt;/tt&amp;gt; variable. Afterwards, we link our executable to the KDE4 kdeui library using &amp;lt;tt&amp;gt;target_link_libraries()&amp;lt;/tt&amp;gt; and the &amp;lt;tt&amp;gt;KDE4_KDEUI_LIBS&amp;lt;/tt&amp;gt; variable which was set by the &amp;lt;tt&amp;gt;find_package()&amp;lt;/tt&amp;gt; function. The line starting with &amp;lt;tt&amp;gt;install&amp;lt;/tt&amp;gt; writes a default &amp;quot;install&amp;quot; target into the Makefile.&lt;br /&gt;
&lt;br /&gt;
=== Make And Run ===&lt;br /&gt;
You can invoke CMake and make manually:&lt;br /&gt;
&lt;br /&gt;
 mkdir build &amp;amp;&amp;amp; cd build&lt;br /&gt;
 cmake .. # Note the two dots - this is no ellipsis, but &amp;quot;parent directory&amp;quot;.&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
Or, if you set up your environment as described in [[Getting_Started/Build/KDE4|Getting Started/Build/KDE4]], you can compile this code with:&lt;br /&gt;
 cmakekde&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And launch it with:&lt;br /&gt;
 ./tutorial1&lt;br /&gt;
&lt;br /&gt;
==Moving On==&lt;br /&gt;
Now you can move on to [[Development/Tutorials/Using_KXmlGuiWindow|using KXmlGuiWindow]].&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-28T10:01:16Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In order to learn how to develop cool KDE application I've start from the beginning: I aim to translate all the tutorial that I'm facing with.&lt;br /&gt;
&lt;br /&gt;
== Done ==&lt;br /&gt;
&lt;br /&gt;
* [[Template:TutorialBrowser_(it)]]&lt;br /&gt;
&lt;br /&gt;
== Work in Progress ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/First_program | Tutorial Hello World]]&lt;br /&gt;
&lt;br /&gt;
=== to recheck ===&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it) | Development/Tutorials/Common_Programming_Mistakes_(it)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Who's Next ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/Using_KXmlGuiWindow | Using KXmlGuiWindow]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-11-27T23:02:57Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere mal utilizzati o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;controllare il passaggio file-static&amp;lt;/font&amp;gt;&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;un namespace anonimo non ha linkage interno?&amp;lt;/font&amp;gt;&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile con qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferisci l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator assicurati di stare chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;; altrimenti, a meno che il tuo container sia esso stesso costante, potrebbero esserci detach non necessari del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
// codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
Preferire l'uso degli incrementi prefissi piuttosto di quelli postfissi negli iteratori così da evitare inutili creazioni di oggetti temporanei nel processo.&lt;br /&gt;
&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;approfondire timer e job&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== Falle nella memoria ===&lt;br /&gt;
&lt;br /&gt;
Un errore di programmazione molto &amp;quot;popolare&amp;quot; consiste nel fare un &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; senza un &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; come in questo programma:&lt;br /&gt;
&lt;br /&gt;
'''mem_buongustaio.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void inquina()&lt;br /&gt;
{&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) inquina();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come puoi vedere, ''inquina()'' istanzia un nuovo oggetto ''inquinatore'' di tipo ''t''. Quindi, la variabile ''inquinatore'' viene persa dato che è locale, ma il contenuto (l'oggetto) rimane nello heap. Posso usare questo programma per rendere il mio computer inutilizzabile in 10 secondi.&lt;br /&gt;
&lt;br /&gt;
Per risolvere, ci sono i seguenti approcci:&lt;br /&gt;
* tieni la variabile nello stack invece che nello heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
diventerà&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t inquinatore();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* cancella l'inquinatore usando la funzione complementare a new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete inquinatore;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uno strumento per individuare le falle di memoria come queste è [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
Puoi fare un dynamic_cast al tipo T dal tipo T2 tali che:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare e approfondire&amp;lt;/font&amp;gt;&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported --&amp;gt;&lt;br /&gt;
* T è definito in una libreria a cui fai il link (avrai un errore dal linker se non è così, dal momento che non troverà le informazioni vtable e RTTI)&lt;br /&gt;
* T è &amp;quot;ben-ancorato&amp;quot; in quella libreria. Con &amp;quot;ben-ancorato&amp;quot; intendo che vtable non è un simbolo COMUNE soggetto a fusioni a run-time da parte del linker dinamico. In altre parole, il primo membro virtuale nella definizione della classe deve esistere e non essere inline: deve essere in un file .cpp.&lt;br /&gt;
* T e T2 sono esportati.&lt;br /&gt;
&lt;br /&gt;
Per esempio, noi abbiamo incontrato qualche problema difficile da individuare nel codice C++ non KDE (NMM credo) a cui stavamo facendo il link:&lt;br /&gt;
* libphonon carica il plugin NMM&lt;br /&gt;
* il plugin NMM fa il link a NMM&lt;br /&gt;
* NMM carica i suoi plugins&lt;br /&gt;
* i plugins propri di NMM, fanno il link a NMM&lt;br /&gt;
&lt;br /&gt;
Qualche classe nella libreria di NMM non aveva vtables ben-ancorate, così il dynamic_casting falliva dentro al plugin NMM di Phonon per gli oggetti creatii nei plugins di NMM&lt;br /&gt;
&lt;br /&gt;
== Progettazione delle applicazioni ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione copriremo un po' di problemi comuni relativi alla progettazione di applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Inizializzazione ritardata ===&lt;br /&gt;
&lt;br /&gt;
Sebbene il design di moderne applicazioni C++ può essere molto complesso, un problema ricorrente, generalmente facile da sistemare, è il non usare la tecnica dell'[http://www.kdedevelopers.org/node/view/509 inizializzazione ritardata].&lt;br /&gt;
&lt;br /&gt;
Per prima cosa, diamo un'occhiata al modo standard di inizializzare un'applicazione KDE:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Nota che &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; viene creata prima di &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; il quale fa partire il ciclo degli eventi. Ciò implica che vogliamo evitare di fare cose non banali nella parte alta del costruttore, visto che verranno eseguite prima ancora che la finestra venga mostrata.&lt;br /&gt;
&lt;br /&gt;
La soluzione è semplice: abbiamo bisogno di ritardare la costruzione di qualunque cosa oltre alla GUI fino a che il ciclo degli eventi sia partito. Qui di seguito è mostrato come il costruttore della classe MainWindow dovrebbe essere per ottenere questo risultato:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Costruisci i tuoi widget qui. Nota che non devono&lt;br /&gt;
     * richiedere una complessa inizializzazione,&lt;br /&gt;
     * o verrà meno lo scopo di questa tecnica.&lt;br /&gt;
     * Tutto ciò che vorresti fare è creare i tuoi oggetti&lt;br /&gt;
     * della GUI ed usare QObject::connect per connettere&lt;br /&gt;
     * i segnali agli slots appropriati.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* Questo slot sarà chiamato non appena parte il ciclo eventi.&lt;br /&gt;
     * Metti tutto il resto che deve essere fatto, compresi&lt;br /&gt;
     * assegnazione di valori, lettura files, ristebilire sessioni, etc...&lt;br /&gt;
     * Tutto ciò prenderà lo stesso del tempo, ma almeno la tua&lt;br /&gt;
     * finestra sarà visibile a schermo, facendo apparire&lt;br /&gt;
     * attiva l'applicazione.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Usare questa tecnica potrebbe non far risparmiare del tempo in più, ma farà ''sembrare'' più veloce l'applicazione agli utenti che la eseguono. Questa percezione di reattività incrementata è rassicurante per l'utente il quale riceve un rapido feedback per il riuscito avvio dell'applicazione.&lt;br /&gt;
&lt;br /&gt;
Quando (e solo in questo caso) l'avvio non può essere reso ragionevolmente abbastanza veloce, considera l'uso di {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Strutture Dati ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione spazieremo su alcune delle nostre più comuni persecuzioni che affliggono le più comuni strutture dati viste nelle applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Passaggio di tipi non POD ===&lt;br /&gt;
&lt;br /&gt;
I tipi di dato diversi da POD ([http://en.wikipedia.org/wiki/Plain_old_data &amp;quot;Plain Old Data&amp;quot;], dati semplici senza la logica di controllo) dovrebbero essere passati sempre per riferimento costante. Questo include qualunque cosa tranne i tipi base come &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Prendi, per esempio, {{qt|QString}}. Dovrebbero sempre essere passati ai metodi come &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Anche se {{qt|QString}} è implicitamente condiviso è comunque più efficiente (e sicuro) passarlo per referenza costante piuttosto che come oggetto per valore.&lt;br /&gt;
&lt;br /&gt;
Quindi la dichiarazione canonica di un metodo che prende QString come argomento è:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void mioMetodo( const QString &amp;amp; x, const QString &amp;amp; y );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
Se avrai mai bisogno di eliminare una classe derivata da QObject dall'interno di uno dei suoi metodi, non farlo mai in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima o poi ciò causerà un crash perché un metodo di quell'oggetto potrebbe essere invocato dal ciclo eventi delle Qt via slots/signals dopo che tu l'hai eliminato.&lt;br /&gt;
&lt;br /&gt;
Invece usa sempre &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; il quale cerca di fare la stessa cosa di &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; ma in modo più sicuro.&lt;br /&gt;
&lt;br /&gt;
=== QStrings vuote ===&lt;br /&gt;
&lt;br /&gt;
Si è soliti voler verificare se un {{qt|QString}} è vuoto. Di seguito ci sono tre modi per farlo, dei quali i primi due sono corretti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sbagliato! &amp;quot;&amp;quot;&lt;br /&gt;
if ( miaStringa == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mentre c'è distinzione tra {{qt|QString}} nulle e vuote, ciò è puramente un artefatto storico e per il nuovo codice se ne scoraggia l'uso.&lt;br /&gt;
&lt;br /&gt;
=== QString e lettura da file ===&lt;br /&gt;
&lt;br /&gt;
Se stai leggendo un file, è più veloce convertirlo dalla codifica locale a Unicode ({{qt|QString}}) tutto in una volta, piuttosto che riga per riga. Questo vuol dire che metodi come &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; sono spesso una buona soluzione, seguiti da una singola istanza di {{qt|QString}}.&lt;br /&gt;
&lt;br /&gt;
Per file molto grandi, considera la possibilità di leggere un blocco di righe e quindi eseguire la conversione. In questo modo hai l'opportunità di aggiornare la GUI. Ciò può essere fatto rientrando normalmente nel ciclo eventi, contemporaneamente utilizzando un timer per leggere i blocchi in background, oppure creando un ciclo eventi locale.&lt;br /&gt;
&lt;br /&gt;
Anche se si potrebbe usare anche &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, è scoraggiato siccome porta facilmente a subdoli problemi fatali.&lt;br /&gt;
&lt;br /&gt;
=== Leggere QString da un KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emette il segnale &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; appena arrivano dei dati.&lt;br /&gt;
Un errore comune sta nel leggere tutti i dati disponibili nello slot connesso e convertirli in un {{qt|QString}} così come sono: i dati arrivano arbitrariamente segmentati, così caratteri multi-byte potrebbero essere tagliati in pezzi e perciò invalidati. Esistono molti approcci al problema:&lt;br /&gt;
&lt;br /&gt;
* Hai veramente bisogno di processare i dati che arrivano? Se no, basta utilizzare &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; dopo che il processo è uscito. Diversamente da KDE3, KProcess è ora capace di accumulare i dati per te.&lt;br /&gt;
* Racchiudi il processo in un {{qt|QTextStream}} e leggi intere righe. Questo dovrebbe funzionare a partire dalle Qt 4.4&lt;br /&gt;
* Accumula i pezzi di dati negli slots e processali ogni volta che arriva una riga e dopo un certo timeout. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Codice di esempio]&lt;br /&gt;
&lt;br /&gt;
=== QString e QByteArray ===&lt;br /&gt;
&lt;br /&gt;
Mentre {{qt|QString}} è lo strumento scelto per molte situazioni che richiedono la gestione di stringhe, ce n'è una dove è particolarmente inefficiente. Se stai lavorando con dati in un {{qt|QByteArray}}, fai attenzione a non passarlo a metodi che prendono parametri di tipo {{qt|QString}}, e che ne tirano fuori un QByteArrays di nuovo.&lt;br /&gt;
&lt;br /&gt;
Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QString mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QString maciullaDati( const QString&amp;amp; dati ) {&lt;br /&gt;
    QByteArray str = dati.toLatin1();&lt;br /&gt;
    // maciulla&lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
L'operazione dispendiosa che si verifica è la conversione in {{qt|QString}}, la quale internamente fa conversioni a Unicode. Ciò è inutile perché la prima cosa che il metodo fa è riconvertirla di nuovo con &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. Quindi, se sei sicuro che la conversione a Unicode non è necessaria, cerca di evitare di usare inavvertitamente QString.&lt;br /&gt;
&lt;br /&gt;
L'esempio di cui sopra dovrebbe invece essere scritto come:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QByteArray mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QByteArray maciullaDati( const QByteArray&amp;amp; dati )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
Quando si deve fare il paring di documenti XML, si ha bisogno di iterare su tutti gli elementi. Potresti essere tentato di usare il seguente codice:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ebbene non è corretto: questo ciclo si fermerà prematuramente non appena incontra un {{qt|QDomNode}} che è qualcosa di diverso da un elemento, come un commento.&lt;br /&gt;
&lt;br /&gt;
Il ciclo corretto è qualcosa di simile:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Development/Tutorials/Common_Programming_Mistakes</id>
		<title>Development/Tutorials/Common Programming Mistakes</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Development/Tutorials/Common_Programming_Mistakes"/>
				<updated>2008-11-27T23:02:16Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Common Programming Mistakes|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Abstract ==&lt;br /&gt;
&lt;br /&gt;
This tutorial aims to combine the experience of KDE developers regarding Qt and KDE frameworks dos and don'ts. Besides actual mistakes, it also covers things which are not necessarily &amp;quot;bugs&amp;quot; but which make the code either slower or less readable.&lt;br /&gt;
&lt;br /&gt;
== General C++ ==&lt;br /&gt;
&lt;br /&gt;
This section guides you through some of the more dusty corners of C++ which either tend to be misused or which people often simply get wrong.&lt;br /&gt;
&lt;br /&gt;
=== Anonymous namespaces vs statics ===&lt;br /&gt;
&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely.&lt;br /&gt;
&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. &lt;br /&gt;
&lt;br /&gt;
So for now instead of using anonymous namespaces use static if you do not want a symbol to be exported.&lt;br /&gt;
&lt;br /&gt;
=== NULL pointer issues ===&lt;br /&gt;
&lt;br /&gt;
First and foremost: it is fine to delete a null pointer. So constructs like this that check for null before deleting are simply redundant: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note however, that '''a null check ''is'' required when you delete an array''' - that's because a relatively recent compiler on Solaris does not handle it properly otherwise.&lt;br /&gt;
&lt;br /&gt;
When you delete a pointer, make sure you also set it to 0 so that future attempts to delete that object will not fail in a double delete. So the complete and proper idiom is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may notice that null pointers are marked variously in one of three ways: 0, 0L and NULL. In C, NULL is defined as a null void pointer. However, in C++, this is not possible due to stricter type checking. Therefore, modern C++ implementations define it to a &amp;quot;magic&amp;quot; null pointer constant which can be assigned to any pointer. Older C++ implementations, OTOH, simply defined it to 0L or 0, which provides no additional type safety - one could assign it to an integer variable, which is obviously wrong.&lt;br /&gt;
&lt;br /&gt;
In pointer context, the integer constant zero means &amp;quot;null pointer&amp;quot; - irrespective of the actual binary representation of a null pointer. This means that the choice between 0, 0L and NULL is a question of personal style and getting used to something rather than a technical one - as far as the code in KDE's SVN goes you will see 0 used more commonly than NULL.&lt;br /&gt;
 &lt;br /&gt;
Note, however, that if you want to pass a null pointer constant to a function in a variable argument list, you *must* explicitly cast it to a pointer - the compiler assumes integer context by default, which might or might not match the binary representation of a pointer. Again, it does not matter whether you cast 0, 0L or NULL, but the shorter representation is generally preferred.&lt;br /&gt;
&lt;br /&gt;
=== Member variables ===&lt;br /&gt;
&lt;br /&gt;
You will encounter four major styles of marking class member variables in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variable''' lowercase m, underscore and the name of the variable starting with a lowercase letter. This is the most common style and one prefered for code in kdelibs.&lt;br /&gt;
* '''mVariable''' lowercase m and the name of variable starting with a uppercase letter&lt;br /&gt;
* '''variable_''' variable name starting with a lowercase letter and then an underscore&lt;br /&gt;
* '''_variable''' underscore and the name of variable starting with a lowercase letter. This style is actually usually frowned upon as this notation is also used in some code for function parameters instead. &lt;br /&gt;
&lt;br /&gt;
As it often happens there is not one correct way of doing it, so remember to always follow the syntax used by the application/library to which you are committing.&lt;br /&gt;
&lt;br /&gt;
=== Static variables ===&lt;br /&gt;
&lt;br /&gt;
Try to limit the number of static variables used in your code, especially when committing to a library. Construction and initialization of large number of static variables really hurts the startup times.&lt;br /&gt;
&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction.&lt;br /&gt;
&lt;br /&gt;
Instead, use a static pointer, together with &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; which is defined in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; and is used like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globalA)&lt;br /&gt;
&lt;br /&gt;
void doSomething()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globalA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void doSomethingElse()&lt;br /&gt;
{&lt;br /&gt;
    if (globalA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globalA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globalA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See the [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 API documentation] for &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; for more information.&lt;br /&gt;
&lt;br /&gt;
=== Constant data ===&lt;br /&gt;
&lt;br /&gt;
If you need some constant data of simple data types in several places, you do good by defining it once at a central place, to avoid a mistype in one of the instances. If the data changes there is also only one place you need to edit.&lt;br /&gt;
&lt;br /&gt;
Even if there is only one instance you do good by defining it elsewhere, to avoid so-called &amp;quot;magic numbers&amp;quot; in the code which are unexplained (cmp. 42). Usually this is done at the top of a file to avoid searching for it.&lt;br /&gt;
&lt;br /&gt;
Define the constant data using the language constructs of C++, not the preprocessor instructions, like you may be used to from plain C. This way the compiler can help you to find mistakes by doing type checking.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const int AnswerToAllQuestions = 42;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define AnswerToAllQuestions 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const char SomeString[] = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
static const char* SomeString = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define SomeString &amp;quot;Example&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Forward Declarations ===&lt;br /&gt;
&lt;br /&gt;
You will reduce compile times by forward declaring classes when possible instead of including their respective headers. For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // slow&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // slow&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // slow&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;  &lt;br /&gt;
  &lt;br /&gt;
The above should instead be written like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // fast&lt;br /&gt;
class QStringList; // fast&lt;br /&gt;
class QString;     // fast&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iterators ===&lt;br /&gt;
&lt;br /&gt;
==== Prefer const iterators and cache end() ====&lt;br /&gt;
Prefer to use &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; over normal iterators when possible. Containers, which are being implicitly shared often detach when a call to a non-const &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; methods is made ({{qt|QList}} is an example of such a container). When using a const_iterator also watch out that you are really calling the const version of &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;. Unless your container is actually const itself this probably will not be the case, possibly causing an unnecessary detach of your container. So basically whenever you use const_iterator initialize them using &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt; instead, to be on the safe side. &lt;br /&gt;
&lt;br /&gt;
Cache the return of the &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) method call before doing iteration over large containers. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;SomeClass&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//code which inserts a large number of elements to the container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This avoids the unnecessary creation of the temporary &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) return object on each loop iteration, largely speeding it up.&lt;br /&gt;
&lt;br /&gt;
Prefer to use pre-increment over post-increment operators on iterators as this avoids creating an unnecessary temporary object in the process.&lt;br /&gt;
&lt;br /&gt;
====Take care when erasing elements inside a loop====&lt;br /&gt;
&lt;br /&gt;
When you want to erase some elements from the list, you maybe would use code similar to this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code will potentially crash because it is a dangling iterator after the call to erase().&lt;br /&gt;
You have to rewrite the code this way:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This problem is also discussed in the [http://doc.trolltech.com/4.3/qmap-iterator.html#details Qt documentation for QMap::iterator] but applies to '''all''' Qt iterators&lt;br /&gt;
&lt;br /&gt;
=== memory leaks ===&lt;br /&gt;
&lt;br /&gt;
A very &amp;quot;popular&amp;quot; programming mistake is to do a &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; without a &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; like in this program:&lt;br /&gt;
&lt;br /&gt;
'''mem_gourmet.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void pollute()&lt;br /&gt;
{&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) pollute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You see, ''pollute()'' instanciates a new object ''polluter'' of the class ''t''. Then, the variable ''polluter'' is lost because it is local, but the content (the object) stays on the heap. I could use this program to render my computer unusable within 10 seconds.&lt;br /&gt;
&lt;br /&gt;
To solve this, there are the following approaches:&lt;br /&gt;
* keep the variable on the stack instead of the heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
would become&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t polluter();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* delete polluter using the complementing function to new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete polluter;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A tool to detect memory leaks like this is [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
You can only dynamic_cast to type T from type T2 provided &lt;br /&gt;
that:&lt;br /&gt;
&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported&lt;br /&gt;
&lt;br /&gt;
For instance, we've seen some hard-to-track problems in non-KDE C++ code we're linking with (I think NMM) because of that. It happened that:&lt;br /&gt;
* libphonon loads the NMM plugin&lt;br /&gt;
* NMM plugin links to NMM&lt;br /&gt;
* NMM loads its own plugins&lt;br /&gt;
* NMM's own plugins link to NMM&lt;br /&gt;
&lt;br /&gt;
Some classes in the NMM library did not have well-anchored vtables, so dynamic_casting failed inside the Phonon NMM plugin for objects created in the NMM's own plugins.&lt;br /&gt;
&lt;br /&gt;
== Program Design ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some common problems related to the design of Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Delayed Initialization ===&lt;br /&gt;
&lt;br /&gt;
Although the design of modern C++ applications can be very complex, one recurring problem, which is generally easy to fix, is not using the technique of [http://www.kdedevelopers.org/node/view/509 delayed initialization]. &lt;br /&gt;
&lt;br /&gt;
First, let us look at the standard way of initializing a KDE application: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Notice that &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; is created before the &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; call that starts the event loop. This implies that we want to avoid doing anything non-trivial in the top-level constructor, since it runs before we can even show the window.&lt;br /&gt;
&lt;br /&gt;
The solution is simple: we need to delay the construction of anything besides the GUI until after the event loop has started. Here is how the example class MainWindow's constructor could look to achieve this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Construct your widgets here.  Note that the widgets you&lt;br /&gt;
     * construct here shouldn't require complex initialization&lt;br /&gt;
     * either, or you've defeated the purpose.&lt;br /&gt;
     * All you want to do is create your GUI objects and&lt;br /&gt;
     * QObject::connect&lt;br /&gt;
     * the appropriate signals to their slots.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* This slot will be called as soon as the event loop starts.&lt;br /&gt;
     * Put everything else that needs to be done, including&lt;br /&gt;
     * restoring values, reading files, session restoring, etc here.&lt;br /&gt;
     * It will still take time, but at least your window will be&lt;br /&gt;
     * on the screen, making your app look active.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
Using this technique may not buy you any overall time, but it makes your app ''seem'' quicker to the user who is starting it. This increased perceived responsiveness is reassuring for the user as they get quick feedback that the action of launching the app has succeeded.&lt;br /&gt;
&lt;br /&gt;
When (and only when) the start up can not be made reasonably fast enough, consider using a {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Data Structures ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some of our most common pet-peeves which affect data structures very commonly seen in Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Passing non-POD types ===&lt;br /&gt;
&lt;br /&gt;
Non-POD (&amp;quot;plain old data&amp;quot;) types should be passed by const reference if at all possible. This includes anything other than the basic types such as &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take, for instance, {{qt|QString}}. They should always be passed into methods as &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Even though {{qt|QString}} is implicitly shared it is still more efficient (and safer) to pass const references as opposed to objects by value. &lt;br /&gt;
&lt;br /&gt;
So the canonical signature of a method taking QString arguments is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void myMethod( const QString &amp;amp; foo, const QString &amp;amp; bar );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&lt;br /&gt;
* Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&lt;br /&gt;
* Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&lt;br /&gt;
* Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-11-27T22:53:20Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere mal utilizzati o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;controllare il passaggio file-static&amp;lt;/font&amp;gt;&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;un namespace anonimo non ha linkage interno?&amp;lt;/font&amp;gt;&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile con qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferisci l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator assicurati di stare chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;; altrimenti, a meno che il tuo container sia esso stesso costante, potrebbero esserci detach non necessari del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
// codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
Preferire l'uso degli incrementi prefissi piuttosto di quelli postfissi negli iteratori così da evitare inutili creazioni di oggetti temporanei nel processo.&lt;br /&gt;
&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;approfondire timer e job&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== Falle nella memoria ===&lt;br /&gt;
&lt;br /&gt;
Un errore di programmazione molto &amp;quot;popolare&amp;quot; consiste nel fare un &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; senza un &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; come in questo programma:&lt;br /&gt;
&lt;br /&gt;
'''mem_buongustaio.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void inquina()&lt;br /&gt;
{&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) inquina();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come puoi vedere, ''inquina()'' istanzia un nuovo oggetto ''inquinatore'' di tipo ''t''. Quindi, la variabile ''inquinatore'' viene persa dato che è locale, ma il contenuto (l'oggetto) rimane nello heap. Posso usare questo programma per rendere il mio computer inutilizzabile in 10 secondi.&lt;br /&gt;
&lt;br /&gt;
Per risolvere, ci sono i seguenti approcci:&lt;br /&gt;
* tieni la variabile nello stack invece che nello heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
diventerà&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t inquinatore();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* cancella l'inquinatore usando la funzione complementare a new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete inquinatore;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uno strumento per individuare le falle di memoria come queste è [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
Puoi fare un dynamic_cast al tipo T dal tipo T2 tali che:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare e approfondire&amp;lt;/font&amp;gt;&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported --&amp;gt;&lt;br /&gt;
* T è definito in una libreria a cui fai il link (avrai un errore dal linker se non è così, dal momento che non troverà le informazioni vtable e RTTI)&lt;br /&gt;
* T è &amp;quot;ben-ancorato&amp;quot; in quella libreria. Con &amp;quot;ben-ancorato&amp;quot; intendo che vtable non è un simbolo COMUNE soggetto a fusioni a run-time da parte del linker dinamico. In altre parole, il primo membro virtuale nella definizione della classe deve esistere e non essere inline: deve essere in un file .cpp.&lt;br /&gt;
* T e T2 sono esportati.&lt;br /&gt;
&lt;br /&gt;
Per esempio, noi abbiamo incontrato qualche problema difficile da individuare nel codice C++ non KDE (NMM credo) a cui stavamo facendo il link:&lt;br /&gt;
* libphonon carica il plugin NMM&lt;br /&gt;
* il plugin NMM fa il link a NMM&lt;br /&gt;
* NMM carica i suoi plugins&lt;br /&gt;
* i plugins propri di NMM, fanno il link a NMM&lt;br /&gt;
&lt;br /&gt;
Qualche classe nella libreria di NMM non aveva vtables ben-ancorate, così il dynamic_casting falliva dentro al plugin NMM di Phonon per gli oggetti creatii nei plugins di NMM&lt;br /&gt;
&lt;br /&gt;
== Progettazione delle applicazioni ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione copriremo un po' di problemi comuni relativi alla progettazione di applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Inizializzazione ritardata ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;posticipata?&amp;lt;/font&amp;gt;&lt;br /&gt;
Sebbene il design di moderne applicazioni C++ può essere molto complesso, un problema ricorrente, generalmente facile da sistemare, è il non usare la tecnica dell'[http://www.kdedevelopers.org/node/view/509 inizializzazione ritardata].&lt;br /&gt;
&lt;br /&gt;
Per prima cosa, diamo un'occhiata al modo standard di inizializzare un'applicazione KDE:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Nota che &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; viene creata prima di &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; il quale fa partire il ciclo degli eventi. Ciò implica che vogliamo evitare di fare cose non banali nella parte alta del costruttore, visto che verranno eseguite prima ancora che la finestra venga mostrata.&lt;br /&gt;
&lt;br /&gt;
La soluzione è semplice: abbiamo bisogno di ritardare la costruzione di qualunque cosa oltre alla GUI fino a che il ciclo degli eventi sia partito. Qui di seguito è mostrato come il costruttore della classe MainWindow dovrebbe essere per ottenere questo risultato:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Costruisci i tuoi widget qui. Nota che non devono&lt;br /&gt;
     * richiedere una complessa inizializzazione,&lt;br /&gt;
     * o verrà meno lo scopo di questa tecnica.&lt;br /&gt;
     * Tutto ciò che vorresti fare è creare i tuoi oggetti&lt;br /&gt;
     * della GUI ed usare QObject::connect per connettere&lt;br /&gt;
     * i segnali agli slots appropriati.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* Questo slot sarà chiamato non appena parte il ciclo eventi.&lt;br /&gt;
     * Metti tutto il resto che deve essere fatto, compresi&lt;br /&gt;
     * assegnazione di valori, lettura files, ristebilire sessioni, etc...&lt;br /&gt;
     * Tutto ciò prenderà lo stesso del tempo, ma almeno la tua&lt;br /&gt;
     * finestra sarà visibile a schermo, facendo apparire&lt;br /&gt;
     * attiva l'applicazione.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Usare questa tecnica potrebbe non far risparmiare del tempo in più, ma farà ''sembrare'' più veloce l'applicazione agli utenti che la eseguono. Questa percezione di reattività incrementata è rassicurante per l'utente il quale riceve un rapido feedback per il riuscito avvio dell'applicazione.&lt;br /&gt;
&lt;br /&gt;
Quando (e solo in questo caso) l'avvio non può essere reso ragionevolmente abbastanza veloce, considera l'uso di {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Strutture Dati ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione spazieremo su alcune delle nostre più comuni persecuzioni che affliggono le più comuni strutture dati viste nelle applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Passaggio di tipi non POD ===&lt;br /&gt;
&lt;br /&gt;
I tipi di dato diversi da POD ([http://en.wikipedia.org/wiki/Plain_old_data &amp;quot;Plain Old Data&amp;quot;], dati semplici senza la logica di controllo) dovrebbero essere passati sempre per riferimento costante. Questo include qualunque cosa tranne i tipi base come &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Prendi, per esempio, {{qt|QString}}. Dovrebbero sempre essere passati ai metodi come &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Anche se {{qt|QString}} è implicitamente condiviso è comunque più efficiente (e sicuro) passarlo per referenza costante piuttosto che come oggetto per valore.&lt;br /&gt;
&lt;br /&gt;
Quindi la dichiarazione canonica di un metodo che prende QString come argomento è:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void mioMetodo( const QString &amp;amp; x, const QString &amp;amp; y );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
Se avrai mai bisogno di eliminare una classe derivata da QObject dall'interno di uno dei suoi metodi, non farlo mai in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima o poi ciò causerà un crash perché un metodo di quell'oggetto potrebbe essere invocato dal ciclo eventi delle Qt via slots/signals dopo che tu l'hai eliminato.&lt;br /&gt;
&lt;br /&gt;
Invece usa sempre &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; il quale cerca di fare la stessa cosa di &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; ma in modo più sicuro.&lt;br /&gt;
&lt;br /&gt;
=== QStrings vuote ===&lt;br /&gt;
&lt;br /&gt;
Si è soliti voler verificare se un {{qt|QString}} è vuoto. Di seguito ci sono tre modi per farlo, dei quali i primi due sono corretti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sbagliato! &amp;quot;&amp;quot;&lt;br /&gt;
if ( miaStringa == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mentre c'è distinzione tra {{qt|QString}} nulle e vuote, ciò è puramente un artefatto storico e per il nuovo codice se ne scoraggia l'uso.&lt;br /&gt;
&lt;br /&gt;
=== QString e lettura da file ===&lt;br /&gt;
&lt;br /&gt;
Se stai leggendo un file, è più veloce convertirlo dalla codifica locale a Unicode ({{qt|QString}}) tutto in una volta, piuttosto che riga per riga. Questo vuol dire che metodi come &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; sono spesso una buona soluzione, seguiti da una singola istanza di {{qt|QString}}.&lt;br /&gt;
&lt;br /&gt;
Per file molto grandi, considera la possibilità di leggere un blocco di righe e quindi eseguire la conversione. In questo modo hai l'opportunità di aggiornare la GUI. Ciò può essere fatto rientrando normalmente nel ciclo eventi, contemporaneamente utilizzando un timer per leggere i blocchi in background, oppure creando un ciclo eventi locale.&lt;br /&gt;
&lt;br /&gt;
Anche se si potrebbe usare anche &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, è scoraggiato siccome porta facilmente a subdoli problemi fatali.&lt;br /&gt;
&lt;br /&gt;
=== Leggere QString da un KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emette il segnale &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; appena arrivano dei dati.&lt;br /&gt;
Un errore comune sta nel leggere tutti i dati disponibili nello slot connesso e convertirli in un {{qt|QString}} così come sono: i dati arrivano arbitrariamente segmentati, così caratteri multi-byte potrebbero essere tagliati in pezzi e perciò invalidati. Esistono molti approcci al problema:&lt;br /&gt;
&lt;br /&gt;
* Hai veramente bisogno di processare i dati che arrivano? Se no, basta utilizzare &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; dopo che il processo è uscito. Diversamente da KDE3, KProcess è ora capace di accumulare i dati per te.&lt;br /&gt;
* Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&lt;br /&gt;
* Racchiudi il processo in un {{qt|QTextStream}} e leggi intere righe. Questo dovrebbe funzionare a partire dalle Qt 4.4&lt;br /&gt;
* Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&lt;br /&gt;
* Accumula i pezzi di dati negli slots e processali ogni volta che arriva una riga e dopo un certo timeout. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Codice di esempio]&lt;br /&gt;
&lt;br /&gt;
=== QString e QByteArray ===&lt;br /&gt;
&lt;br /&gt;
Mentre {{qt|QString}} è lo strumento scelto per molte situazioni che richiedono la gestione di stringhe, ce n'è una dove è particolarmente inefficiente. Se stai lavorando con dati in un {{qt|QByteArray}}, fai attenzione a non passarlo a metodi che prendono parametri di tipo {{qt|QString}}, e che ne tirano fuori un QByteArrays di nuovo.&lt;br /&gt;
&lt;br /&gt;
Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QString mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QString maciullaDati( const QString&amp;amp; dati ) {&lt;br /&gt;
    QByteArray str = dati.toLatin1();&lt;br /&gt;
    // maciulla&lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
L'operazione dispendiosa che si verifica è la conversione in {{qt|QString}}, la quale internamente fa conversioni a Unicode. Ciò è inutile perché la prima cosa che il metodo fa è riconvertirla di nuovo con &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. Quindi, se sei sicuro che la conversione a Unicode non è necessaria, cerca di evitare di usare inavvertitamente QString.&lt;br /&gt;
&lt;br /&gt;
L'esempio di cui sopra dovrebbe invece essere scritto come:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QByteArray mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QByteArray maciullaDati( const QByteArray&amp;amp; dati )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
Quando si deve fare il paring di documenti XML, si ha bisogno di iterare su tutti gli elementi. Potresti essere tentato di usare il seguente codice:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ebbene non è corretto: questo ciclo si fermerà prematuramente non appena incontra un {{qt|QDomNode}} che è qualcosa di diverso da un elemento, come un commento.&lt;br /&gt;
&lt;br /&gt;
Il ciclo corretto è qualcosa di simile:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-27T22:52:16Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In order to learn how to develop cool KDE application I've start from the beginning: I aim to translate all the tutorial that I'm facing with.&lt;br /&gt;
&lt;br /&gt;
== Done ==&lt;br /&gt;
&lt;br /&gt;
* [[Template:TutorialBrowser_(it)]]&lt;br /&gt;
&lt;br /&gt;
== Work in Progress ==&lt;br /&gt;
&lt;br /&gt;
=== to recheck ===&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it) | Development/Tutorials/Common_Programming_Mistakes_(it)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Who's Next ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/First_program | Tutorial Hello World]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Template:TutorialBrowser_(it)</id>
		<title>Template:TutorialBrowser (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Template:TutorialBrowser_(it)"/>
				<updated>2008-11-27T22:51:20Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: User:Fresbeeplayer/Template:TutorialBrowser (it) moved to Template:TutorialBrowser (it): Traduzione completa e funzionante&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div class=&amp;quot;rbroundbox&amp;quot; style=&amp;quot;width:100%;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;div class=&amp;quot;rbtopwrap&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;rbtop&amp;quot;&amp;gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;{{{name}}}&amp;lt;/div&amp;gt;&lt;br /&gt;
 &amp;lt;div class=&amp;quot;rbcontent&amp;quot;&amp;gt;&lt;br /&gt;
{|style=&amp;quot;width:100%; background:#dadde0;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| width=10% align=right valign=top | '''Collezione&amp;amp;nbsp;di&amp;amp;nbsp;Tutorial'''&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
| {{{series|n/a}}}&lt;br /&gt;
|-&lt;br /&gt;
| align=right valign=top | '''Prerequisiti'''&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
| {{{pre|Nessuno}}}&lt;br /&gt;
|-&lt;br /&gt;
| align=right valign=top | '''A &amp;amp;nbsp;Seguire'''&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
| {{{next|n/a}}}&lt;br /&gt;
|-&lt;br /&gt;
| align=right valign=top | '''Ulteriori&amp;amp;nbsp;Letture'''&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
|{{{reading|n/a}}}&lt;br /&gt;
|}&lt;br /&gt;
 &amp;lt;/div&amp;gt;&lt;br /&gt;
 &amp;lt;div class=&amp;quot;rbbot&amp;quot;&amp;gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
[[Category:Tutorial]]&amp;lt;noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Uso Raccomandato ==&lt;br /&gt;
&lt;br /&gt;
Per tenere un qualche genere di consistenza nel nostro wiki: se userai questo template, mettilo in cima alla pagina, sopra qualunque ==testata== (ciò lo posizionerà sopra la sezione dei contenuti della pagina) così i lettori potranno trovare le informazioni il più velocemente possibile.&lt;br /&gt;
&lt;br /&gt;
Nota anche che questo template aggiungerà automaticamente la pagina alla [[:Category:Tutorial|categoria tutorial]] in modo che non debba farlo tu stesso.&lt;br /&gt;
&lt;br /&gt;
== Parametri ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Parametro !! Funzione&lt;br /&gt;
|-&lt;br /&gt;
| '''series''' || Il nome della serie di tutorial a cui appartiene&lt;br /&gt;
|-&lt;br /&gt;
| '''name''' || Nome lungo di questo tutorial. Una specie di prefazione super corta.&lt;br /&gt;
|-&lt;br /&gt;
| '''pre''' || Qualsiasi prerequisito per questo tutorial (cerca di ordinarli in modo appropriato e assicurati che siano collegati!).&lt;br /&gt;
|-&lt;br /&gt;
| '''next''' || Un tutorial o due che seguono logicamente questo (nella stessa serie). Oppure un tutorial o due in cui ha senso proseguire.&lt;br /&gt;
|-&lt;br /&gt;
| '''reading''' || Ulteriori letture riguardo la materia di studio trattata in questo tutorial.&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Template]]&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Template:TutorialBrowser_(it)</id>
		<title>User:Fresbeeplayer/Template:TutorialBrowser (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Template:TutorialBrowser_(it)"/>
				<updated>2008-11-27T22:51:20Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: User:Fresbeeplayer/Template:TutorialBrowser (it) moved to Template:TutorialBrowser (it): Traduzione completa e funzionante&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Template:TutorialBrowser (it)]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-11-27T22:49:52Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{User:Fresbeeplayer/Template:TutorialBrowser_(it)|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere mal utilizzati o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;controllare il passaggio file-static&amp;lt;/font&amp;gt;&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;un namespace anonimo non ha linkage interno?&amp;lt;/font&amp;gt;&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile con qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferisci l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator assicurati di stare chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;; altrimenti, a meno che il tuo container sia esso stesso costante, potrebbero esserci detach non necessari del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
// codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
Preferire l'uso degli incrementi prefissi piuttosto di quelli postfissi negli iteratori così da evitare inutili creazioni di oggetti temporanei nel processo.&lt;br /&gt;
&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;approfondire timer e job&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== Falle nella memoria ===&lt;br /&gt;
&lt;br /&gt;
Un errore di programmazione molto &amp;quot;popolare&amp;quot; consiste nel fare un &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; senza un &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; come in questo programma:&lt;br /&gt;
&lt;br /&gt;
'''mem_buongustaio.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void inquina()&lt;br /&gt;
{&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) inquina();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come puoi vedere, ''inquina()'' istanzia un nuovo oggetto ''inquinatore'' di tipo ''t''. Quindi, la variabile ''inquinatore'' viene persa dato che è locale, ma il contenuto (l'oggetto) rimane nello heap. Posso usare questo programma per rendere il mio computer inutilizzabile in 10 secondi.&lt;br /&gt;
&lt;br /&gt;
Per risolvere, ci sono i seguenti approcci:&lt;br /&gt;
* tieni la variabile nello stack invece che nello heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
diventerà&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t inquinatore();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* cancella l'inquinatore usando la funzione complementare a new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete inquinatore;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uno strumento per individuare le falle di memoria come queste è [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
Puoi fare un dynamic_cast al tipo T dal tipo T2 tali che:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare e approfondire&amp;lt;/font&amp;gt;&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported --&amp;gt;&lt;br /&gt;
* T è definito in una libreria a cui fai il link (avrai un errore dal linker se non è così, dal momento che non troverà le informazioni vtable e RTTI)&lt;br /&gt;
* T è &amp;quot;ben-ancorato&amp;quot; in quella libreria. Con &amp;quot;ben-ancorato&amp;quot; intendo che vtable non è un simbolo COMUNE soggetto a fusioni a run-time da parte del linker dinamico. In altre parole, il primo membro virtuale nella definizione della classe deve esistere e non essere inline: deve essere in un file .cpp.&lt;br /&gt;
* T e T2 sono esportati.&lt;br /&gt;
&lt;br /&gt;
Per esempio, noi abbiamo incontrato qualche problema difficile da individuare nel codice C++ non KDE (NMM credo) a cui stavamo facendo il link:&lt;br /&gt;
* libphonon carica il plugin NMM&lt;br /&gt;
* il plugin NMM fa il link a NMM&lt;br /&gt;
* NMM carica i suoi plugins&lt;br /&gt;
* i plugins propri di NMM, fanno il link a NMM&lt;br /&gt;
&lt;br /&gt;
Qualche classe nella libreria di NMM non aveva vtables ben-ancorate, così il dynamic_casting falliva dentro al plugin NMM di Phonon per gli oggetti creatii nei plugins di NMM&lt;br /&gt;
&lt;br /&gt;
== Progettazione delle applicazioni ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione copriremo un po' di problemi comuni relativi alla progettazione di applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Inizializzazione ritardata ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;posticipata?&amp;lt;/font&amp;gt;&lt;br /&gt;
Sebbene il design di moderne applicazioni C++ può essere molto complesso, un problema ricorrente, generalmente facile da sistemare, è il non usare la tecnica dell'[http://www.kdedevelopers.org/node/view/509 inizializzazione ritardata].&lt;br /&gt;
&lt;br /&gt;
Per prima cosa, diamo un'occhiata al modo standard di inizializzare un'applicazione KDE:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Nota che &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; viene creata prima di &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; il quale fa partire il ciclo degli eventi. Ciò implica che vogliamo evitare di fare cose non banali nella parte alta del costruttore, visto che verranno eseguite prima ancora che la finestra venga mostrata.&lt;br /&gt;
&lt;br /&gt;
La soluzione è semplice: abbiamo bisogno di ritardare la costruzione di qualunque cosa oltre alla GUI fino a che il ciclo degli eventi sia partito. Qui di seguito è mostrato come il costruttore della classe MainWindow dovrebbe essere per ottenere questo risultato:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Costruisci i tuoi widget qui. Nota che non devono&lt;br /&gt;
     * richiedere una complessa inizializzazione,&lt;br /&gt;
     * o verrà meno lo scopo di questa tecnica.&lt;br /&gt;
     * Tutto ciò che vorresti fare è creare i tuoi oggetti&lt;br /&gt;
     * della GUI ed usare QObject::connect per connettere&lt;br /&gt;
     * i segnali agli slots appropriati.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* Questo slot sarà chiamato non appena parte il ciclo eventi.&lt;br /&gt;
     * Metti tutto il resto che deve essere fatto, compresi&lt;br /&gt;
     * assegnazione di valori, lettura files, ristebilire sessioni, etc...&lt;br /&gt;
     * Tutto ciò prenderà lo stesso del tempo, ma almeno la tua&lt;br /&gt;
     * finestra sarà visibile a schermo, facendo apparire&lt;br /&gt;
     * attiva l'applicazione.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Usare questa tecnica potrebbe non far risparmiare del tempo in più, ma farà ''sembrare'' più veloce l'applicazione agli utenti che la eseguono. Questa percezione di reattività incrementata è rassicurante per l'utente il quale riceve un rapido feedback per il riuscito avvio dell'applicazione.&lt;br /&gt;
&lt;br /&gt;
Quando (e solo in questo caso) l'avvio non può essere reso ragionevolmente abbastanza veloce, considera l'uso di {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Strutture Dati ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione spazieremo su alcune delle nostre più comuni persecuzioni che affliggono le più comuni strutture dati viste nelle applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Passaggio di tipi non POD ===&lt;br /&gt;
&lt;br /&gt;
I tipi di dato diversi da POD ([http://en.wikipedia.org/wiki/Plain_old_data &amp;quot;Plain Old Data&amp;quot;], dati semplici senza la logica di controllo) dovrebbero essere passati sempre per riferimento costante. Questo include qualunque cosa tranne i tipi base come &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Prendi, per esempio, {{qt|QString}}. Dovrebbero sempre essere passati ai metodi come &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Anche se {{qt|QString}} è implicitamente condiviso è comunque più efficiente (e sicuro) passarlo per referenza costante piuttosto che come oggetto per valore.&lt;br /&gt;
&lt;br /&gt;
Quindi la dichiarazione canonica di un metodo che prende QString come argomento è:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void mioMetodo( const QString &amp;amp; x, const QString &amp;amp; y );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
Se avrai mai bisogno di eliminare una classe derivata da QObject dall'interno di uno dei suoi metodi, non farlo mai in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima o poi ciò causerà un crash perché un metodo di quell'oggetto potrebbe essere invocato dal ciclo eventi delle Qt via slots/signals dopo che tu l'hai eliminato.&lt;br /&gt;
&lt;br /&gt;
Invece usa sempre &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; il quale cerca di fare la stessa cosa di &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; ma in modo più sicuro.&lt;br /&gt;
&lt;br /&gt;
=== QStrings vuote ===&lt;br /&gt;
&lt;br /&gt;
Si è soliti voler verificare se un {{qt|QString}} è vuoto. Di seguito ci sono tre modi per farlo, dei quali i primi due sono corretti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sbagliato! &amp;quot;&amp;quot;&lt;br /&gt;
if ( miaStringa == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mentre c'è distinzione tra {{qt|QString}} nulle e vuote, ciò è puramente un artefatto storico e per il nuovo codice se ne scoraggia l'uso.&lt;br /&gt;
&lt;br /&gt;
=== QString e lettura da file ===&lt;br /&gt;
&lt;br /&gt;
Se stai leggendo un file, è più veloce convertirlo dalla codifica locale a Unicode ({{qt|QString}}) tutto in una volta, piuttosto che riga per riga. Questo vuol dire che metodi come &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; sono spesso una buona soluzione, seguiti da una singola istanza di {{qt|QString}}.&lt;br /&gt;
&lt;br /&gt;
Per file molto grandi, considera la possibilità di leggere un blocco di righe e quindi eseguire la conversione. In questo modo hai l'opportunità di aggiornare la GUI. Ciò può essere fatto rientrando normalmente nel ciclo eventi, contemporaneamente utilizzando un timer per leggere i blocchi in background, oppure creando un ciclo eventi locale.&lt;br /&gt;
&lt;br /&gt;
Anche se si potrebbe usare anche &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, è scoraggiato siccome porta facilmente a subdoli problemi fatali.&lt;br /&gt;
&lt;br /&gt;
=== Leggere QString da un KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emette il segnale &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; appena arrivano dei dati.&lt;br /&gt;
Un errore comune sta nel leggere tutti i dati disponibili nello slot connesso e convertirli in un {{qt|QString}} così come sono: i dati arrivano arbitrariamente segmentati, così caratteri multi-byte potrebbero essere tagliati in pezzi e perciò invalidati. Esistono molti approcci al problema:&lt;br /&gt;
&lt;br /&gt;
* Hai veramente bisogno di processare i dati che arrivano? Se no, basta utilizzare &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; dopo che il processo è uscito. Diversamente da KDE3, KProcess è ora capace di accumulare i dati per te.&lt;br /&gt;
* Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&lt;br /&gt;
* Racchiudi il processo in un {{qt|QTextStream}} e leggi intere righe. Questo dovrebbe funzionare a partire dalle Qt 4.4&lt;br /&gt;
* Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&lt;br /&gt;
* Accumula i pezzi di dati negli slots e processali ogni volta che arriva una riga e dopo un certo timeout. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Codice di esempio]&lt;br /&gt;
&lt;br /&gt;
=== QString e QByteArray ===&lt;br /&gt;
&lt;br /&gt;
Mentre {{qt|QString}} è lo strumento scelto per molte situazioni che richiedono la gestione di stringhe, ce n'è una dove è particolarmente inefficiente. Se stai lavorando con dati in un {{qt|QByteArray}}, fai attenzione a non passarlo a metodi che prendono parametri di tipo {{qt|QString}}, e che ne tirano fuori un QByteArrays di nuovo.&lt;br /&gt;
&lt;br /&gt;
Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QString mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QString maciullaDati( const QString&amp;amp; dati ) {&lt;br /&gt;
    QByteArray str = dati.toLatin1();&lt;br /&gt;
    // maciulla&lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
L'operazione dispendiosa che si verifica è la conversione in {{qt|QString}}, la quale internamente fa conversioni a Unicode. Ciò è inutile perché la prima cosa che il metodo fa è riconvertirla di nuovo con &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. Quindi, se sei sicuro che la conversione a Unicode non è necessaria, cerca di evitare di usare inavvertitamente QString.&lt;br /&gt;
&lt;br /&gt;
L'esempio di cui sopra dovrebbe invece essere scritto come:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QByteArray mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QByteArray maciullaDati( const QByteArray&amp;amp; dati )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
Quando si deve fare il paring di documenti XML, si ha bisogno di iterare su tutti gli elementi. Potresti essere tentato di usare il seguente codice:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ebbene non è corretto: questo ciclo si fermerà prematuramente non appena incontra un {{qt|QDomNode}} che è qualcosa di diverso da un elemento, come un commento.&lt;br /&gt;
&lt;br /&gt;
Il ciclo corretto è qualcosa di simile:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-27T22:46:30Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In order to learn how to develop cool KDE application I've start from the beginning: I aim to translate all the tutorial that I'm facing with.&lt;br /&gt;
&lt;br /&gt;
== Done ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Work in Progress ==&lt;br /&gt;
&lt;br /&gt;
=== to recheck ===&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it) | Development/Tutorials/Common_Programming_Mistakes_(it)]]&lt;br /&gt;
* [[User:Fresbeeplayer/Template:TutorialBrowser_(it) | Template:TutorialBrowser_(it)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Who's Next ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/First_program | Tutorial Hello World]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Template:TutorialBrowser_(it)</id>
		<title>Template:TutorialBrowser (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Template:TutorialBrowser_(it)"/>
				<updated>2008-11-27T22:44:43Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: New page: &amp;lt;div class=&amp;quot;rbroundbox&amp;quot; style=&amp;quot;width:100%;&amp;quot;&amp;gt;  &amp;lt;div class=&amp;quot;rbtopwrap&amp;quot;&amp;gt;   &amp;lt;div class=&amp;quot;rbtop&amp;quot;&amp;gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;{{{name}}}&amp;lt;/div&amp;gt;  &amp;lt;div class=&amp;quot;rbcontent&amp;quot;&amp;gt; {|style=&amp;quot;width:100%; background:#dadde...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div class=&amp;quot;rbroundbox&amp;quot; style=&amp;quot;width:100%;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;div class=&amp;quot;rbtopwrap&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;rbtop&amp;quot;&amp;gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;{{{name}}}&amp;lt;/div&amp;gt;&lt;br /&gt;
 &amp;lt;div class=&amp;quot;rbcontent&amp;quot;&amp;gt;&lt;br /&gt;
{|style=&amp;quot;width:100%; background:#dadde0;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| width=10% align=right valign=top | '''Collezione&amp;amp;nbsp;di&amp;amp;nbsp;Tutorial'''&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
| {{{series|n/a}}}&lt;br /&gt;
|-&lt;br /&gt;
| align=right valign=top | '''Prerequisiti'''&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
| {{{pre|Nessuno}}}&lt;br /&gt;
|-&lt;br /&gt;
| align=right valign=top | '''A &amp;amp;nbsp;Seguire'''&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
| {{{next|n/a}}}&lt;br /&gt;
|-&lt;br /&gt;
| align=right valign=top | '''Ulteriori&amp;amp;nbsp;Letture'''&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
|{{{reading|n/a}}}&lt;br /&gt;
|}&lt;br /&gt;
 &amp;lt;/div&amp;gt;&lt;br /&gt;
 &amp;lt;div class=&amp;quot;rbbot&amp;quot;&amp;gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
[[Category:Tutorial]]&amp;lt;noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Uso Raccomandato ==&lt;br /&gt;
&lt;br /&gt;
Per tenere un qualche genere di consistenza nel nostro wiki: se userai questo template, mettilo in cima alla pagina, sopra qualunque ==testata== (ciò lo posizionerà sopra la sezione dei contenuti della pagina) così i lettori potranno trovare le informazioni il più velocemente possibile.&lt;br /&gt;
&lt;br /&gt;
Nota anche che questo template aggiungerà automaticamente la pagina alla [[:Category:Tutorial|categoria tutorial]] in modo che non debba farlo tu stesso.&lt;br /&gt;
&lt;br /&gt;
== Parametri ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Parametro !! Funzione&lt;br /&gt;
|-&lt;br /&gt;
| '''series''' || Il nome della serie di tutorial a cui appartiene&lt;br /&gt;
|-&lt;br /&gt;
| '''name''' || Nome lungo di questo tutorial. Una specie di prefazione super corta.&lt;br /&gt;
|-&lt;br /&gt;
| '''pre''' || Qualsiasi prerequisito per questo tutorial (cerca di ordinarli in modo appropriato e assicurati che siano collegati!).&lt;br /&gt;
|-&lt;br /&gt;
| '''next''' || Un tutorial o due che seguono logicamente questo (nella stessa serie). Oppure un tutorial o due in cui ha senso proseguire.&lt;br /&gt;
|-&lt;br /&gt;
| '''reading''' || Ulteriori letture riguardo la materia di studio trattata in questo tutorial.&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Template]]&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-27T22:13:43Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In order to learn how to develop cool KDE application I've start from the beginning: I aim to translate all the tutorial that I'm facing with.&lt;br /&gt;
&lt;br /&gt;
== Done ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Work in Progress ==&lt;br /&gt;
&lt;br /&gt;
=== to recheck ===&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it) | Development/Tutorials/Common_Programming_Mistakes_(it)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Who's Next ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/First_program | Tutorial Hello World]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-11-27T22:10:51Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere mal utilizzati o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;controllare il passaggio file-static&amp;lt;/font&amp;gt;&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;un namespace anonimo non ha linkage interno?&amp;lt;/font&amp;gt;&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile con qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferisci l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator assicurati di stare chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;; altrimenti, a meno che il tuo container sia esso stesso costante, potrebbero esserci detach non necessari del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
// codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
Preferire l'uso degli incrementi prefissi piuttosto di quelli postfissi negli iteratori così da evitare inutili creazioni di oggetti temporanei nel processo.&lt;br /&gt;
&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;approfondire timer e job&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== Falle nella memoria ===&lt;br /&gt;
&lt;br /&gt;
Un errore di programmazione molto &amp;quot;popolare&amp;quot; consiste nel fare un &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; senza un &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; come in questo programma:&lt;br /&gt;
&lt;br /&gt;
'''mem_buongustaio.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void inquina()&lt;br /&gt;
{&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) inquina();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come puoi vedere, ''inquina()'' istanzia un nuovo oggetto ''inquinatore'' di tipo ''t''. Quindi, la variabile ''inquinatore'' viene persa dato che è locale, ma il contenuto (l'oggetto) rimane nello heap. Posso usare questo programma per rendere il mio computer inutilizzabile in 10 secondi.&lt;br /&gt;
&lt;br /&gt;
Per risolvere, ci sono i seguenti approcci:&lt;br /&gt;
* tieni la variabile nello stack invece che nello heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
diventerà&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t inquinatore();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* cancella l'inquinatore usando la funzione complementare a new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete inquinatore;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uno strumento per individuare le falle di memoria come queste è [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
Puoi fare un dynamic_cast al tipo T dal tipo T2 tali che:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare e approfondire&amp;lt;/font&amp;gt;&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported --&amp;gt;&lt;br /&gt;
* T è definito in una libreria a cui fai il link (avrai un errore dal linker se non è così, dal momento che non troverà le informazioni vtable e RTTI)&lt;br /&gt;
* T è &amp;quot;ben-ancorato&amp;quot; in quella libreria. Con &amp;quot;ben-ancorato&amp;quot; intendo che vtable non è un simbolo COMUNE soggetto a fusioni a run-time da parte del linker dinamico. In altre parole, il primo membro virtuale nella definizione della classe deve esistere e non essere inline: deve essere in un file .cpp.&lt;br /&gt;
* T e T2 sono esportati.&lt;br /&gt;
&lt;br /&gt;
Per esempio, noi abbiamo incontrato qualche problema difficile da individuare nel codice C++ non KDE (NMM credo) a cui stavamo facendo il link:&lt;br /&gt;
* libphonon carica il plugin NMM&lt;br /&gt;
* il plugin NMM fa il link a NMM&lt;br /&gt;
* NMM carica i suoi plugins&lt;br /&gt;
* i plugins propri di NMM, fanno il link a NMM&lt;br /&gt;
&lt;br /&gt;
Qualche classe nella libreria di NMM non aveva vtables ben-ancorate, così il dynamic_casting falliva dentro al plugin NMM di Phonon per gli oggetti creatii nei plugins di NMM&lt;br /&gt;
&lt;br /&gt;
== Progettazione delle applicazioni ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione copriremo un po' di problemi comuni relativi alla progettazione di applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Inizializzazione ritardata ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;posticipata?&amp;lt;/font&amp;gt;&lt;br /&gt;
Sebbene il design di moderne applicazioni C++ può essere molto complesso, un problema ricorrente, generalmente facile da sistemare, è il non usare la tecnica dell'[http://www.kdedevelopers.org/node/view/509 inizializzazione ritardata].&lt;br /&gt;
&lt;br /&gt;
Per prima cosa, diamo un'occhiata al modo standard di inizializzare un'applicazione KDE:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Nota che &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; viene creata prima di &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; il quale fa partire il ciclo degli eventi. Ciò implica che vogliamo evitare di fare cose non banali nella parte alta del costruttore, visto che verranno eseguite prima ancora che la finestra venga mostrata.&lt;br /&gt;
&lt;br /&gt;
La soluzione è semplice: abbiamo bisogno di ritardare la costruzione di qualunque cosa oltre alla GUI fino a che il ciclo degli eventi sia partito. Qui di seguito è mostrato come il costruttore della classe MainWindow dovrebbe essere per ottenere questo risultato:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Costruisci i tuoi widget qui. Nota che non devono&lt;br /&gt;
     * richiedere una complessa inizializzazione,&lt;br /&gt;
     * o verrà meno lo scopo di questa tecnica.&lt;br /&gt;
     * Tutto ciò che vorresti fare è creare i tuoi oggetti&lt;br /&gt;
     * della GUI ed usare QObject::connect per connettere&lt;br /&gt;
     * i segnali agli slots appropriati.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* Questo slot sarà chiamato non appena parte il ciclo eventi.&lt;br /&gt;
     * Metti tutto il resto che deve essere fatto, compresi&lt;br /&gt;
     * assegnazione di valori, lettura files, ristebilire sessioni, etc...&lt;br /&gt;
     * Tutto ciò prenderà lo stesso del tempo, ma almeno la tua&lt;br /&gt;
     * finestra sarà visibile a schermo, facendo apparire&lt;br /&gt;
     * attiva l'applicazione.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Usare questa tecnica potrebbe non far risparmiare del tempo in più, ma farà ''sembrare'' più veloce l'applicazione agli utenti che la eseguono. Questa percezione di reattività incrementata è rassicurante per l'utente il quale riceve un rapido feedback per il riuscito avvio dell'applicazione.&lt;br /&gt;
&lt;br /&gt;
Quando (e solo in questo caso) l'avvio non può essere reso ragionevolmente abbastanza veloce, considera l'uso di {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Strutture Dati ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione spazieremo su alcune delle nostre più comuni persecuzioni che affliggono le più comuni strutture dati viste nelle applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Passaggio di tipi non POD ===&lt;br /&gt;
&lt;br /&gt;
I tipi di dato diversi da POD ([http://en.wikipedia.org/wiki/Plain_old_data &amp;quot;Plain Old Data&amp;quot;], dati semplici senza la logica di controllo) dovrebbero essere passati sempre per riferimento costante. Questo include qualunque cosa tranne i tipi base come &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Prendi, per esempio, {{qt|QString}}. Dovrebbero sempre essere passati ai metodi come &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Anche se {{qt|QString}} è implicitamente condiviso è comunque più efficiente (e sicuro) passarlo per referenza costante piuttosto che come oggetto per valore.&lt;br /&gt;
&lt;br /&gt;
Quindi la dichiarazione canonica di un metodo che prende QString come argomento è:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void mioMetodo( const QString &amp;amp; x, const QString &amp;amp; y );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
Se avrai mai bisogno di eliminare una classe derivata da QObject dall'interno di uno dei suoi metodi, non farlo mai in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima o poi ciò causerà un crash perché un metodo di quell'oggetto potrebbe essere invocato dal ciclo eventi delle Qt via slots/signals dopo che tu l'hai eliminato.&lt;br /&gt;
&lt;br /&gt;
Invece usa sempre &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; il quale cerca di fare la stessa cosa di &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; ma in modo più sicuro.&lt;br /&gt;
&lt;br /&gt;
=== QStrings vuote ===&lt;br /&gt;
&lt;br /&gt;
Si è soliti voler verificare se un {{qt|QString}} è vuoto. Di seguito ci sono tre modi per farlo, dei quali i primi due sono corretti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sbagliato! &amp;quot;&amp;quot;&lt;br /&gt;
if ( miaStringa == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mentre c'è distinzione tra {{qt|QString}} nulle e vuote, ciò è puramente un artefatto storico e per il nuovo codice se ne scoraggia l'uso.&lt;br /&gt;
&lt;br /&gt;
=== QString e lettura da file ===&lt;br /&gt;
&lt;br /&gt;
Se stai leggendo un file, è più veloce convertirlo dalla codifica locale a Unicode ({{qt|QString}}) tutto in una volta, piuttosto che riga per riga. Questo vuol dire che metodi come &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; sono spesso una buona soluzione, seguiti da una singola istanza di {{qt|QString}}.&lt;br /&gt;
&lt;br /&gt;
Per file molto grandi, considera la possibilità di leggere un blocco di righe e quindi eseguire la conversione. In questo modo hai l'opportunità di aggiornare la GUI. Ciò può essere fatto rientrando normalmente nel ciclo eventi, contemporaneamente utilizzando un timer per leggere i blocchi in background, oppure creando un ciclo eventi locale.&lt;br /&gt;
&lt;br /&gt;
Anche se si potrebbe usare anche &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, è scoraggiato siccome porta facilmente a subdoli problemi fatali.&lt;br /&gt;
&lt;br /&gt;
=== Leggere QString da un KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emette il segnale &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; appena arrivano dei dati.&lt;br /&gt;
Un errore comune sta nel leggere tutti i dati disponibili nello slot connesso e convertirli in un {{qt|QString}} così come sono: i dati arrivano arbitrariamente segmentati, così caratteri multi-byte potrebbero essere tagliati in pezzi e perciò invalidati. Esistono molti approcci al problema:&lt;br /&gt;
&lt;br /&gt;
* Hai veramente bisogno di processare i dati che arrivano? Se no, basta utilizzare &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; dopo che il processo è uscito. Diversamente da KDE3, KProcess è ora capace di accumulare i dati per te.&lt;br /&gt;
* Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&lt;br /&gt;
* Racchiudi il processo in un {{qt|QTextStream}} e leggi intere righe. Questo dovrebbe funzionare a partire dalle Qt 4.4&lt;br /&gt;
* Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&lt;br /&gt;
* Accumula i pezzi di dati negli slots e processali ogni volta che arriva una riga e dopo un certo timeout. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Codice di esempio]&lt;br /&gt;
&lt;br /&gt;
=== QString e QByteArray ===&lt;br /&gt;
&lt;br /&gt;
Mentre {{qt|QString}} è lo strumento scelto per molte situazioni che richiedono la gestione di stringhe, ce n'è una dove è particolarmente inefficiente. Se stai lavorando con dati in un {{qt|QByteArray}}, fai attenzione a non passarlo a metodi che prendono parametri di tipo {{qt|QString}}, e che ne tirano fuori un QByteArrays di nuovo.&lt;br /&gt;
&lt;br /&gt;
Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QString mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QString maciullaDati( const QString&amp;amp; dati ) {&lt;br /&gt;
    QByteArray str = dati.toLatin1();&lt;br /&gt;
    // maciulla&lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
L'operazione dispendiosa che si verifica è la conversione in {{qt|QString}}, la quale internamente fa conversioni a Unicode. Ciò è inutile perché la prima cosa che il metodo fa è riconvertirla di nuovo con &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. Quindi, se sei sicuro che la conversione a Unicode non è necessaria, cerca di evitare di usare inavvertitamente QString.&lt;br /&gt;
&lt;br /&gt;
L'esempio di cui sopra dovrebbe invece essere scritto come:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QByteArray mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QByteArray maciullaDati( const QByteArray&amp;amp; dati )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
Quando si deve fare il paring di documenti XML, si ha bisogno di iterare su tutti gli elementi. Potresti essere tentato di usare il seguente codice:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ebbene non è corretto: questo ciclo si fermerà prematuramente non appena incontra un {{qt|QDomNode}} che è qualcosa di diverso da un elemento, come un commento.&lt;br /&gt;
&lt;br /&gt;
Il ciclo corretto è qualcosa di simile:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-11-27T22:05:17Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere mal utilizzati o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;controllare il passaggio file-static&amp;lt;/font&amp;gt;&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;un namespace anonimo non ha linkage interno?&amp;lt;/font&amp;gt;&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile con qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferisci l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator assicurati di stare chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;; altrimenti, a meno che il tuo container sia esso stesso costante, potrebbero esserci detach non necessari del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
// codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
Preferire l'uso degli incrementi prefissi piuttosto di quelli postfissi negli iteratori così da evitare inutili creazioni di oggetti temporanei nel processo.&lt;br /&gt;
&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;approfondire timer e job&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== Falle nella memoria ===&lt;br /&gt;
&lt;br /&gt;
Un errore di programmazione molto &amp;quot;popolare&amp;quot; consiste nel fare un &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; senza un &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; come in questo programma:&lt;br /&gt;
&lt;br /&gt;
'''mem_buongustaio.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void inquina()&lt;br /&gt;
{&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) inquina();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come puoi vedere, ''inquina()'' istanzia un nuovo oggetto ''inquinatore'' di tipo ''t''. Quindi, la variabile ''inquinatore'' viene persa dato che è locale, ma il contenuto (l'oggetto) rimane nello heap. Posso usare questo programma per rendere il mio computer inutilizzabile in 10 secondi.&lt;br /&gt;
&lt;br /&gt;
Per risolvere, ci sono i seguenti approcci:&lt;br /&gt;
* tieni la variabile nello stack invece che nello heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
diventerà&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t inquinatore();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* cancella l'inquinatore usando la funzione complementare a new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete inquinatore;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uno strumento per individuare le falle di memoria come queste è [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
Puoi fare un dynamic_cast al tipo T dal tipo T2 tali che:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare e approfondire&amp;lt;/font&amp;gt;&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported --&amp;gt;&lt;br /&gt;
* T è definito in una libreria a cui fai il link (avrai un errore dal linker se non è così, dal momento che non troverà le informazioni vtable e RTTI)&lt;br /&gt;
* T è &amp;quot;ben-ancorato&amp;quot; in quella libreria. Con &amp;quot;ben-ancorato&amp;quot; intendo che vtable non è un simbolo COMUNE soggetto a fusioni a run-time da parte del linker dinamico. In altre parole, il primo membro virtuale nella definizione della classe deve esistere e non essere inline: deve essere in un file .cpp.&lt;br /&gt;
* T e T2 sono esportati.&lt;br /&gt;
&lt;br /&gt;
Per esempio, noi abbiamo incontrato qualche problema difficile da individuare nel codice C++ non KDE (NMM credo) a cui stavamo facendo il link:&lt;br /&gt;
* libphonon carica il plugin NMM&lt;br /&gt;
* il plugin NMM fa il link a NMM&lt;br /&gt;
* NMM carica i suoi plugins&lt;br /&gt;
* i plugins propri di NMM, fanno il link a NMM&lt;br /&gt;
&lt;br /&gt;
Qualche classe nella libreria di NMM non aveva vtables ben-ancorate, così il dynamic_casting falliva dentro al plugin NMM di Phonon per gli oggetti creatii nei plugins di NMM&lt;br /&gt;
&lt;br /&gt;
== Progettazione delle applicazioni ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione copriremo un po' di problemi comuni relativi alla progettazione di applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Inizializzazione ritardata ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;posticipata?&amp;lt;/font&amp;gt;&lt;br /&gt;
Sebbene il design di moderne applicazioni C++ può essere molto complesso, un problema ricorrente, generalmente facile da sistemare, è il non usare la tecnica dell'[http://www.kdedevelopers.org/node/view/509 inizializzazione ritardata].&lt;br /&gt;
&lt;br /&gt;
Per prima cosa, diamo un'occhiata al modo standard di inizializzare un'applicazione KDE:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Nota che &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; viene creata prima di &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; il quale fa partire il ciclo degli eventi. Ciò implica che vogliamo evitare di fare cose non banali nella parte alta del costruttore, visto che verranno eseguite prima ancora che la finestra venga mostrata.&lt;br /&gt;
&lt;br /&gt;
La soluzione è semplice: abbiamo bisogno di ritardare la costruzione di qualunque cosa oltre alla GUI fino a che il ciclo degli eventi sia partito. Qui di seguito è mostrato come il costruttore della classe MainWindow dovrebbe essere per ottenere questo risultato:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Costruisci i tuoi widget qui. Nota che non devono&lt;br /&gt;
     * richiedere una complessa inizializzazione,&lt;br /&gt;
     * o verrà meno lo scopo di questa tecnica.&lt;br /&gt;
     * Tutto ciò che vorresti fare è creare i tuoi oggetti&lt;br /&gt;
     * della GUI ed usare QObject::connect per connettere&lt;br /&gt;
     * i segnali agli slots appropriati.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* Questo slot sarà chiamato non appena parte il ciclo eventi.&lt;br /&gt;
     * Metti tutto il resto che deve essere fatto, compresi&lt;br /&gt;
     * assegnazione di valori, lettura files, ristebilire sessioni, etc...&lt;br /&gt;
     * Tutto ciò prenderà lo stesso del tempo, ma almeno la tua&lt;br /&gt;
     * finestra sarà visibile a schermo, facendo apparire&lt;br /&gt;
     * attiva l'applicazione.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Usare questa tecnica potrebbe non far risparmiare del tempo in più, ma farà ''sembrare'' più veloce l'applicazione agli utenti che la eseguono. Questa percezione di reattività incrementata è rassicurante per l'utente il quale riceve un rapido feedback per il riuscito avvio dell'applicazione.&lt;br /&gt;
&lt;br /&gt;
Quando (e solo in questo caso) l'avvio non può essere reso ragionevolmente abbastanza veloce, considera l'uso di {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Strutture Dati ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione spazieremo su alcune delle nostre più comuni persecuzioni che affliggono le più comuni strutture dati viste nelle applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Passaggio di tipi non POD ===&lt;br /&gt;
&lt;br /&gt;
I tipi di dato diversi da POD ([http://en.wikipedia.org/wiki/Plain_old_data &amp;quot;Plain Old Data&amp;quot;], dati semplici senza la logica di controllo) dovrebbero essere passati sempre per riferimento costante. Questo include qualunque cosa tranne i tipi base come &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Prendi, per esempio, {{qt|QString}}. Dovrebbero sempre essere passati ai metodi come &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Anche se {{qt|QString}} è implicitamente condiviso è comunque più efficiente (e sicuro) passarlo per referenza costante piuttosto che come oggetto per valore.&lt;br /&gt;
&lt;br /&gt;
Quindi la dichiarazione canonica di un metodo che prende QString come argomento è:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void mioMetodo( const QString &amp;amp; x, const QString &amp;amp; y );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
Se avrai mai bisogno di eliminare una classe derivata da QObject dall'interno di uno dei suoi metodi, non farlo mai in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima o poi ciò causerà un crash perché un metodo di quell'oggetto potrebbe essere invocato dal ciclo eventi delle Qt via slots/signals dopo che tu l'hai eliminato.&lt;br /&gt;
&lt;br /&gt;
Invece usa sempre &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; il quale cerca di fare la stessa cosa di &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; ma in modo più sicuro.&lt;br /&gt;
&lt;br /&gt;
=== QStrings vuote ===&lt;br /&gt;
&lt;br /&gt;
Si è soliti voler verificare se un {{qt|QString}} è vuoto. Di seguito ci sono tre modi per farlo, dei quali i primi due sono corretti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Corretto&lt;br /&gt;
if ( miaStringa == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sbagliato! &amp;quot;&amp;quot;&lt;br /&gt;
if ( miaStringa == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mentre c'è distinzione tra {{qt|QString}} nulle e vuote, ciò è puramente un artefatto storico e per il nuovo codice se ne scoraggia l'uso.&lt;br /&gt;
&lt;br /&gt;
=== QString e lettura da file ===&lt;br /&gt;
&lt;br /&gt;
Se stai leggendo un file, è più veloce convertirlo dalla codifica locale a Unicode ({{qt|QString}}) tutto in una volta, piuttosto che riga per riga. Questo vuol dire che metodi come &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; sono spesso una buona soluzione, seguiti da una singola istanza di {{qt|QString}}.&lt;br /&gt;
&lt;br /&gt;
Per file molto grandi, considera la possibilità di leggere un blocco di righe e quindi eseguire la conversione. In questo modo hai l'opportunità di aggiornare la GUI. Ciò può essere fatto rientrando normalmente nel ciclo eventi, contemporaneamente utilizzando un timer per leggere i blocchi in background, oppure creando un ciclo eventi locale.&lt;br /&gt;
&lt;br /&gt;
Anche se si potrebbe usare anche &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, è scoraggiato siccome porta facilmente a subdoli problemi fatali.&lt;br /&gt;
&lt;br /&gt;
=== Leggere QString da un KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emette il segnale &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; appena arrivano dei dati.&lt;br /&gt;
Un errore comune sta nel leggere tutti i dati disponibili nello slot connesso e convertirli in un {{qt|QString}} così come sono: i dati arrivano arbitrariamente segmentati, così caratteri multi-byte potrebbero essere tagliati in pezzi e perciò invalidati. Esistono molti approcci al problema:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Hai veramente bisogno di processare i dati che arrivano? Se no, basta utilizzare &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; dopo che il processo è uscito. Diversamente da KDE3, KProcess è ora capace di accumulare i dati per te.&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Racchiudi il processo in un {{qt|QTextStream}} e leggi intere righe. Questo dovrebbe funzionare a partire dalle Qt 4.4&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;Accumula i pezzi di dati negli slots e processali ogni volta che arriva una riga e dopo un certo timeout. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Codice di esempio]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString e QByteArray ===&lt;br /&gt;
&lt;br /&gt;
Mentre {{qt|QString}} è lo strumento scelto per molte situazioni che richiedono la gestione di stringhe, ce n'è una dove è particolarmente inefficiente. Se stai lavorando con dati in un {{qt|QByteArray}}, fai attenzione a non passarlo a metodi che prendono parametri di tipo {{qt|QString}}, e che ne tirano fuori un QByteArrays di nuovo.&lt;br /&gt;
&lt;br /&gt;
Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QString mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QString maciullaDati( const QString&amp;amp; dati ) {&lt;br /&gt;
    QByteArray str = dati.toLatin1();&lt;br /&gt;
    // maciulla&lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
L'operazione dispendiosa che si verifica è la conversione in {{qt|QString}}, la quale internamente fa conversioni a Unicode. Ciò è inutile perché la prima cosa che il metodo fa è riconvertirla di nuovo con &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. Quindi, se sei sicuro che la conversione a Unicode non è necessaria, cerca di evitare di usare inavvertitamente QString.&lt;br /&gt;
&lt;br /&gt;
L'esempio di cui sopra dovrebbe invece essere scritto come:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray mieiDati;&lt;br /&gt;
QByteArray mieiNuoviDati = maciullaDati( mieiDati );&lt;br /&gt;
&lt;br /&gt;
QByteArray maciullaDati( const QByteArray&amp;amp; dati )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
Quando si deve fare il paring di documenti XML, si ha bisogno di iterare su tutti gli elementi. Potresti essere tentato di usare il seguente codice:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ebbene non è corretto: questo ciclo si fermerà prematuramente non appena incontra un {{qt|QDomNode}} che è qualcosa di diverso da un elemento, come un commento.&lt;br /&gt;
&lt;br /&gt;
Il ciclo corretto è qualcosa di simile:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-11-27T20:52:21Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere mal utilizzati o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;controllare il passaggio file-static&amp;lt;/font&amp;gt;&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;un namespace anonimo non ha linkage interno?&amp;lt;/font&amp;gt;&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile con qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferisci l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator assicurati di stare chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;; altrimenti, a meno che il tuo container sia esso stesso costante, potrebbero esserci detach non necessari del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
// codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
Preferire l'uso degli incrementi prefissi piuttosto di quelli postfissi negli iteratori così da evitare inutili creazioni di oggetti temporanei nel processo.&lt;br /&gt;
&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;approfondire timer e job&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== Falle nella memoria ===&lt;br /&gt;
&lt;br /&gt;
Un errore di programmazione molto &amp;quot;popolare&amp;quot; consiste nel fare un &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; senza un &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; come in questo programma:&lt;br /&gt;
&lt;br /&gt;
'''mem_buongustaio.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void inquina()&lt;br /&gt;
{&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) inquina();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come puoi vedere, ''inquina()'' istanzia un nuovo oggetto ''inquinatore'' di tipo ''t''. Quindi, la variabile ''inquinatore'' viene persa dato che è locale, ma il contenuto (l'oggetto) rimane nello heap. Posso usare questo programma per rendere il mio computer inutilizzabile in 10 secondi.&lt;br /&gt;
&lt;br /&gt;
Per risolvere, ci sono i seguenti approcci:&lt;br /&gt;
* tieni la variabile nello stack invece che nello heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
diventerà&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t inquinatore();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* cancella l'inquinatore usando la funzione complementare a new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete inquinatore;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uno strumento per individuare le falle di memoria come queste è [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
Puoi fare un dynamic_cast al tipo T dal tipo T2 tali che:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare e approfondire&amp;lt;/font&amp;gt;&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported --&amp;gt;&lt;br /&gt;
* T è definito in una libreria a cui fai il link (avrai un errore dal linker se non è così, dal momento che non troverà le informazioni vtable e RTTI)&lt;br /&gt;
* T è &amp;quot;ben-ancorato&amp;quot; in quella libreria. Con &amp;quot;ben-ancorato&amp;quot; intendo che vtable non è un simbolo COMUNE soggetto a fusioni a run-time da parte del linker dinamico. In altre parole, il primo membro virtuale nella definizione della classe deve esistere e non essere inline: deve essere in un file .cpp.&lt;br /&gt;
* T e T2 sono esportati.&lt;br /&gt;
&lt;br /&gt;
Per esempio, noi abbiamo incontrato qualche problema difficile da individuare nel codice C++ non KDE (NMM credo) a cui stavamo facendo il link:&lt;br /&gt;
* libphonon carica il plugin NMM&lt;br /&gt;
* il plugin NMM fa il link a NMM&lt;br /&gt;
* NMM carica i suoi plugins&lt;br /&gt;
* i plugins propri di NMM, fanno il link a NMM&lt;br /&gt;
&lt;br /&gt;
Qualche classe nella libreria di NMM non aveva vtables ben-ancorate, così il dynamic_casting falliva dentro al plugin NMM di Phonon per gli oggetti creatii nei plugins di NMM&lt;br /&gt;
&lt;br /&gt;
== Progettazione delle applicazioni ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione copriremo un po' di problemi comuni relativi alla progettazione di applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Inizializzazione ritardata ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;posticipata?&amp;lt;/font&amp;gt;&lt;br /&gt;
Sebbene il design di moderne applicazioni C++ può essere molto complesso, un problema ricorrente, generalmente facile da sistemare, è il non usare la tecnica dell'[http://www.kdedevelopers.org/node/view/509 inizializzazione ritardata].&lt;br /&gt;
&lt;br /&gt;
Per prima cosa, diamo un'occhiata al modo standard di inizializzare un'applicazione KDE:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Nota che &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; viene creata prima di &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; il quale fa partire il ciclo degli eventi. Ciò implica che vogliamo evitare di fare cose non banali nella parte alta del costruttore, visto che verranno eseguite prima ancora che la finestra venga mostrata.&lt;br /&gt;
&lt;br /&gt;
La soluzione è semplice: abbiamo bisogno di ritardare la costruzione di qualunque cosa oltre alla GUI fino a che il ciclo degli eventi sia partito. Qui di seguito è mostrato come il costruttore della classe MainWindow dovrebbe essere per ottenere questo risultato:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Costruisci i tuoi widget qui. Nota che non devono&lt;br /&gt;
     * richiedere una complessa inizializzazione,&lt;br /&gt;
     * o verrà meno lo scopo di questa tecnica.&lt;br /&gt;
     * Tutto ciò che vorresti fare è creare i tuoi oggetti&lt;br /&gt;
     * della GUI ed usare QObject::connect per connettere&lt;br /&gt;
     * i segnali agli slots appropriati.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* Questo slot sarà chiamato non appena parte il ciclo eventi.&lt;br /&gt;
     * Metti tutto il resto che deve essere fatto, compresi&lt;br /&gt;
     * assegnazione di valori, lettura files, ristebilire sessioni, etc...&lt;br /&gt;
     * Tutto ciò prenderà lo stesso del tempo, ma almeno la tua&lt;br /&gt;
     * finestra sarà visibile a schermo, facendo apparire&lt;br /&gt;
     * attiva l'applicazione.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Usare questa tecnica potrebbe non far risparmiare del tempo in più, ma farà ''sembrare'' più veloce l'applicazione agli utenti che la eseguono. Questa percezione di reattività incrementata è rassicurante per l'utente il quale riceve un rapido feedback per il riuscito avvio dell'applicazione.&lt;br /&gt;
&lt;br /&gt;
Quando (e solo in questo caso) l'avvio non può essere reso ragionevolmente abbastanza veloce, considera l'uso di {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Strutture Dati ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione spazieremo su alcune delle nostre più comuni persecuzioni che affliggono le più comuni strutture dati viste nelle applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Passaggio di tipi non POD ===&lt;br /&gt;
&lt;br /&gt;
I tipi di dato diversi da POD ([http://en.wikipedia.org/wiki/Plain_old_data | &amp;quot;Plain Old Data&amp;quot;], dati semplici senza la logica di controllo) dovrebbero essere passati sempre per riferimento costante. Questo include qualunque cosa tranne i tipi base come &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Prendi, per esempio, {{qt|QString}}. Dovrebbero sempre essere passati ai metodi come &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Anche se {{qt|QString}} è implicitamente condiviso è comunque più efficiente (e sicuro) passarlo per referenza costante piuttosto che come oggetto per valore.&lt;br /&gt;
&lt;br /&gt;
Quindi la dichiarazione canonica di un metodo che prende QString come argomento è:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void mioMetodo( const QString &amp;amp; x, const QString &amp;amp; y );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-11-27T18:46:30Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere mal utilizzati o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;controllare il passaggio file-static&amp;lt;/font&amp;gt;&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;un namespace anonimo non ha linkage interno?&amp;lt;/font&amp;gt;&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile con qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferisci l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator assicurati di stare chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;; altrimenti, a meno che il tuo container sia esso stesso costante, potrebbero esserci detach non necessari del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
Preferire l'uso degli incrementi prefissi piuttosto di quelli postfissi negli iteratori così da evitare inutili creazioni di oggetti temporanei nel processo.&lt;br /&gt;
&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;approfondire timer e job&amp;lt;/font&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        // Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== Falle nella memoria ===&lt;br /&gt;
&lt;br /&gt;
Un errore di programmazione molto &amp;quot;popolare&amp;quot; consiste nel fare un &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; senza un &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; come in questo programma:&lt;br /&gt;
&lt;br /&gt;
'''mem_buongustaio.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void inquina()&lt;br /&gt;
{&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) inquina();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come puoi vedere, ''inquina()'' istanzia un nuovo oggetto ''inquinatore'' di tipo ''t''. Quindi, la variabile ''inquinatore'' viene persa dato che è locale, ma il contenuto (l'oggetto) rimane nello heap. Posso usare questo programma per rendere il mio computer inutilizzabile in 10 secondi.&lt;br /&gt;
&lt;br /&gt;
Per risolvere, ci sono i seguenti approcci:&lt;br /&gt;
* tieni la variabile nello stack invece che nello heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
diventerà&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t inquinatore();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* cancella l'inquinatore usando la funzione complementare a new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete inquinatore;&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uno strumento per individuare le falle di memoria come queste è [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
Puoi fare un dynamic_cast al tipo T dal tipo T2 tali che:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare e approfondire&amp;lt;/font&amp;gt;&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported --&amp;gt;&lt;br /&gt;
* T è definito in una libreria a cui fai il link (avrai un errore dal linker se non è così, dal momento che non troverà le informazioni vtable e RTTI)&lt;br /&gt;
* T è &amp;quot;ben-ancorato&amp;quot; in quella libreria. Con &amp;quot;ben-ancorato&amp;quot; intendo che vtable non è un simbolo COMUNE soggetto a fusioni a run-time da parte del linker dinamico. In altre parole, il primo membro virtuale nella definizione della classe deve esistere e non essere inline: deve essere in un file .cpp.&lt;br /&gt;
* T e T2 sono esportati.&lt;br /&gt;
&lt;br /&gt;
Per esempio, noi abbiamo incontrato qualche problema difficile da individuare nel codice C++ non KDE (NMM credo) a cui stavamo facendo il link:&lt;br /&gt;
* libphonon carica il plugin NMM&lt;br /&gt;
* il plugin NMM fa il link a NMM&lt;br /&gt;
* NMM carica i suoi plugins&lt;br /&gt;
* i plugins propri di NMM, fanno il link a NMM&lt;br /&gt;
&lt;br /&gt;
Qualche classe nella libreria di NMM non aveva vtables ben-ancorate, così il dynamic_casting falliva dentro al plugin NMM di Phonon per gli oggetti creatii nei plugins di NMM&lt;br /&gt;
&lt;br /&gt;
== Progettazione delle applicazioni ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione copriremo un po' di problemi comuni relativi alla progettazione di applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Inizializzazione ritardata ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;posticipata?&amp;lt;/font&amp;gt;&lt;br /&gt;
Sebbene il design di moderne applicazioni C++ può essere molto complesso, un problema ricorrente, generalmente facile da sistemare, è il non usare la tecnica dell'[http://www.kdedevelopers.org/node/view/509 inizializzazione ritardata].&lt;br /&gt;
&lt;br /&gt;
Per prima cosa, diamo un'occhiata al modo standard di inizializzare un'applicazione KDE:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Nota che &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; viene creata prima di &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; il quale fa partire il ciclo degli eventi. Ciò implica che vogliamo evitare di fare cose non banali nella parte alta del costruttore, visto che verranno eseguite prima ancora che la finestra venga mostrata.&lt;br /&gt;
&lt;br /&gt;
La soluzione è semplice: abbiamo bisogno di ritardare la costruzione di qualunque cosa oltre alla GUI fino a che il ciclo degli eventi sia partito. Qui di seguito è mostrato come il costruttore della classe MainWindow dovrebbe essere per ottenere questo risultato:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Costruisci i tuoi widget qui. Nota che non devono&lt;br /&gt;
     * richiedere una complessa inizializzazione,&lt;br /&gt;
     * o verrà meno lo scopo di questa tecnica.&lt;br /&gt;
     * Tutto ciò che vorresti fare è creare i tuoi oggetti&lt;br /&gt;
     * della GUI ed usare QObject::connect per connettere&lt;br /&gt;
     * i segnali agli slots appropriati.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* Questo slot sarà chiamato non appena parte il ciclo eventi.&lt;br /&gt;
     * Metti tutto il resto che deve essere fatto, compresi&lt;br /&gt;
     * assegnazione di valori, lettura files, ristebilire sessioni, etc...&lt;br /&gt;
     * Tutto ciò prenderà lo stesso del tempo, ma almeno la tua&lt;br /&gt;
     * finestra sarà visibile a schermo, facendo apparire&lt;br /&gt;
     * attiva l'applicazione.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Usare questa tecnica potrebbe non far risparmiare del tempo in più, ma farà ''sembrare'' più veloce l'applicazione agli utenti che la eseguono. Questa percezione di reattività incrementata è rassicurante per l'utente il quale riceve un rapido feedback per il riuscito avvio dell'applicazione.&lt;br /&gt;
&lt;br /&gt;
Quando (e solo in questo caso) l'avvio non può essere reso ragionevolmente abbastanza veloce, considera l'uso di {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Strutture Dati ==&lt;br /&gt;
&lt;br /&gt;
In questa sezione spazieremo su alcune delle nostre più comuni persecuzioni che affliggono le più comuni strutture dati viste nelle applicazioni Qt/KDE.&lt;br /&gt;
&lt;br /&gt;
=== Passing non-POD types ===&lt;br /&gt;
&lt;br /&gt;
Non-POD (&amp;quot;plain old data&amp;quot;) types should be passed by const reference if at all possible. This includes anything other than the basic types such as &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take, for instance, {{qt|QString}}. They should always be passed into methods as &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Even though {{qt|QString}} is implicitly shared it is still more efficient (and safer) to pass const references as opposed to objects by value. &lt;br /&gt;
&lt;br /&gt;
So the canonical signature of a method taking QString arguments is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void myMethod( const QString &amp;amp; foo, const QString &amp;amp; bar );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-11-27T15:10:50Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere mal utilizzati o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;controllare il passaggio file-static&amp;lt;/font&amp;gt;&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;un namespace anonimo non ha linkage interno?&amp;lt;/font&amp;gt;&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! -- &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;ricontrollare&amp;lt;/font&amp;gt;&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile con qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferire l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator viene controllato anche che stai veramente chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;. A meno che il tuo container è esso stesso costante non sarà questo il caso, probabilmente causando un detach non necessario del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
Preferire l'uso degli incrementi prefissi piuttosto di quelli postfissi negli iteratori così da evitare inutili creazioni di oggetti temporanei nel processo.&lt;br /&gt;
&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== Falle nella memoria ===&lt;br /&gt;
&lt;br /&gt;
Un errore di programmazione molto &amp;quot;popolare&amp;quot; consiste nel fare un &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; senza un &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; come in questo programma:&lt;br /&gt;
&lt;br /&gt;
'''mem_buongustaio.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void inquina()&lt;br /&gt;
{&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) inquina();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come puoi vedere, ''inquina()'' istanzia un nuovo oggetto ''inquinatore'' di tipo ''t''. Quindi, la variabile ''inquinatore'' viene persa dato che è locale, ma il contenuto (l'oggetto) rimane nello heap. Posso usare questo programma per rendere il mio computer inutilizzabile in 10 secondi.&lt;br /&gt;
&lt;br /&gt;
Per risolvere, ci sono i seguenti approcci:&lt;br /&gt;
* tieni la variabile nello stack invece che nello heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* inquinatore = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
diventerà&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t inquinatore();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* cancella l'inquinatore usando la funzione complementare a new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete inquinatore;&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uno strumento per individuare le falle di memoria come queste è [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
You can only dynamic_cast to type T from type T2 provided &lt;br /&gt;
that:&lt;br /&gt;
&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported&lt;br /&gt;
&lt;br /&gt;
For instance, we've seen some hard-to-track problems in non-KDE C++ code we're linking with (I think NMM) because of that. It happened that:&lt;br /&gt;
* libphonon loads the NMM plugin&lt;br /&gt;
* NMM plugin links to NMM&lt;br /&gt;
* NMM loads its own plugins&lt;br /&gt;
* NMM's own plugins link to NMM&lt;br /&gt;
&lt;br /&gt;
Some classes in the NMM library did not have well-anchored vtables, so dynamic_casting failed inside the Phonon NMM plugin for objects created in the NMM's own plugins.&lt;br /&gt;
&lt;br /&gt;
== Program Design ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some common problems related to the design of Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Delayed Initialization ===&lt;br /&gt;
&lt;br /&gt;
Although the design of modern C++ applications can be very complex, one recurring problem, which is generally easy to fix, is not using the technique of [http://www.kdedevelopers.org/node/view/509 delayed initialization]. &lt;br /&gt;
&lt;br /&gt;
First, let us look at the standard way of initializing a KDE application: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Notice that &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; is created before the &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; call that starts the event loop. This implies that we want to avoid doing anything non-trivial in the top-level constructor, since it runs before we can even show the window.&lt;br /&gt;
&lt;br /&gt;
The solution is simple: we need to delay the construction of anything besides the GUI until after the event loop has started. Here is how the example class MainWindow's constructor could look to achieve this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Construct your widgets here.  Note that the widgets you&lt;br /&gt;
     * construct here shouldn't require complex initialization&lt;br /&gt;
     * either, or you've defeated the purpose.&lt;br /&gt;
     * All you want to do is create your GUI objects and&lt;br /&gt;
     * QObject::connect&lt;br /&gt;
     * the appropriate signals to their slots.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* This slot will be called as soon as the event loop starts.&lt;br /&gt;
     * Put everything else that needs to be done, including&lt;br /&gt;
     * restoring values, reading files, session restoring, etc here.&lt;br /&gt;
     * It will still take time, but at least your window will be&lt;br /&gt;
     * on the screen, making your app look active.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
Using this technique may not buy you any overall time, but it makes your app ''seem'' quicker to the user who is starting it. This increased perceived responsiveness is reassuring for the user as they get quick feedback that the action of launching the app has succeeded.&lt;br /&gt;
&lt;br /&gt;
When (and only when) the start up can not be made reasonably fast enough, consider using a {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Data Structures ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some of our most common pet-peeves which affect data structures very commonly seen in Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Passing non-POD types ===&lt;br /&gt;
&lt;br /&gt;
Non-POD (&amp;quot;plain old data&amp;quot;) types should be passed by const reference if at all possible. This includes anything other than the basic types such as &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take, for instance, {{qt|QString}}. They should always be passed into methods as &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Even though {{qt|QString}} is implicitly shared it is still more efficient (and safer) to pass const references as opposed to objects by value. &lt;br /&gt;
&lt;br /&gt;
So the canonical signature of a method taking QString arguments is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void myMethod( const QString &amp;amp; foo, const QString &amp;amp; bar );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-11-27T14:34:05Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == Abstract == --&amp;gt;&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This tutorial aims to combine the experience of KDE developers regarding Qt and KDE frameworks dos and don'ts. Besides actual mistakes, it also covers things which are not necessarily &amp;quot;bugs&amp;quot; but which make the code either slower or less readable. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == General C++ == --&amp;gt;&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This section guides you through some of the more dusty corners of C++ which either tend to be misused or which people often simply get wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere usati male o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Anonymous namespaces vs statics === --&amp;gt;&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- So for now instead of using anonymous namespaces use static if you do not want a symbol to be exported. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === NULL pointer issues === --&amp;gt;&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- First and foremost: it is fine to delete a null pointer. So constructs like this that check for null before deleting are simply redundant: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Note however, that '''a null check ''is'' required when you delete an array''' - that's because a relatively recent compiler on Solaris does not handle it properly otherwise. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- When you delete a pointer, make sure you also set it to 0 so that future attempts to delete that object will not fail in a double delete. So the complete and proper idiom is: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You may notice that null pointers are marked variously in one of three ways: 0, 0L and NULL. In C, NULL is defined as a null void pointer. However, in C++, this is not possible due to stricter type checking. Therefore, modern C++ implementations define it to a &amp;quot;magic&amp;quot; null pointer constant which can be assigned to any pointer. Older C++ implementations, OTOH, simply defined it to 0L or 0, which provides no additional type safety - one could assign it to an integer variable, which is obviously wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- In pointer context, the integer constant zero means &amp;quot;null pointer&amp;quot; - irrespective of the actual binary representation of a null pointer. This means that the choice between 0, 0L and NULL is a question of personal style and getting used to something rather than a technical one - as far as the code in KDE's SVN goes you will see 0 used more commonly than NULL. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;!-- Note, however, that if you want to pass a null pointer constant to a function in a variable argument list, you *must* explicitly cast it to a pointer - the compiler assumes integer context by default, which might or might not match the binary representation of a pointer. Again, it does not matter whether you cast 0, 0L or NULL, but the shorter representation is generally preferred. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Member variables === --&amp;gt;&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You will encounter four major styles of marking class member variables in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variable''' lowercase m, underscore and the name of the variable starting with a lowercase letter. This is the most common style and one prefered for code in kdelibs.&lt;br /&gt;
* '''mVariable''' lowercase m and the name of variable starting with a uppercase letter&lt;br /&gt;
* '''variable_''' variable name starting with a lowercase letter and then an underscore&lt;br /&gt;
* '''_variable''' underscore and the name of variable starting with a lowercase letter. This style is actually usually frowned upon as this notation is also used in some code for function parameters instead. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- As it often happens there is not one correct way of doing it, so remember to always follow the syntax used by the application/library to which you are committing. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Static variables === --&amp;gt;&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Try to limit the number of static variables used in your code, especially when committing to a library. Construction and initialization of large number of static variables really hurts the startup times. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Instead, use a static pointer, together with &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; which is defined in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; and is used like this: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globalA)&lt;br /&gt;
&lt;br /&gt;
void doSomething()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globalA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void doSomethingElse()&lt;br /&gt;
{&lt;br /&gt;
    if (globalA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globalA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globalA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- See the [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 API documentation] for &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; for more information. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Constant data === --&amp;gt;&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you need some constant data of simple data types in several places, you do good by defining it once at a central place, to avoid a mistype in one of the instances. If the data changes there is also only one place you need to edit. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Even if there is only one instance you do good by defining it elsewhere, to avoid so-called &amp;quot;magic numbers&amp;quot; in the code which are unexplained (cmp. 42). Usually this is done at the top of a file to avoid searching for it. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Define the constant data using the language constructs of C++, not the preprocessor instructions, like you may be used to from plain C. This way the compiler can help you to find mistakes by doing type checking. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const int AnswerToAllQuestions = 42;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define AnswerToAllQuestions 42&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile che punta a qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const char SomeString[] = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
static const char* SomeString = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define SomeString &amp;quot;Example&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Forward Declarations === --&amp;gt;&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You will reduce compile times by forward declaring classes when possible instead of including their respective headers. For example: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // slow&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // slow&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // slow&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- The above should instead be written like this: --&amp;gt;&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // fast&lt;br /&gt;
class QStringList; // fast&lt;br /&gt;
class QString;     // fast&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Iterators === --&amp;gt;&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- ==== Prefer const iterators and cache end() ====&lt;br /&gt;
Prefer to use &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; over normal iterators when possible. Containers, which are being implicitly shared often detach when a call to a non-const &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; methods is made ({{qt|QList}} is an example of such a container). When using a const_iterator also watch out that you are really calling the const version of &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;. Unless your container is actually const itself this probably will not be the case, possibly causing an unnecessary detach of your container. So basically whenever you use const_iterator initialize them using &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt; instead, to be on the safe side. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferire l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator viene controllato anche che stai veramente chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; e &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;. A meno che il tuo container è esso stesso costante non sarà questo il caso, probabilmente causando un detach non necessario del tuo container. Fondamentalmente ogni qual volta usi const_iterator inizializzalo usando &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;, per stare sul sicuro.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Cache the return of the &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) method call before doing iteration over large containers. For example: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Conserva il valore di ritorno del metodo &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) prima di iterare su un grande container. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;SomeClass&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//code which inserts a large number of elements to the container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;QualcheClasse&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//codice che inserisce un grande numero di elementi nel container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This avoids the unnecessary creation of the temporary &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) return object on each loop iteration, largely speeding it up. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo evita la creazione non necessaria di un oggetto temporaneo ritornato da &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (o &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) ad ogni iterazione del ciclo, velocizzandolo ampiamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Prefer to use pre-increment over post-increment operators on iterators as this avoids creating an unnecessary temporary object in the process. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Preferire l'uso degli incrementi prefissi piuttosto di quelli postfissi negli iteratori così da evitare inutili creazioni di oggetti temporanei nel processo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- ====Take care when erasing elements inside a loop==== --&amp;gt;&lt;br /&gt;
==== Fai attenzione quando cancelli elementi dentro un ciclo ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- When you want to erase some elements from the list, you maybe would use code similar to this: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quando vuoi cancellare qualche elemento dalla lista, vorresti usare codice simile a questo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This code will potentially crash because it is a dangling iterator after the call to erase().&lt;br /&gt;
You have to rewrite the code this way:--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo codice può potenzialmente andare in crash a causa dell'iteratore pendente dopo la chiamata a erase().&lt;br /&gt;
Devi riscrivere il codice in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //Trovato un timer per questo job. Fermiamolo.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This problem is also discussed in the [http://doc.trolltech.com/4.3/qmap-iterator.html#details Qt documentation for QMap::iterator] but applies to '''all''' Qt iterators --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo problema è anche discusso nella [http://doc.trolltech.com/4.3/qmap-iterator.html#details documentazione Qt di QMap::iterator] ma si applica a '''tutti''' gli iteratori delle Qt.&lt;br /&gt;
&lt;br /&gt;
=== memory leaks ===&lt;br /&gt;
&lt;br /&gt;
A very &amp;quot;popular&amp;quot; programming mistake is to do a &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; without a &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; like in this program:&lt;br /&gt;
&lt;br /&gt;
'''mem_gourmet.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void pollute()&lt;br /&gt;
{&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) pollute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You see, ''pollute()'' instanciates a new object ''polluter'' of the class ''t''. Then, the variable ''polluter'' is lost because it is local, but the content (the object) stays on the heap. I could use this program to render my computer unusable within 10 seconds.&lt;br /&gt;
&lt;br /&gt;
To solve this, there are the following approaches:&lt;br /&gt;
* keep the variable on the stack instead of the heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
would become&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t polluter();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* delete polluter using the complementing function to new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete polluter;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A tool to detect memory leaks like this is [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
You can only dynamic_cast to type T from type T2 provided &lt;br /&gt;
that:&lt;br /&gt;
&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported&lt;br /&gt;
&lt;br /&gt;
For instance, we've seen some hard-to-track problems in non-KDE C++ code we're linking with (I think NMM) because of that. It happened that:&lt;br /&gt;
* libphonon loads the NMM plugin&lt;br /&gt;
* NMM plugin links to NMM&lt;br /&gt;
* NMM loads its own plugins&lt;br /&gt;
* NMM's own plugins link to NMM&lt;br /&gt;
&lt;br /&gt;
Some classes in the NMM library did not have well-anchored vtables, so dynamic_casting failed inside the Phonon NMM plugin for objects created in the NMM's own plugins.&lt;br /&gt;
&lt;br /&gt;
== Program Design ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some common problems related to the design of Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Delayed Initialization ===&lt;br /&gt;
&lt;br /&gt;
Although the design of modern C++ applications can be very complex, one recurring problem, which is generally easy to fix, is not using the technique of [http://www.kdedevelopers.org/node/view/509 delayed initialization]. &lt;br /&gt;
&lt;br /&gt;
First, let us look at the standard way of initializing a KDE application: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Notice that &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; is created before the &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; call that starts the event loop. This implies that we want to avoid doing anything non-trivial in the top-level constructor, since it runs before we can even show the window.&lt;br /&gt;
&lt;br /&gt;
The solution is simple: we need to delay the construction of anything besides the GUI until after the event loop has started. Here is how the example class MainWindow's constructor could look to achieve this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Construct your widgets here.  Note that the widgets you&lt;br /&gt;
     * construct here shouldn't require complex initialization&lt;br /&gt;
     * either, or you've defeated the purpose.&lt;br /&gt;
     * All you want to do is create your GUI objects and&lt;br /&gt;
     * QObject::connect&lt;br /&gt;
     * the appropriate signals to their slots.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* This slot will be called as soon as the event loop starts.&lt;br /&gt;
     * Put everything else that needs to be done, including&lt;br /&gt;
     * restoring values, reading files, session restoring, etc here.&lt;br /&gt;
     * It will still take time, but at least your window will be&lt;br /&gt;
     * on the screen, making your app look active.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
Using this technique may not buy you any overall time, but it makes your app ''seem'' quicker to the user who is starting it. This increased perceived responsiveness is reassuring for the user as they get quick feedback that the action of launching the app has succeeded.&lt;br /&gt;
&lt;br /&gt;
When (and only when) the start up can not be made reasonably fast enough, consider using a {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Data Structures ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some of our most common pet-peeves which affect data structures very commonly seen in Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Passing non-POD types ===&lt;br /&gt;
&lt;br /&gt;
Non-POD (&amp;quot;plain old data&amp;quot;) types should be passed by const reference if at all possible. This includes anything other than the basic types such as &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take, for instance, {{qt|QString}}. They should always be passed into methods as &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Even though {{qt|QString}} is implicitly shared it is still more efficient (and safer) to pass const references as opposed to objects by value. &lt;br /&gt;
&lt;br /&gt;
So the canonical signature of a method taking QString arguments is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void myMethod( const QString &amp;amp; foo, const QString &amp;amp; bar );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/KDE_TechBase:Contributors</id>
		<title>KDE TechBase:Contributors</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/KDE_TechBase:Contributors"/>
				<updated>2008-11-27T14:02:15Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: I've added myself to italian team&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Welcome to the contributors page.&lt;br /&gt;
&lt;br /&gt;
'''This site contains a list of ''active'' contributors. It should help to build teams which maintain KDE TechBase's content. If you have questions about KDE TechBase you can ask/email the corresponding person.'''&lt;br /&gt;
&lt;br /&gt;
Please add yourself to the list where appropriate. If you are inactive, please remove yourself again.&lt;br /&gt;
&lt;br /&gt;
== Administrators ==&lt;br /&gt;
&lt;br /&gt;
This is a list of KDE TechBase administrators.&lt;br /&gt;
&lt;br /&gt;
* [[User:Danimo|Danimo]]&lt;br /&gt;
* [[User:Dhaumann|Dhaumann]], &amp;lt;dhaumann at kde dot org&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Reviewers and Article Writers ==&lt;br /&gt;
&lt;br /&gt;
If you are continuously reviewing KDE TechBase changes or writing articles add yourself to the list.&lt;br /&gt;
&lt;br /&gt;
* [[User:Dhaumann|Dhaumann]], &amp;lt;dhaumann at kde dot org&amp;gt;&lt;br /&gt;
* [[User:Milliams|Milliams]]&lt;br /&gt;
* name, &amp;lt;email&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Translation Teams ==&lt;br /&gt;
&lt;br /&gt;
KDE TechBase is [[Help:Wiki Translation|translated]] into many languages. If you translate pages please add yourself to the right translation team.&lt;br /&gt;
&lt;br /&gt;
=== Chinese(simplified) Team ===&lt;br /&gt;
* [[User:Liangqi|Liangqi]], cavendish.qi at gmail dot com&lt;br /&gt;
* name, &amp;lt;email&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spanish Team ===&lt;br /&gt;
* [[User:Martin J. Ponce|Martin J. Ponce]], mjp dot ttc at gmail dot com&lt;br /&gt;
* [[User:edumardo| Eduardo Delgado Díaz (edumardo)]], aesalemolo at gmail dot com&lt;br /&gt;
* name, &amp;lt;email&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== German Team ===&lt;br /&gt;
* [[User:DrSlowDecay|DrSlowDecay]], kde at metalhorde dot de&lt;br /&gt;
* [[User:Rememberme|rememberme]] redict dot info at gmx dot net&lt;br /&gt;
* [[User:sschloenvoigt|Steffen Schloenvoigt]], steffen at schloenvoigt dot de &lt;br /&gt;
* name, &amp;lt;email&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Italian Team ===&lt;br /&gt;
* [[User:Thunder Teaser|Thunder Teaser]], totokid at gmail dot com&lt;br /&gt;
* [[User:Panda84|Panda84]], panda84 at inwind dot it&lt;br /&gt;
* [[User:Fresbeeplayer|Fresbeeplayer]], fresbeeplayer at gmail dot com&lt;br /&gt;
* name, &amp;lt;email&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tamil Team ===&lt;br /&gt;
* [[User:Shriramadhas|Shriramadhas]], shriramadhas at gmail dot com&lt;br /&gt;
* name, &amp;lt;email&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ... Team ===&lt;br /&gt;
* name, &amp;lt;email&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-27T13:56:30Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In order to learn how to develop cool KDE application I've start from the beginning: I aim to translate all the tutorial that I'm facing with.&lt;br /&gt;
&lt;br /&gt;
== Work in Progress ==&lt;br /&gt;
&lt;br /&gt;
* [[User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it) | Development/Tutorials/Common_Programming_Mistakes_(it)]]&lt;br /&gt;
&lt;br /&gt;
== Who's Next ==&lt;br /&gt;
&lt;br /&gt;
* [[Development/Tutorials/First_program | Tutorial Hello World]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni</id>
		<title>User:Fresbeeplayer/Translate ErroriProgrammazioneComuni</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni"/>
				<updated>2008-11-27T13:10:52Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Removing all content from page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)</id>
		<title>User:Fresbeeplayer/Development/Tutorials/Common Programming Mistakes (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Development/Tutorials/Common_Programming_Mistakes_(it)"/>
				<updated>2008-11-27T13:10:33Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: New page: {{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}  {{TutorialBrowser|  series=Getting Started|  name=Errori di Programmazione Comuni|  reading=[[P...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == Abstract == --&amp;gt;&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This tutorial aims to combine the experience of KDE developers regarding Qt and KDE frameworks dos and don'ts. Besides actual mistakes, it also covers things which are not necessarily &amp;quot;bugs&amp;quot; but which make the code either slower or less readable. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == General C++ == --&amp;gt;&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This section guides you through some of the more dusty corners of C++ which either tend to be misused or which people often simply get wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere usati male o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Anonymous namespaces vs statics === --&amp;gt;&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- So for now instead of using anonymous namespaces use static if you do not want a symbol to be exported. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === NULL pointer issues === --&amp;gt;&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- First and foremost: it is fine to delete a null pointer. So constructs like this that check for null before deleting are simply redundant: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Note however, that '''a null check ''is'' required when you delete an array''' - that's because a relatively recent compiler on Solaris does not handle it properly otherwise. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- When you delete a pointer, make sure you also set it to 0 so that future attempts to delete that object will not fail in a double delete. So the complete and proper idiom is: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You may notice that null pointers are marked variously in one of three ways: 0, 0L and NULL. In C, NULL is defined as a null void pointer. However, in C++, this is not possible due to stricter type checking. Therefore, modern C++ implementations define it to a &amp;quot;magic&amp;quot; null pointer constant which can be assigned to any pointer. Older C++ implementations, OTOH, simply defined it to 0L or 0, which provides no additional type safety - one could assign it to an integer variable, which is obviously wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- In pointer context, the integer constant zero means &amp;quot;null pointer&amp;quot; - irrespective of the actual binary representation of a null pointer. This means that the choice between 0, 0L and NULL is a question of personal style and getting used to something rather than a technical one - as far as the code in KDE's SVN goes you will see 0 used more commonly than NULL. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;!-- Note, however, that if you want to pass a null pointer constant to a function in a variable argument list, you *must* explicitly cast it to a pointer - the compiler assumes integer context by default, which might or might not match the binary representation of a pointer. Again, it does not matter whether you cast 0, 0L or NULL, but the shorter representation is generally preferred. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Member variables === --&amp;gt;&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You will encounter four major styles of marking class member variables in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variable''' lowercase m, underscore and the name of the variable starting with a lowercase letter. This is the most common style and one prefered for code in kdelibs.&lt;br /&gt;
* '''mVariable''' lowercase m and the name of variable starting with a uppercase letter&lt;br /&gt;
* '''variable_''' variable name starting with a lowercase letter and then an underscore&lt;br /&gt;
* '''_variable''' underscore and the name of variable starting with a lowercase letter. This style is actually usually frowned upon as this notation is also used in some code for function parameters instead. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- As it often happens there is not one correct way of doing it, so remember to always follow the syntax used by the application/library to which you are committing. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Static variables === --&amp;gt;&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Try to limit the number of static variables used in your code, especially when committing to a library. Construction and initialization of large number of static variables really hurts the startup times. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Instead, use a static pointer, together with &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; which is defined in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; and is used like this: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globalA)&lt;br /&gt;
&lt;br /&gt;
void doSomething()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globalA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void doSomethingElse()&lt;br /&gt;
{&lt;br /&gt;
    if (globalA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globalA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globalA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- See the [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 API documentation] for &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; for more information. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Constant data === --&amp;gt;&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you need some constant data of simple data types in several places, you do good by defining it once at a central place, to avoid a mistype in one of the instances. If the data changes there is also only one place you need to edit. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Even if there is only one instance you do good by defining it elsewhere, to avoid so-called &amp;quot;magic numbers&amp;quot; in the code which are unexplained (cmp. 42). Usually this is done at the top of a file to avoid searching for it. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Define the constant data using the language constructs of C++, not the preprocessor instructions, like you may be used to from plain C. This way the compiler can help you to find mistakes by doing type checking. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const int AnswerToAllQuestions = 42;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define AnswerToAllQuestions 42&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile che punta a qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const char SomeString[] = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
static const char* SomeString = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define SomeString &amp;quot;Example&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Forward Declarations === --&amp;gt;&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You will reduce compile times by forward declaring classes when possible instead of including their respective headers. For example: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // slow&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // slow&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // slow&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- The above should instead be written like this: --&amp;gt;&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // fast&lt;br /&gt;
class QStringList; // fast&lt;br /&gt;
class QString;     // fast&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Iterators === --&amp;gt;&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- ==== Prefer const iterators and cache end() ====&lt;br /&gt;
Prefer to use &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; over normal iterators when possible. Containers, which are being implicitly shared often detach when a call to a non-const &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; methods is made ({{qt|QList}} is an example of such a container). When using a const_iterator also watch out that you are really calling the const version of &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;. Unless your container is actually const itself this probably will not be the case, possibly causing an unnecessary detach of your container. So basically whenever you use const_iterator initialize them using &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt; instead, to be on the safe side. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferire l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso eseguono un detach() (vedi [http://doc.trolltech.com/4.4/shared.html#shared-classes] per più informazioni, n.d.t.) quando viene fatta una chiamata ad un metodo &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; o &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; non costanti ({{qt|QList}} è un esempio di tale container). Usando i const_iterator viene controllato anche che stai veramente chiamando la versione costante di &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt;  &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Cache the return of the &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) method call before doing iteration over large containers. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;SomeClass&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//code which inserts a large number of elements to the container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This avoids the unnecessary creation of the temporary &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) return object on each loop iteration, largely speeding it up.&lt;br /&gt;
&lt;br /&gt;
Prefer to use pre-increment over post-increment operators on iterators as this avoids creating an unnecessary temporary object in the process.&lt;br /&gt;
&lt;br /&gt;
====Take care when erasing elements inside a loop====&lt;br /&gt;
&lt;br /&gt;
When you want to erase some elements from the list, you maybe would use code similar to this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code will potentially crash because it is a dangling iterator after the call to erase().&lt;br /&gt;
You have to rewrite the code this way:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This problem is also discussed in the [http://doc.trolltech.com/4.3/qmap-iterator.html#details Qt documentation for QMap::iterator] but applies to '''all''' Qt iterators&lt;br /&gt;
&lt;br /&gt;
=== memory leaks ===&lt;br /&gt;
&lt;br /&gt;
A very &amp;quot;popular&amp;quot; programming mistake is to do a &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; without a &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; like in this program:&lt;br /&gt;
&lt;br /&gt;
'''mem_gourmet.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void pollute()&lt;br /&gt;
{&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) pollute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You see, ''pollute()'' instanciates a new object ''polluter'' of the class ''t''. Then, the variable ''polluter'' is lost because it is local, but the content (the object) stays on the heap. I could use this program to render my computer unusable within 10 seconds.&lt;br /&gt;
&lt;br /&gt;
To solve this, there are the following approaches:&lt;br /&gt;
* keep the variable on the stack instead of the heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
would become&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t polluter();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* delete polluter using the complementing function to new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete polluter;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A tool to detect memory leaks like this is [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
You can only dynamic_cast to type T from type T2 provided &lt;br /&gt;
that:&lt;br /&gt;
&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported&lt;br /&gt;
&lt;br /&gt;
For instance, we've seen some hard-to-track problems in non-KDE C++ code we're linking with (I think NMM) because of that. It happened that:&lt;br /&gt;
* libphonon loads the NMM plugin&lt;br /&gt;
* NMM plugin links to NMM&lt;br /&gt;
* NMM loads its own plugins&lt;br /&gt;
* NMM's own plugins link to NMM&lt;br /&gt;
&lt;br /&gt;
Some classes in the NMM library did not have well-anchored vtables, so dynamic_casting failed inside the Phonon NMM plugin for objects created in the NMM's own plugins.&lt;br /&gt;
&lt;br /&gt;
== Program Design ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some common problems related to the design of Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Delayed Initialization ===&lt;br /&gt;
&lt;br /&gt;
Although the design of modern C++ applications can be very complex, one recurring problem, which is generally easy to fix, is not using the technique of [http://www.kdedevelopers.org/node/view/509 delayed initialization]. &lt;br /&gt;
&lt;br /&gt;
First, let us look at the standard way of initializing a KDE application: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Notice that &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; is created before the &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; call that starts the event loop. This implies that we want to avoid doing anything non-trivial in the top-level constructor, since it runs before we can even show the window.&lt;br /&gt;
&lt;br /&gt;
The solution is simple: we need to delay the construction of anything besides the GUI until after the event loop has started. Here is how the example class MainWindow's constructor could look to achieve this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Construct your widgets here.  Note that the widgets you&lt;br /&gt;
     * construct here shouldn't require complex initialization&lt;br /&gt;
     * either, or you've defeated the purpose.&lt;br /&gt;
     * All you want to do is create your GUI objects and&lt;br /&gt;
     * QObject::connect&lt;br /&gt;
     * the appropriate signals to their slots.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* This slot will be called as soon as the event loop starts.&lt;br /&gt;
     * Put everything else that needs to be done, including&lt;br /&gt;
     * restoring values, reading files, session restoring, etc here.&lt;br /&gt;
     * It will still take time, but at least your window will be&lt;br /&gt;
     * on the screen, making your app look active.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
Using this technique may not buy you any overall time, but it makes your app ''seem'' quicker to the user who is starting it. This increased perceived responsiveness is reassuring for the user as they get quick feedback that the action of launching the app has succeeded.&lt;br /&gt;
&lt;br /&gt;
When (and only when) the start up can not be made reasonably fast enough, consider using a {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Data Structures ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some of our most common pet-peeves which affect data structures very commonly seen in Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Passing non-POD types ===&lt;br /&gt;
&lt;br /&gt;
Non-POD (&amp;quot;plain old data&amp;quot;) types should be passed by const reference if at all possible. This includes anything other than the basic types such as &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take, for instance, {{qt|QString}}. They should always be passed into methods as &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Even though {{qt|QString}} is implicitly shared it is still more efficient (and safer) to pass const references as opposed to objects by value. &lt;br /&gt;
&lt;br /&gt;
So the canonical signature of a method taking QString arguments is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void myMethod( const QString &amp;amp; foo, const QString &amp;amp; bar );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni</id>
		<title>User:Fresbeeplayer/Translate ErroriProgrammazioneComuni</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni"/>
				<updated>2008-11-27T11:32:54Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == Abstract == --&amp;gt;&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This tutorial aims to combine the experience of KDE developers regarding Qt and KDE frameworks dos and don'ts. Besides actual mistakes, it also covers things which are not necessarily &amp;quot;bugs&amp;quot; but which make the code either slower or less readable. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == General C++ == --&amp;gt;&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This section guides you through some of the more dusty corners of C++ which either tend to be misused or which people often simply get wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere usati male o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Anonymous namespaces vs statics === --&amp;gt;&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- So for now instead of using anonymous namespaces use static if you do not want a symbol to be exported. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === NULL pointer issues === --&amp;gt;&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- First and foremost: it is fine to delete a null pointer. So constructs like this that check for null before deleting are simply redundant: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Note however, that '''a null check ''is'' required when you delete an array''' - that's because a relatively recent compiler on Solaris does not handle it properly otherwise. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- When you delete a pointer, make sure you also set it to 0 so that future attempts to delete that object will not fail in a double delete. So the complete and proper idiom is: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You may notice that null pointers are marked variously in one of three ways: 0, 0L and NULL. In C, NULL is defined as a null void pointer. However, in C++, this is not possible due to stricter type checking. Therefore, modern C++ implementations define it to a &amp;quot;magic&amp;quot; null pointer constant which can be assigned to any pointer. Older C++ implementations, OTOH, simply defined it to 0L or 0, which provides no additional type safety - one could assign it to an integer variable, which is obviously wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- In pointer context, the integer constant zero means &amp;quot;null pointer&amp;quot; - irrespective of the actual binary representation of a null pointer. This means that the choice between 0, 0L and NULL is a question of personal style and getting used to something rather than a technical one - as far as the code in KDE's SVN goes you will see 0 used more commonly than NULL. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;!-- Note, however, that if you want to pass a null pointer constant to a function in a variable argument list, you *must* explicitly cast it to a pointer - the compiler assumes integer context by default, which might or might not match the binary representation of a pointer. Again, it does not matter whether you cast 0, 0L or NULL, but the shorter representation is generally preferred. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Member variables === --&amp;gt;&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You will encounter four major styles of marking class member variables in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variable''' lowercase m, underscore and the name of the variable starting with a lowercase letter. This is the most common style and one prefered for code in kdelibs.&lt;br /&gt;
* '''mVariable''' lowercase m and the name of variable starting with a uppercase letter&lt;br /&gt;
* '''variable_''' variable name starting with a lowercase letter and then an underscore&lt;br /&gt;
* '''_variable''' underscore and the name of variable starting with a lowercase letter. This style is actually usually frowned upon as this notation is also used in some code for function parameters instead. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- As it often happens there is not one correct way of doing it, so remember to always follow the syntax used by the application/library to which you are committing. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Static variables === --&amp;gt;&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Try to limit the number of static variables used in your code, especially when committing to a library. Construction and initialization of large number of static variables really hurts the startup times. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Instead, use a static pointer, together with &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; which is defined in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; and is used like this: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globalA)&lt;br /&gt;
&lt;br /&gt;
void doSomething()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globalA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void doSomethingElse()&lt;br /&gt;
{&lt;br /&gt;
    if (globalA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globalA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globalA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- See the [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 API documentation] for &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; for more information. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Constant data === --&amp;gt;&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you need some constant data of simple data types in several places, you do good by defining it once at a central place, to avoid a mistype in one of the instances. If the data changes there is also only one place you need to edit. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Even if there is only one instance you do good by defining it elsewhere, to avoid so-called &amp;quot;magic numbers&amp;quot; in the code which are unexplained (cmp. 42). Usually this is done at the top of a file to avoid searching for it. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Define the constant data using the language constructs of C++, not the preprocessor instructions, like you may be used to from plain C. This way the compiler can help you to find mistakes by doing type checking. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const int AnswerToAllQuestions = 42;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define AnswerToAllQuestions 42&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile che punta a qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const char SomeString[] = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
static const char* SomeString = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define SomeString &amp;quot;Example&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Forward Declarations === --&amp;gt;&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You will reduce compile times by forward declaring classes when possible instead of including their respective headers. For example: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // slow&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // slow&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // slow&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- The above should instead be written like this: --&amp;gt;&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // fast&lt;br /&gt;
class QStringList; // fast&lt;br /&gt;
class QString;     // fast&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Iterators === --&amp;gt;&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- ==== Prefer const iterators and cache end() ==== --&amp;gt;&lt;br /&gt;
&amp;lt;! --Prefer to use &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; over normal iterators when possible. Containers, which are being implicitly shared often detach when a call to a non-const &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; methods is made ({{qt|QList}} is an example of such a container). When using a const_iterator also watch out that you are really calling the const version of &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;. Unless your container is actually const itself this probably will not be the case, possibly causing an unnecessary detach of your container. So basically whenever you use const_iterator initialize them using &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt; instead, to be on the safe side. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferire l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso &lt;br /&gt;
&lt;br /&gt;
Cache the return of the &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) method call before doing iteration over large containers. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;SomeClass&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//code which inserts a large number of elements to the container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This avoids the unnecessary creation of the temporary &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) return object on each loop iteration, largely speeding it up.&lt;br /&gt;
&lt;br /&gt;
Prefer to use pre-increment over post-increment operators on iterators as this avoids creating an unnecessary temporary object in the process.&lt;br /&gt;
&lt;br /&gt;
====Take care when erasing elements inside a loop====&lt;br /&gt;
&lt;br /&gt;
When you want to erase some elements from the list, you maybe would use code similar to this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code will potentially crash because it is a dangling iterator after the call to erase().&lt;br /&gt;
You have to rewrite the code this way:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This problem is also discussed in the [http://doc.trolltech.com/4.3/qmap-iterator.html#details Qt documentation for QMap::iterator] but applies to '''all''' Qt iterators&lt;br /&gt;
&lt;br /&gt;
=== memory leaks ===&lt;br /&gt;
&lt;br /&gt;
A very &amp;quot;popular&amp;quot; programming mistake is to do a &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; without a &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; like in this program:&lt;br /&gt;
&lt;br /&gt;
'''mem_gourmet.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void pollute()&lt;br /&gt;
{&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) pollute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You see, ''pollute()'' instanciates a new object ''polluter'' of the class ''t''. Then, the variable ''polluter'' is lost because it is local, but the content (the object) stays on the heap. I could use this program to render my computer unusable within 10 seconds.&lt;br /&gt;
&lt;br /&gt;
To solve this, there are the following approaches:&lt;br /&gt;
* keep the variable on the stack instead of the heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
would become&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t polluter();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* delete polluter using the complementing function to new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete polluter;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A tool to detect memory leaks like this is [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
You can only dynamic_cast to type T from type T2 provided &lt;br /&gt;
that:&lt;br /&gt;
&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported&lt;br /&gt;
&lt;br /&gt;
For instance, we've seen some hard-to-track problems in non-KDE C++ code we're linking with (I think NMM) because of that. It happened that:&lt;br /&gt;
* libphonon loads the NMM plugin&lt;br /&gt;
* NMM plugin links to NMM&lt;br /&gt;
* NMM loads its own plugins&lt;br /&gt;
* NMM's own plugins link to NMM&lt;br /&gt;
&lt;br /&gt;
Some classes in the NMM library did not have well-anchored vtables, so dynamic_casting failed inside the Phonon NMM plugin for objects created in the NMM's own plugins.&lt;br /&gt;
&lt;br /&gt;
== Program Design ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some common problems related to the design of Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Delayed Initialization ===&lt;br /&gt;
&lt;br /&gt;
Although the design of modern C++ applications can be very complex, one recurring problem, which is generally easy to fix, is not using the technique of [http://www.kdedevelopers.org/node/view/509 delayed initialization]. &lt;br /&gt;
&lt;br /&gt;
First, let us look at the standard way of initializing a KDE application: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Notice that &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; is created before the &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; call that starts the event loop. This implies that we want to avoid doing anything non-trivial in the top-level constructor, since it runs before we can even show the window.&lt;br /&gt;
&lt;br /&gt;
The solution is simple: we need to delay the construction of anything besides the GUI until after the event loop has started. Here is how the example class MainWindow's constructor could look to achieve this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Construct your widgets here.  Note that the widgets you&lt;br /&gt;
     * construct here shouldn't require complex initialization&lt;br /&gt;
     * either, or you've defeated the purpose.&lt;br /&gt;
     * All you want to do is create your GUI objects and&lt;br /&gt;
     * QObject::connect&lt;br /&gt;
     * the appropriate signals to their slots.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* This slot will be called as soon as the event loop starts.&lt;br /&gt;
     * Put everything else that needs to be done, including&lt;br /&gt;
     * restoring values, reading files, session restoring, etc here.&lt;br /&gt;
     * It will still take time, but at least your window will be&lt;br /&gt;
     * on the screen, making your app look active.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
Using this technique may not buy you any overall time, but it makes your app ''seem'' quicker to the user who is starting it. This increased perceived responsiveness is reassuring for the user as they get quick feedback that the action of launching the app has succeeded.&lt;br /&gt;
&lt;br /&gt;
When (and only when) the start up can not be made reasonably fast enough, consider using a {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Data Structures ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some of our most common pet-peeves which affect data structures very commonly seen in Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Passing non-POD types ===&lt;br /&gt;
&lt;br /&gt;
Non-POD (&amp;quot;plain old data&amp;quot;) types should be passed by const reference if at all possible. This includes anything other than the basic types such as &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take, for instance, {{qt|QString}}. They should always be passed into methods as &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Even though {{qt|QString}} is implicitly shared it is still more efficient (and safer) to pass const references as opposed to objects by value. &lt;br /&gt;
&lt;br /&gt;
So the canonical signature of a method taking QString arguments is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void myMethod( const QString &amp;amp; foo, const QString &amp;amp; bar );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni</id>
		<title>User:Fresbeeplayer/Translate ErroriProgrammazioneComuni</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni"/>
				<updated>2008-11-27T11:31:37Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == Abstract == --&amp;gt;&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This tutorial aims to combine the experience of KDE developers regarding Qt and KDE frameworks dos and don'ts. Besides actual mistakes, it also covers things which are not necessarily &amp;quot;bugs&amp;quot; but which make the code either slower or less readable. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == General C++ == --&amp;gt;&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This section guides you through some of the more dusty corners of C++ which either tend to be misused or which people often simply get wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere usati male o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Anonymous namespaces vs statics === --&amp;gt;&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- So for now instead of using anonymous namespaces use static if you do not want a symbol to be exported. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === NULL pointer issues === --&amp;gt;&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- First and foremost: it is fine to delete a null pointer. So constructs like this that check for null before deleting are simply redundant: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Note however, that '''a null check ''is'' required when you delete an array''' - that's because a relatively recent compiler on Solaris does not handle it properly otherwise. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- When you delete a pointer, make sure you also set it to 0 so that future attempts to delete that object will not fail in a double delete. So the complete and proper idiom is: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You may notice that null pointers are marked variously in one of three ways: 0, 0L and NULL. In C, NULL is defined as a null void pointer. However, in C++, this is not possible due to stricter type checking. Therefore, modern C++ implementations define it to a &amp;quot;magic&amp;quot; null pointer constant which can be assigned to any pointer. Older C++ implementations, OTOH, simply defined it to 0L or 0, which provides no additional type safety - one could assign it to an integer variable, which is obviously wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- In pointer context, the integer constant zero means &amp;quot;null pointer&amp;quot; - irrespective of the actual binary representation of a null pointer. This means that the choice between 0, 0L and NULL is a question of personal style and getting used to something rather than a technical one - as far as the code in KDE's SVN goes you will see 0 used more commonly than NULL. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;!-- Note, however, that if you want to pass a null pointer constant to a function in a variable argument list, you *must* explicitly cast it to a pointer - the compiler assumes integer context by default, which might or might not match the binary representation of a pointer. Again, it does not matter whether you cast 0, 0L or NULL, but the shorter representation is generally preferred. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Member variables === --&amp;gt;&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You will encounter four major styles of marking class member variables in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variable''' lowercase m, underscore and the name of the variable starting with a lowercase letter. This is the most common style and one prefered for code in kdelibs.&lt;br /&gt;
* '''mVariable''' lowercase m and the name of variable starting with a uppercase letter&lt;br /&gt;
* '''variable_''' variable name starting with a lowercase letter and then an underscore&lt;br /&gt;
* '''_variable''' underscore and the name of variable starting with a lowercase letter. This style is actually usually frowned upon as this notation is also used in some code for function parameters instead. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- As it often happens there is not one correct way of doing it, so remember to always follow the syntax used by the application/library to which you are committing. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Static variables === --&amp;gt;&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Try to limit the number of static variables used in your code, especially when committing to a library. Construction and initialization of large number of static variables really hurts the startup times. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Instead, use a static pointer, together with &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; which is defined in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; and is used like this: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globalA)&lt;br /&gt;
&lt;br /&gt;
void doSomething()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globalA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void doSomethingElse()&lt;br /&gt;
{&lt;br /&gt;
    if (globalA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globalA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globalA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- See the [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 API documentation] for &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; for more information. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Constant data === --&amp;gt;&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you need some constant data of simple data types in several places, you do good by defining it once at a central place, to avoid a mistype in one of the instances. If the data changes there is also only one place you need to edit. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Even if there is only one instance you do good by defining it elsewhere, to avoid so-called &amp;quot;magic numbers&amp;quot; in the code which are unexplained (cmp. 42). Usually this is done at the top of a file to avoid searching for it. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Define the constant data using the language constructs of C++, not the preprocessor instructions, like you may be used to from plain C. This way the compiler can help you to find mistakes by doing type checking. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const int AnswerToAllQuestions = 42;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define AnswerToAllQuestions 42&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile che punta a qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const char SomeString[] = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
static const char* SomeString = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define SomeString &amp;quot;Example&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Forward Declarations === --&amp;gt;&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You will reduce compile times by forward declaring classes when possible instead of including their respective headers. For example: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // slow&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // slow&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // slow&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- The above should instead be written like this: --&amp;gt;&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // fast&lt;br /&gt;
class QStringList; // fast&lt;br /&gt;
class QString;     // fast&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Iterators === --&amp;gt;&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- ==== Prefer const iterators and cache end() ====&lt;br /&gt;
Prefer to use &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; over normal iterators when possible. Containers, which are being implicitly shared often detach when a call to a non-const &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; methods is made ({{qt|QList}} is an example of such a container). When using a const_iterator also watch out that you are really calling the const version of &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;. Unless your container is actually const itself this probably will not be the case, possibly causing an unnecessary detach of your container. So basically whenever you use const_iterator initialize them using &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt; instead, to be on the safe side. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferire l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso &lt;br /&gt;
&lt;br /&gt;
Cache the return of the &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) method call before doing iteration over large containers. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;SomeClass&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//code which inserts a large number of elements to the container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This avoids the unnecessary creation of the temporary &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) return object on each loop iteration, largely speeding it up.&lt;br /&gt;
&lt;br /&gt;
Prefer to use pre-increment over post-increment operators on iterators as this avoids creating an unnecessary temporary object in the process.&lt;br /&gt;
&lt;br /&gt;
====Take care when erasing elements inside a loop====&lt;br /&gt;
&lt;br /&gt;
When you want to erase some elements from the list, you maybe would use code similar to this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code will potentially crash because it is a dangling iterator after the call to erase().&lt;br /&gt;
You have to rewrite the code this way:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This problem is also discussed in the [http://doc.trolltech.com/4.3/qmap-iterator.html#details Qt documentation for QMap::iterator] but applies to '''all''' Qt iterators&lt;br /&gt;
&lt;br /&gt;
=== memory leaks ===&lt;br /&gt;
&lt;br /&gt;
A very &amp;quot;popular&amp;quot; programming mistake is to do a &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; without a &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; like in this program:&lt;br /&gt;
&lt;br /&gt;
'''mem_gourmet.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void pollute()&lt;br /&gt;
{&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) pollute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You see, ''pollute()'' instanciates a new object ''polluter'' of the class ''t''. Then, the variable ''polluter'' is lost because it is local, but the content (the object) stays on the heap. I could use this program to render my computer unusable within 10 seconds.&lt;br /&gt;
&lt;br /&gt;
To solve this, there are the following approaches:&lt;br /&gt;
* keep the variable on the stack instead of the heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
would become&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t polluter();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* delete polluter using the complementing function to new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete polluter;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A tool to detect memory leaks like this is [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
You can only dynamic_cast to type T from type T2 provided &lt;br /&gt;
that:&lt;br /&gt;
&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported&lt;br /&gt;
&lt;br /&gt;
For instance, we've seen some hard-to-track problems in non-KDE C++ code we're linking with (I think NMM) because of that. It happened that:&lt;br /&gt;
* libphonon loads the NMM plugin&lt;br /&gt;
* NMM plugin links to NMM&lt;br /&gt;
* NMM loads its own plugins&lt;br /&gt;
* NMM's own plugins link to NMM&lt;br /&gt;
&lt;br /&gt;
Some classes in the NMM library did not have well-anchored vtables, so dynamic_casting failed inside the Phonon NMM plugin for objects created in the NMM's own plugins.&lt;br /&gt;
&lt;br /&gt;
== Program Design ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some common problems related to the design of Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Delayed Initialization ===&lt;br /&gt;
&lt;br /&gt;
Although the design of modern C++ applications can be very complex, one recurring problem, which is generally easy to fix, is not using the technique of [http://www.kdedevelopers.org/node/view/509 delayed initialization]. &lt;br /&gt;
&lt;br /&gt;
First, let us look at the standard way of initializing a KDE application: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Notice that &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; is created before the &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; call that starts the event loop. This implies that we want to avoid doing anything non-trivial in the top-level constructor, since it runs before we can even show the window.&lt;br /&gt;
&lt;br /&gt;
The solution is simple: we need to delay the construction of anything besides the GUI until after the event loop has started. Here is how the example class MainWindow's constructor could look to achieve this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Construct your widgets here.  Note that the widgets you&lt;br /&gt;
     * construct here shouldn't require complex initialization&lt;br /&gt;
     * either, or you've defeated the purpose.&lt;br /&gt;
     * All you want to do is create your GUI objects and&lt;br /&gt;
     * QObject::connect&lt;br /&gt;
     * the appropriate signals to their slots.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* This slot will be called as soon as the event loop starts.&lt;br /&gt;
     * Put everything else that needs to be done, including&lt;br /&gt;
     * restoring values, reading files, session restoring, etc here.&lt;br /&gt;
     * It will still take time, but at least your window will be&lt;br /&gt;
     * on the screen, making your app look active.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
Using this technique may not buy you any overall time, but it makes your app ''seem'' quicker to the user who is starting it. This increased perceived responsiveness is reassuring for the user as they get quick feedback that the action of launching the app has succeeded.&lt;br /&gt;
&lt;br /&gt;
When (and only when) the start up can not be made reasonably fast enough, consider using a {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Data Structures ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some of our most common pet-peeves which affect data structures very commonly seen in Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Passing non-POD types ===&lt;br /&gt;
&lt;br /&gt;
Non-POD (&amp;quot;plain old data&amp;quot;) types should be passed by const reference if at all possible. This includes anything other than the basic types such as &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take, for instance, {{qt|QString}}. They should always be passed into methods as &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Even though {{qt|QString}} is implicitly shared it is still more efficient (and safer) to pass const references as opposed to objects by value. &lt;br /&gt;
&lt;br /&gt;
So the canonical signature of a method taking QString arguments is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void myMethod( const QString &amp;amp; foo, const QString &amp;amp; bar );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni</id>
		<title>User:Fresbeeplayer/Translate ErroriProgrammazioneComuni</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni"/>
				<updated>2008-11-27T11:30:14Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == Abstract == --&amp;gt;&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This tutorial aims to combine the experience of KDE developers regarding Qt and KDE frameworks dos and don'ts. Besides actual mistakes, it also covers things which are not necessarily &amp;quot;bugs&amp;quot; but which make the code either slower or less readable. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle librerie Qt e KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bachi&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == General C++ == --&amp;gt;&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This section guides you through some of the more dusty corners of C++ which either tend to be misused or which people often simply get wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere usati male o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Anonymous namespaces vs statics === --&amp;gt;&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- So for now instead of using anonymous namespaces use static if you do not want a symbol to be exported. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === NULL pointer issues === --&amp;gt;&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- First and foremost: it is fine to delete a null pointer. So constructs like this that check for null before deleting are simply redundant: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Note however, that '''a null check ''is'' required when you delete an array''' - that's because a relatively recent compiler on Solaris does not handle it properly otherwise. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- When you delete a pointer, make sure you also set it to 0 so that future attempts to delete that object will not fail in a double delete. So the complete and proper idiom is: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You may notice that null pointers are marked variously in one of three ways: 0, 0L and NULL. In C, NULL is defined as a null void pointer. However, in C++, this is not possible due to stricter type checking. Therefore, modern C++ implementations define it to a &amp;quot;magic&amp;quot; null pointer constant which can be assigned to any pointer. Older C++ implementations, OTOH, simply defined it to 0L or 0, which provides no additional type safety - one could assign it to an integer variable, which is obviously wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- In pointer context, the integer constant zero means &amp;quot;null pointer&amp;quot; - irrespective of the actual binary representation of a null pointer. This means that the choice between 0, 0L and NULL is a question of personal style and getting used to something rather than a technical one - as far as the code in KDE's SVN goes you will see 0 used more commonly than NULL. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;!-- Note, however, that if you want to pass a null pointer constant to a function in a variable argument list, you *must* explicitly cast it to a pointer - the compiler assumes integer context by default, which might or might not match the binary representation of a pointer. Again, it does not matter whether you cast 0, 0L or NULL, but the shorter representation is generally preferred. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Member variables === --&amp;gt;&lt;br /&gt;
=== Variabili membro ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! --You will encounter four major styles of marking class member variables in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variable''' lowercase m, underscore and the name of the variable starting with a lowercase letter. This is the most common style and one prefered for code in kdelibs.&lt;br /&gt;
* '''mVariable''' lowercase m and the name of variable starting with a uppercase letter&lt;br /&gt;
* '''variable_''' variable name starting with a lowercase letter and then an underscore&lt;br /&gt;
* '''_variable''' underscore and the name of variable starting with a lowercase letter. This style is actually usually frowned upon as this notation is also used in some code for function parameters instead. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Incontrerai quattro maggiori stili per segnare le variabili membre delle classi in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variabile''' m minuscola, underscore ed il nome della variabile che comincia con una lettera minuscola. Questo è lo stile più comune ed uno dei preferiti nel codice delle kdelibs.&lt;br /&gt;
* '''mVariabile''' m minuscola ed il nome della variabile che comincia con una lettera maiuscola.&lt;br /&gt;
* '''variabile_''' il nome della variabile comincia con la lettera minuscola ed alla fine un underscore.&lt;br /&gt;
* '''_variabile''' un underscore e poi il nome della variabile con la lettera iniziale minuscola. Questa notazione di solito è sconsigliata siccome è anche utilizzata in qualche codice per i parametri delle funzioni.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- As it often happens there is not one correct way of doing it, so remember to always follow the syntax used by the application/library to which you are committing. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Come accade spesso non c'è un modo corretto per farlo, perciò ricorda sempre di rispettare la sintassi utilizzata dall'applicazione/libreria alla quale stai facendo commit.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Static variables === --&amp;gt;&lt;br /&gt;
=== Variabili statiche ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;! --Try to limit the number of static variables used in your code, especially when committing to a library. Construction and initialization of large number of static variables really hurts the startup times. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cerca di limitare il numero di variabili statiche nel tuo codice, specialmente quando fate il commit per una libreria. Costruzione ed inizializzazione di un grande numero di variabili statiche fa veramente male ai tempi di avvio.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non usare variabili class-static, in particolare non nelle librerie e nei moduli sebbene sia anche scoraggiato nelle applicazioni. Oggetti statici portano ad un sacco di problemi tra cui difficoltà di debug dei crash dovuto ad un ordine indefinito di costruttore/distruttore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Instead, use a static pointer, together with &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; which is defined in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; and is used like this: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Invece, usa un puntatore statico insieme a &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; definito in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; ed usato in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globalA)&lt;br /&gt;
&lt;br /&gt;
void doSomething()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globalA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void doSomethingElse()&lt;br /&gt;
{&lt;br /&gt;
    if (globalA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globalA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globalA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globaleA)&lt;br /&gt;
&lt;br /&gt;
void faQualcosa()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globaleA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void faQualcosAltro()&lt;br /&gt;
{&lt;br /&gt;
    if (globaleA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globaleA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installaPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globaleA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- See the [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 API documentation] for &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; for more information. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Vedi la [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 documentazione delle API] per più informazioni su &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Constant data === --&amp;gt;&lt;br /&gt;
=== Dati costanti ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you need some constant data of simple data types in several places, you do good by defining it once at a central place, to avoid a mistype in one of the instances. If the data changes there is also only one place you need to edit. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai bisogno di qualche dato costante per semplici tipi di dato in molti punti, fai bene a definirli una volta sola in un posto centrale, onde evitare errori di digitazione in una delle istanze. Se i dati cambiano hai bisogno di editare solo in un punto.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Even if there is only one instance you do good by defining it elsewhere, to avoid so-called &amp;quot;magic numbers&amp;quot; in the code which are unexplained (cmp. 42). Usually this is done at the top of a file to avoid searching for it. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Anche se usati una sola volta è meglio definirli da un'altra parte, per evitare inspiegabili &amp;quot;numeri magici&amp;quot; nel codice (cmp. 42). Di solito ciò viene fatto in cima al file per non doverli ricercare.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Define the constant data using the language constructs of C++, not the preprocessor instructions, like you may be used to from plain C. This way the compiler can help you to find mistakes by doing type checking. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definisci i dati costanti usando i costrutti del C++, non le istruzioni del preprocessore, come potresti essere abituato a fare dal C. In questo modo il compilatore può aiutarti a trovare errori facendo il controllo di tipo.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const int AnswerToAllQuestions = 42;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define AnswerToAllQuestions 42&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const int LaRispostaATutteLeDomande = 42;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define LaRispostaATutteLeDomande 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se stai definendo un array costante non usare un puntatore come tipo di dato. Invece usa il suo tipo ed appendi il simbolo dell'array di indefinita lunghezza, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, dopo il nome. Altrimenti definirai anche una variabile che punta a qualche dato costante. La variabile potrebbe per sbaglio essere assegnata ad un altro puntatore, senza che il compilatore se ne lamenti. E l'accesso all'array sarebbe indiretto, perché per primo deve essere letto il valore della variabile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const char SomeString[] = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
static const char* SomeString = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define SomeString &amp;quot;Example&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Corretto!&lt;br /&gt;
static const char UnaStringa[] = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
static const char* UnaStringa = &amp;quot;Esempio&amp;quot;;&lt;br /&gt;
// Sbagliato!&lt;br /&gt;
#define UnaStringa &amp;quot;Esempio&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Forward Declarations === --&amp;gt;&lt;br /&gt;
=== Dichiarazioni anticipate ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You will reduce compile times by forward declaring classes when possible instead of including their respective headers. For example: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ridurrai i tempi di compilazione dichiarando anticipatamente le classi quando possibile invece di includere i rispettivi headers. Per esempio:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // slow&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // slow&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // slow&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // lento&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // lento&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // lento&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- The above should instead be written like this: --&amp;gt;&lt;br /&gt;
Dovrebbe invece essere scritto in questo modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // fast&lt;br /&gt;
class QStringList; // fast&lt;br /&gt;
class QString;     // fast&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // veloce&lt;br /&gt;
class QStringList; // veloce&lt;br /&gt;
class QString;     // veloce&lt;br /&gt;
class QualcheInterfaccia&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void azioneWidget( QWidget *widget ) =0;&lt;br /&gt;
    virtual void azioneStringa( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void azioniListaStringhe( const QStringList&amp;amp; strLista ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Iterators === --&amp;gt;&lt;br /&gt;
=== Iteratori ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- ==== Prefer const iterators and cache end() ====&lt;br /&gt;
Prefer to use &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; over normal iterators when possible. Containers, which are being implicitly shared often detach when a call to a non-const &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; methods is made ({{qt|QList}} is an example of such a container). When using a const_iterator also watch out that you are really calling the const version of &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;. Unless your container is actually const itself this probably will not be the case, possibly causing an unnecessary detach of your container. So basically whenever you use const_iterator initialize them using &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt; instead, to be on the safe side. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Preferire iteratori costanti e conservare end() ====&lt;br /&gt;
Preferire l'uso dei &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; rispetto ai normali iteratori quando possibile. I containers, implicitamente condivisi, spesso &lt;br /&gt;
&lt;br /&gt;
Cache the return of the &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) method call before doing iteration over large containers. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;SomeClass&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//code which inserts a large number of elements to the container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This avoids the unnecessary creation of the temporary &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) return object on each loop iteration, largely speeding it up.&lt;br /&gt;
&lt;br /&gt;
Prefer to use pre-increment over post-increment operators on iterators as this avoids creating an unnecessary temporary object in the process.&lt;br /&gt;
&lt;br /&gt;
====Take care when erasing elements inside a loop====&lt;br /&gt;
&lt;br /&gt;
When you want to erase some elements from the list, you maybe would use code similar to this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code will potentially crash because it is a dangling iterator after the call to erase().&lt;br /&gt;
You have to rewrite the code this way:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This problem is also discussed in the [http://doc.trolltech.com/4.3/qmap-iterator.html#details Qt documentation for QMap::iterator] but applies to '''all''' Qt iterators&lt;br /&gt;
&lt;br /&gt;
=== memory leaks ===&lt;br /&gt;
&lt;br /&gt;
A very &amp;quot;popular&amp;quot; programming mistake is to do a &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; without a &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; like in this program:&lt;br /&gt;
&lt;br /&gt;
'''mem_gourmet.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void pollute()&lt;br /&gt;
{&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) pollute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You see, ''pollute()'' instanciates a new object ''polluter'' of the class ''t''. Then, the variable ''polluter'' is lost because it is local, but the content (the object) stays on the heap. I could use this program to render my computer unusable within 10 seconds.&lt;br /&gt;
&lt;br /&gt;
To solve this, there are the following approaches:&lt;br /&gt;
* keep the variable on the stack instead of the heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
would become&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t polluter();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* delete polluter using the complementing function to new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete polluter;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A tool to detect memory leaks like this is [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
You can only dynamic_cast to type T from type T2 provided &lt;br /&gt;
that:&lt;br /&gt;
&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported&lt;br /&gt;
&lt;br /&gt;
For instance, we've seen some hard-to-track problems in non-KDE C++ code we're linking with (I think NMM) because of that. It happened that:&lt;br /&gt;
* libphonon loads the NMM plugin&lt;br /&gt;
* NMM plugin links to NMM&lt;br /&gt;
* NMM loads its own plugins&lt;br /&gt;
* NMM's own plugins link to NMM&lt;br /&gt;
&lt;br /&gt;
Some classes in the NMM library did not have well-anchored vtables, so dynamic_casting failed inside the Phonon NMM plugin for objects created in the NMM's own plugins.&lt;br /&gt;
&lt;br /&gt;
== Program Design ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some common problems related to the design of Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Delayed Initialization ===&lt;br /&gt;
&lt;br /&gt;
Although the design of modern C++ applications can be very complex, one recurring problem, which is generally easy to fix, is not using the technique of [http://www.kdedevelopers.org/node/view/509 delayed initialization]. &lt;br /&gt;
&lt;br /&gt;
First, let us look at the standard way of initializing a KDE application: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Notice that &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; is created before the &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; call that starts the event loop. This implies that we want to avoid doing anything non-trivial in the top-level constructor, since it runs before we can even show the window.&lt;br /&gt;
&lt;br /&gt;
The solution is simple: we need to delay the construction of anything besides the GUI until after the event loop has started. Here is how the example class MainWindow's constructor could look to achieve this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Construct your widgets here.  Note that the widgets you&lt;br /&gt;
     * construct here shouldn't require complex initialization&lt;br /&gt;
     * either, or you've defeated the purpose.&lt;br /&gt;
     * All you want to do is create your GUI objects and&lt;br /&gt;
     * QObject::connect&lt;br /&gt;
     * the appropriate signals to their slots.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* This slot will be called as soon as the event loop starts.&lt;br /&gt;
     * Put everything else that needs to be done, including&lt;br /&gt;
     * restoring values, reading files, session restoring, etc here.&lt;br /&gt;
     * It will still take time, but at least your window will be&lt;br /&gt;
     * on the screen, making your app look active.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
Using this technique may not buy you any overall time, but it makes your app ''seem'' quicker to the user who is starting it. This increased perceived responsiveness is reassuring for the user as they get quick feedback that the action of launching the app has succeeded.&lt;br /&gt;
&lt;br /&gt;
When (and only when) the start up can not be made reasonably fast enough, consider using a {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Data Structures ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some of our most common pet-peeves which affect data structures very commonly seen in Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Passing non-POD types ===&lt;br /&gt;
&lt;br /&gt;
Non-POD (&amp;quot;plain old data&amp;quot;) types should be passed by const reference if at all possible. This includes anything other than the basic types such as &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take, for instance, {{qt|QString}}. They should always be passed into methods as &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Even though {{qt|QString}} is implicitly shared it is still more efficient (and safer) to pass const references as opposed to objects by value. &lt;br /&gt;
&lt;br /&gt;
So the canonical signature of a method taking QString arguments is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void myMethod( const QString &amp;amp; foo, const QString &amp;amp; bar );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni</id>
		<title>User:Fresbeeplayer/Translate ErroriProgrammazioneComuni</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni"/>
				<updated>2008-11-27T00:10:14Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Errori di Programmazione Comuni|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == Abstract == --&amp;gt;&lt;br /&gt;
== Prefazione ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This tutorial aims to combine the experience of KDE developers regarding Qt and KDE frameworks dos and don'ts. Besides actual mistakes, it also covers things which are not necessarily &amp;quot;bugs&amp;quot; but which make the code either slower or less readable. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questo tutorial mira a combinare le esperienze degli sviluppatori KDE su cosa fare e cosa non fare in merito alle Qt ed al framework KDE. Oltre agli errori, vengono coperte anche cose che non sono necessariamente &amp;quot;bugs&amp;quot; ma che rendono il codice più lento e di difficile lettura.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- == General C++ == --&amp;gt;&lt;br /&gt;
== C++ in generale ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- This section guides you through some of the more dusty corners of C++ which either tend to be misused or which people often simply get wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questa sezione ti guida attraverso alcuni degli angoli più remoti del C++ che tendono ad essere usati male o dei quali la gente si sbaglia.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === Anonymous namespaces vs statics === --&amp;gt;&lt;br /&gt;
=== Namespace anonimi contro static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Se hai un metodo in una classe che non accede ad alcun membro e quindi non ha bisogno di un oggetto per funzionare, rendilo statico. Se in più è una funzione di supporto privata che non viene utilizzata all'esterno del file, rendila file-static. In questo modo essa viene nascosta completamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Le entità definite in un namespace anonimo in C++ non hanno linkage interni. I namespace anonimi offrono soltanto un nome unico per quella translation unit e basta; non cambiano in nessun modo il linkage dell'identificatore. Il linkage non viene cambiato perché la seconda delle due fasi di ricerca dei nomi ignora le funzioni con linkage interno. Per di più, le entità con linkage interno non possono essere usate come argomento di un template.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- So for now instead of using anonymous namespaces use static if you do not want a symbol to be exported. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A questo punto invece di usare namespace anonimi usa la parola chiave static se non vuoi che un simbolo venga esportato.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- === NULL pointer issues === --&amp;gt;&lt;br /&gt;
=== Problemi di puntatore Nullo ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- First and foremost: it is fine to delete a null pointer. So constructs like this that check for null before deleting are simply redundant: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prima di tutto: va bene eliminare un puntatore nullo. Quindi costruttori come il seguente che controllano che il valore sia nullo prima di eliminarlo sono semplicemente ridondanti:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Note however, that '''a null check ''is'' required when you delete an array''' - that's because a relatively recent compiler on Solaris does not handle it properly otherwise. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Da notare comunque, che '''un controllo per valore nullo ''è'' richiesto quando cancelli un array''' - questo perché altrimenti un compilatore relativamente recente per Solaris non lo gestisce opportunamente.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- When you delete a pointer, make sure you also set it to 0 so that future attempts to delete that object will not fail in a double delete. So the complete and proper idiom is: --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quando elimini un puntatore, assicurati anche di settarlo a 0 in modo che futuri tentativi di cancellazione non falliscano in una doppia eliminazione. Per cui il modo completo e corretto di procedere è: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- You may notice that null pointers are marked variously in one of three ways: 0, 0L and NULL. In C, NULL is defined as a null void pointer. However, in C++, this is not possible due to stricter type checking. Therefore, modern C++ implementations define it to a &amp;quot;magic&amp;quot; null pointer constant which can be assigned to any pointer. Older C++ implementations, OTOH, simply defined it to 0L or 0, which provides no additional type safety - one could assign it to an integer variable, which is obviously wrong. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Potresti notare che i puntatori nulli sono variamente indicati in uno di questi tre modi: 0, 0L e NULL. In C, NULL è definito come un puntatore nullo di tipo void. Ma in C++ ciò non è possibile a causa di un controllo di tipo più stretto. Perciò, moderne implementazioni del C++ lo rendono come un &amp;quot;magico&amp;quot; puntatore nullo costante il quale può essere assegnato a qualunque altro puntatore. D'altra parte le implementazioni più vecchie di C++ semplicemente lo associano a 0 o 0L, il quale non tiene conto di alcuna sicurezza di tipo - si potrebbe assegnarlo ad una variabile intera, ovviamente sbagliando.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- In pointer context, the integer constant zero means &amp;quot;null pointer&amp;quot; - irrespective of the actual binary representation of a null pointer. This means that the choice between 0, 0L and NULL is a question of personal style and getting used to something rather than a technical one - as far as the code in KDE's SVN goes you will see 0 used more commonly than NULL. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Nel contesto dei puntatori, la costante intera zero significa &amp;quot;puntatore nullo&amp;quot; - irrispettoso della rappresentazione binaria di un puntatore nullo. Ciò significa che la scelta tra 0, 0L e NULL è una questione di stile personale e abitudine piuttosto che tecnica - fintantoché nel codice SVN di KDE vedrai 0 usato più comunemente di NULL.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;!-- Note, however, that if you want to pass a null pointer constant to a function in a variable argument list, you *must* explicitly cast it to a pointer - the compiler assumes integer context by default, which might or might not match the binary representation of a pointer. Again, it does not matter whether you cast 0, 0L or NULL, but the shorter representation is generally preferred. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notare, comunque, che se vuoi passare un puntatore nullo costante ad una funzione nella lista delle variabili degli argomenti, *devi* esplicitamente farne il cast in un puntatore - il compilatore assume di default il contesto degli interi, il quale può o non può coincidere con la rappresentazione binaria di un puntatore. Di nuovo, non ha importanza il fatto che fai il cast a 0, 0L o NULL, ma la rappresentazione più corta è generalmente preferita.&lt;br /&gt;
&lt;br /&gt;
=== Member variables ===&lt;br /&gt;
&lt;br /&gt;
You will encounter four major styles of marking class member variables in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variable''' lowercase m, underscore and the name of the variable starting with a lowercase letter. This is the most common style and one prefered for code in kdelibs.&lt;br /&gt;
* '''mVariable''' lowercase m and the name of variable starting with a uppercase letter&lt;br /&gt;
* '''variable_''' variable name starting with a lowercase letter and then an underscore&lt;br /&gt;
* '''_variable''' underscore and the name of variable starting with a lowercase letter. This style is actually usually frowned upon as this notation is also used in some code for function parameters instead. &lt;br /&gt;
&lt;br /&gt;
As it often happens there is not one correct way of doing it, so remember to always follow the syntax used by the application/library to which you are committing.&lt;br /&gt;
&lt;br /&gt;
=== Static variables ===&lt;br /&gt;
&lt;br /&gt;
Try to limit the number of static variables used in your code, especially when committing to a library. Construction and initialization of large number of static variables really hurts the startup times.&lt;br /&gt;
&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction.&lt;br /&gt;
&lt;br /&gt;
Instead, use a static pointer, together with &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; which is defined in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; and is used like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globalA)&lt;br /&gt;
&lt;br /&gt;
void doSomething()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globalA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void doSomethingElse()&lt;br /&gt;
{&lt;br /&gt;
    if (globalA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globalA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globalA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See the [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 API documentation] for &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; for more information.&lt;br /&gt;
&lt;br /&gt;
=== Constant data ===&lt;br /&gt;
&lt;br /&gt;
If you need some constant data of simple data types in several places, you do good by defining it once at a central place, to avoid a mistype in one of the instances. If the data changes there is also only one place you need to edit.&lt;br /&gt;
&lt;br /&gt;
Even if there is only one instance you do good by defining it elsewhere, to avoid so-called &amp;quot;magic numbers&amp;quot; in the code which are unexplained (cmp. 42). Usually this is done at the top of a file to avoid searching for it.&lt;br /&gt;
&lt;br /&gt;
Define the constant data using the language constructs of C++, not the preprocessor instructions, like you may be used to from plain C. This way the compiler can help you to find mistakes by doing type checking.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const int AnswerToAllQuestions = 42;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define AnswerToAllQuestions 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const char SomeString[] = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
static const char* SomeString = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define SomeString &amp;quot;Example&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Forward Declarations ===&lt;br /&gt;
&lt;br /&gt;
You will reduce compile times by forward declaring classes when possible instead of including their respective headers. For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // slow&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // slow&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // slow&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;  &lt;br /&gt;
  &lt;br /&gt;
The above should instead be written like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // fast&lt;br /&gt;
class QStringList; // fast&lt;br /&gt;
class QString;     // fast&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iterators ===&lt;br /&gt;
&lt;br /&gt;
==== Prefer const iterators and cache end() ====&lt;br /&gt;
Prefer to use &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; over normal iterators when possible. Containers, which are being implicitly shared often detach when a call to a non-const &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; methods is made ({{qt|QList}} is an example of such a container). When using a const_iterator also watch out that you are really calling the const version of &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;. Unless your container is actually const itself this probably will not be the case, possibly causing an unnecessary detach of your container. So basically whenever you use const_iterator initialize them using &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt; instead, to be on the safe side. &lt;br /&gt;
&lt;br /&gt;
Cache the return of the &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) method call before doing iteration over large containers. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;SomeClass&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//code which inserts a large number of elements to the container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This avoids the unnecessary creation of the temporary &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) return object on each loop iteration, largely speeding it up.&lt;br /&gt;
&lt;br /&gt;
Prefer to use pre-increment over post-increment operators on iterators as this avoids creating an unnecessary temporary object in the process.&lt;br /&gt;
&lt;br /&gt;
====Take care when erasing elements inside a loop====&lt;br /&gt;
&lt;br /&gt;
When you want to erase some elements from the list, you maybe would use code similar to this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code will potentially crash because it is a dangling iterator after the call to erase().&lt;br /&gt;
You have to rewrite the code this way:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This problem is also discussed in the [http://doc.trolltech.com/4.3/qmap-iterator.html#details Qt documentation for QMap::iterator] but applies to '''all''' Qt iterators&lt;br /&gt;
&lt;br /&gt;
=== memory leaks ===&lt;br /&gt;
&lt;br /&gt;
A very &amp;quot;popular&amp;quot; programming mistake is to do a &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; without a &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; like in this program:&lt;br /&gt;
&lt;br /&gt;
'''mem_gourmet.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void pollute()&lt;br /&gt;
{&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) pollute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You see, ''pollute()'' instanciates a new object ''polluter'' of the class ''t''. Then, the variable ''polluter'' is lost because it is local, but the content (the object) stays on the heap. I could use this program to render my computer unusable within 10 seconds.&lt;br /&gt;
&lt;br /&gt;
To solve this, there are the following approaches:&lt;br /&gt;
* keep the variable on the stack instead of the heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
would become&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t polluter();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* delete polluter using the complementing function to new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete polluter;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A tool to detect memory leaks like this is [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
You can only dynamic_cast to type T from type T2 provided &lt;br /&gt;
that:&lt;br /&gt;
&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported&lt;br /&gt;
&lt;br /&gt;
For instance, we've seen some hard-to-track problems in non-KDE C++ code we're linking with (I think NMM) because of that. It happened that:&lt;br /&gt;
* libphonon loads the NMM plugin&lt;br /&gt;
* NMM plugin links to NMM&lt;br /&gt;
* NMM loads its own plugins&lt;br /&gt;
* NMM's own plugins link to NMM&lt;br /&gt;
&lt;br /&gt;
Some classes in the NMM library did not have well-anchored vtables, so dynamic_casting failed inside the Phonon NMM plugin for objects created in the NMM's own plugins.&lt;br /&gt;
&lt;br /&gt;
== Program Design ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some common problems related to the design of Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Delayed Initialization ===&lt;br /&gt;
&lt;br /&gt;
Although the design of modern C++ applications can be very complex, one recurring problem, which is generally easy to fix, is not using the technique of [http://www.kdedevelopers.org/node/view/509 delayed initialization]. &lt;br /&gt;
&lt;br /&gt;
First, let us look at the standard way of initializing a KDE application: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Notice that &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; is created before the &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; call that starts the event loop. This implies that we want to avoid doing anything non-trivial in the top-level constructor, since it runs before we can even show the window.&lt;br /&gt;
&lt;br /&gt;
The solution is simple: we need to delay the construction of anything besides the GUI until after the event loop has started. Here is how the example class MainWindow's constructor could look to achieve this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Construct your widgets here.  Note that the widgets you&lt;br /&gt;
     * construct here shouldn't require complex initialization&lt;br /&gt;
     * either, or you've defeated the purpose.&lt;br /&gt;
     * All you want to do is create your GUI objects and&lt;br /&gt;
     * QObject::connect&lt;br /&gt;
     * the appropriate signals to their slots.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* This slot will be called as soon as the event loop starts.&lt;br /&gt;
     * Put everything else that needs to be done, including&lt;br /&gt;
     * restoring values, reading files, session restoring, etc here.&lt;br /&gt;
     * It will still take time, but at least your window will be&lt;br /&gt;
     * on the screen, making your app look active.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
Using this technique may not buy you any overall time, but it makes your app ''seem'' quicker to the user who is starting it. This increased perceived responsiveness is reassuring for the user as they get quick feedback that the action of launching the app has succeeded.&lt;br /&gt;
&lt;br /&gt;
When (and only when) the start up can not be made reasonably fast enough, consider using a {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Data Structures ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some of our most common pet-peeves which affect data structures very commonly seen in Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Passing non-POD types ===&lt;br /&gt;
&lt;br /&gt;
Non-POD (&amp;quot;plain old data&amp;quot;) types should be passed by const reference if at all possible. This includes anything other than the basic types such as &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take, for instance, {{qt|QString}}. They should always be passed into methods as &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Even though {{qt|QString}} is implicitly shared it is still more efficient (and safer) to pass const references as opposed to objects by value. &lt;br /&gt;
&lt;br /&gt;
So the canonical signature of a method taking QString arguments is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void myMethod( const QString &amp;amp; foo, const QString &amp;amp; bar );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-26T21:46:50Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Tutorial translating: English -&amp;gt; Italian&lt;br /&gt;
&lt;br /&gt;
Working in progress:&amp;lt;br/&amp;gt;&lt;br /&gt;
[[User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/Help:Contribute_(it)</id>
		<title>Help:Contribute (it)</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/Help:Contribute_(it)"/>
				<updated>2008-11-26T21:20:33Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: cambiato il link a wikipedia in modo che punti alla sezione di editing in lingua italiana&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; &lt;br /&gt;
{{Template:I18n/Language Navigation Bar|Help:Contribute}}&lt;br /&gt;
&lt;br /&gt;
Il tuo contributo al wiki di KDE TechBase è ben accetto. Per ottenere contenuti e articoli di alta qualità ci sono un po' di linee guida che devi rispettare. Questa è una breve introduzione a come modificare e contribuire al wiki.&lt;br /&gt;
&lt;br /&gt;
== I contenuti di KDE TechBase ==&lt;br /&gt;
&lt;br /&gt;
Il wiki di KDE TechBase è pensato per contenere informazioni tecniche relative a KDE, ad esempio:&lt;br /&gt;
* informazioni per gli sviluppatori (ad esempio [[Development/Tutorials_(it)|tutorial]])&lt;br /&gt;
* [[KDE System Administration|amministrazione di sistema con KDE]]&lt;br /&gt;
* regole e piani di lavoro&lt;br /&gt;
* eccetera&lt;br /&gt;
&lt;br /&gt;
Ulteriori dettagli possono essere trovati nell'articolo sulla [[Help:Wiki Structure|struttura del wiki]].&lt;br /&gt;
&lt;br /&gt;
[http://docs.kde.org Documentazione per l'utente] o [http://api.kde.org documentazione delle API] ''non'' vanno aggiunte qui. Per questioni non tecniche usare [http://wiki.kde.org il wiki di KDE].&lt;br /&gt;
&lt;br /&gt;
=== Dove inserire i nuovi articoli ===&lt;br /&gt;
&lt;br /&gt;
Il wiki di KDE TechBase usa sottopagine. Dai una rapida occhiata all'articolo riguardo la [[Help:Wiki Structure|struttura del wiki]]. Per farla breve: non aggiungere a caso pagine nel livello più alto.&lt;br /&gt;
&lt;br /&gt;
È possibile tradurre articoli di KDE TechBase in altre lingue. Leggi l'articolo riguardo la [[Help:Wiki Translation_(it)|traduzione del wiki]] per ulteriori dettagli.&lt;br /&gt;
&lt;br /&gt;
=== La procedura ===&lt;br /&gt;
&lt;br /&gt;
Vuoi aggiungere nuovo contenuto. Per mantenere alto lo standard di qualità per favore prima crea la pagina dell'articolo nella tua pagina personale (ad esempio User:foo/My Article). Una volta che è pronta, discuti l'articolo con altri sviluppatori e ricontrollala. Quando hai trovato il posto adatto dove inserirla sposta la pagina.&lt;br /&gt;
&lt;br /&gt;
== Le basi della modifica ==&lt;br /&gt;
&lt;br /&gt;
Nel wiki di KDE TechBase tutti gli utenti registrati possono modificare il contenuto e la struttura.&lt;br /&gt;
&lt;br /&gt;
=== Regole di revisione e convenzioni ===&lt;br /&gt;
&lt;br /&gt;
Assicurati che l'informazione che aggiungi sia rilevante al proposito specifico del wiki, in caso contrario i tuoi contenuti potrebbero essere rimossi. Puoi sempre usare le [[Help:Talk page|pagine  di ''Discussione'' o ''Talk'']] per fare domande o assicurarti che la tua idea venga accettata. Per favore assicurati anche che i tuoi contributi non violino alcuna licenza.&lt;br /&gt;
&lt;br /&gt;
=== Cominciare a modificare ===&lt;br /&gt;
&lt;br /&gt;
Per cominciare a modificare una pagina di [[Main Page|KDE TechBase]] clicca su '''Modifica''' nella parte superiore del wiki. Verrai portato alla pagina di modifica: qui avrai a disposizione un'area di testo contenente il ''wikitext'' ovvero il codice modificabile da cui il server produce poi la pagina finale visibile all'utente. ''Se vuoi solo fare esperimenti puoi esercitarti nella [[Sandbox|sandbox]], non farlo qui''.&lt;br /&gt;
&lt;br /&gt;
=== Inserire le modifiche ===&lt;br /&gt;
&lt;br /&gt;
Per cominciare è sufficiente inserire il proprio testo. Per migliorare l'aspetto e l'ordine è necessario usare gli appositi comandi wiki, ad esempio per creare dei link o dare un'impaginazione migliore. Per favore usa lo stile utilizzato anche negli altri articoli del wiki. Se segui queste semplici regole i tuoi contributi saranno più utili in quanto non richiederanno successive ripuliture.&lt;br /&gt;
&lt;br /&gt;
=== Riassumere le modifiche ===&lt;br /&gt;
&lt;br /&gt;
Scrivi un breve riassunto nel piccolo campo sotto l'area di testo, ad esempio &amp;quot;Corretto un errore di ortografia.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Anteprima prima di salvare ===&lt;br /&gt;
&lt;br /&gt;
Quando hai finito clicca su '''Visualizza anteprima''' per vedere come si presenteranno le tue modifiche '''prima''' di renderle permanenti. Ripeti il processo di modifica e anteprima finchè non sei soddisfatto, poi clicca su '''Salva la pagina''' e le tue modifiche saranno immediatamente applicate all'articolo.&lt;br /&gt;
&lt;br /&gt;
== Pagina dei comandi Wiki ==&lt;br /&gt;
&lt;br /&gt;
La wikipedia fornisce una rapida introduzione alla principale sintassi mediawiki. Per favore leggi l'apposita [http://it.wikipedia.org/wiki/Aiuto:Markup pagina di aiuto].&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer</id>
		<title>User:Fresbeeplayer</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer"/>
				<updated>2008-11-26T21:11:15Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: New page: Tutorial translating: English -&amp;gt; Italian  Working in progress: User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Tutorial translating: English -&amp;gt; Italian&lt;br /&gt;
&lt;br /&gt;
Working in progress:&lt;br /&gt;
[[User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni]]&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	<entry>
		<id>http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni</id>
		<title>User:Fresbeeplayer/Translate ErroriProgrammazioneComuni</title>
		<link rel="alternate" type="text/html" href="http://techbase.kde.org/User:Fresbeeplayer/Translate_ErroriProgrammazioneComuni"/>
				<updated>2008-11-26T20:33:39Z</updated>
		
		<summary type="html">&lt;p&gt;Fresbeeplayer: Translate in progress...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:I18n/Language Navigation Bar|Development/Tutorials/Common Programming Mistakes}}&lt;br /&gt;
&lt;br /&gt;
{{TutorialBrowser|&lt;br /&gt;
&lt;br /&gt;
series=Getting Started|&lt;br /&gt;
&lt;br /&gt;
name=Common Programming Mistakes|&lt;br /&gt;
&lt;br /&gt;
reading=[[Policies/API_to_Avoid|APIs to avoid]]&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Abstract ==&lt;br /&gt;
&lt;br /&gt;
This tutorial aims to combine the experience of KDE developers regarding Qt and KDE frameworks dos and don'ts. Besides actual mistakes, it also covers things which are not necessarily &amp;quot;bugs&amp;quot; but which make the code either slower or less readable.&lt;br /&gt;
&lt;br /&gt;
== General C++ ==&lt;br /&gt;
&lt;br /&gt;
This section guides you through some of the more dusty corners of C++ which either tend to be misused or which people often simply get wrong.&lt;br /&gt;
&lt;br /&gt;
=== Anonymous namespaces vs statics ===&lt;br /&gt;
&lt;br /&gt;
If you have a method in a class that does not access any members and therefore does not need an object to operate, make it static. If additionally it is a private helper function that is not needed outside of the file, make it a file-static function. That hides the symbol completely.&lt;br /&gt;
&lt;br /&gt;
Symbols defined in a C++ anonymous namespace do not have internal linkage. Anonymous namespaces only give a unique name for that translation unit and that is it; they do not change the linkage of the symbol at all. Linkage is not changed on those because the second phase of two-phase name lookup ignores functions with internal linkages. Also, entities with internal linkage cannot be used as template arguments. &lt;br /&gt;
&lt;br /&gt;
So for now instead of using anonymous namespaces use static if you do not want a symbol to be exported.&lt;br /&gt;
&lt;br /&gt;
=== NULL pointer issues ===&lt;br /&gt;
&lt;br /&gt;
First and foremost: it is fine to delete a null pointer. So constructs like this that check for null before deleting are simply redundant: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
if ( ptr ) {&lt;br /&gt;
   delete ptr;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note however, that '''a null check ''is'' required when you delete an array''' - that's because a relatively recent compiler on Solaris does not handle it properly otherwise.&lt;br /&gt;
&lt;br /&gt;
When you delete a pointer, make sure you also set it to 0 so that future attempts to delete that object will not fail in a double delete. So the complete and proper idiom is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
delete ptr; &lt;br /&gt;
ptr = 0;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may notice that null pointers are marked variously in one of three ways: 0, 0L and NULL. In C, NULL is defined as a null void pointer. However, in C++, this is not possible due to stricter type checking. Therefore, modern C++ implementations define it to a &amp;quot;magic&amp;quot; null pointer constant which can be assigned to any pointer. Older C++ implementations, OTOH, simply defined it to 0L or 0, which provides no additional type safety - one could assign it to an integer variable, which is obviously wrong.&lt;br /&gt;
&lt;br /&gt;
In pointer context, the integer constant zero means &amp;quot;null pointer&amp;quot; - irrespective of the actual binary representation of a null pointer. This means that the choice between 0, 0L and NULL is a question of personal style and getting used to something rather than a technical one - as far as the code in KDE's SVN goes you will see 0 used more commonly than NULL.&lt;br /&gt;
 &lt;br /&gt;
Note, however, that if you want to pass a null pointer constant to a function in a variable argument list, you *must* explicitly cast it to a pointer - the compiler assumes integer context by default, which might or might not match the binary representation of a pointer. Again, it does not matter whether you cast 0, 0L or NULL, but the shorter representation is generally preferred.&lt;br /&gt;
&lt;br /&gt;
=== Member variables ===&lt;br /&gt;
&lt;br /&gt;
You will encounter four major styles of marking class member variables in KDE:&lt;br /&gt;
&lt;br /&gt;
* '''m_variable''' lowercase m, underscore and the name of the variable starting with a lowercase letter. This is the most common style and one prefered for code in kdelibs.&lt;br /&gt;
* '''mVariable''' lowercase m and the name of variable starting with a uppercase letter&lt;br /&gt;
* '''variable_''' variable name starting with a lowercase letter and then an underscore&lt;br /&gt;
* '''_variable''' underscore and the name of variable starting with a lowercase letter. This style is actually usually frowned upon as this notation is also used in some code for function parameters instead. &lt;br /&gt;
&lt;br /&gt;
As it often happens there is not one correct way of doing it, so remember to always follow the syntax used by the application/library to which you are committing.&lt;br /&gt;
&lt;br /&gt;
=== Static variables ===&lt;br /&gt;
&lt;br /&gt;
Try to limit the number of static variables used in your code, especially when committing to a library. Construction and initialization of large number of static variables really hurts the startup times.&lt;br /&gt;
&lt;br /&gt;
Do not use class-static variables, especially not in libraries and loadable modules though it is even discouraged in applications. Static objects lead to lots of problems such as hard to debug crashes due to undefined order of construction/destruction.&lt;br /&gt;
&lt;br /&gt;
Instead, use a static pointer, together with &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; which is defined in &amp;lt;tt&amp;gt;kglobal.h&amp;lt;/tt&amp;gt; and is used like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class A { ... };&lt;br /&gt;
&lt;br /&gt;
K_GLOBAL_STATIC(A, globalA)&lt;br /&gt;
&lt;br /&gt;
void doSomething()&lt;br /&gt;
{&lt;br /&gt;
     A *a = globalA;&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void doSomethingElse()&lt;br /&gt;
{&lt;br /&gt;
    if (globalA.isDestroyed()) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    A *a = globalA;&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void installPostRoutine()&lt;br /&gt;
{&lt;br /&gt;
    qAddPostRoutine(globalA.destroy);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See the [http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/kdecore/html/kglobal_8h.html#75ca0c60b03dc5e4f9427263bf4043c7 API documentation] for &amp;lt;tt&amp;gt;K_GLOBAL_STATIC&amp;lt;/tt&amp;gt; for more information.&lt;br /&gt;
&lt;br /&gt;
=== Constant data ===&lt;br /&gt;
&lt;br /&gt;
If you need some constant data of simple data types in several places, you do good by defining it once at a central place, to avoid a mistype in one of the instances. If the data changes there is also only one place you need to edit.&lt;br /&gt;
&lt;br /&gt;
Even if there is only one instance you do good by defining it elsewhere, to avoid so-called &amp;quot;magic numbers&amp;quot; in the code which are unexplained (cmp. 42). Usually this is done at the top of a file to avoid searching for it.&lt;br /&gt;
&lt;br /&gt;
Define the constant data using the language constructs of C++, not the preprocessor instructions, like you may be used to from plain C. This way the compiler can help you to find mistakes by doing type checking.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const int AnswerToAllQuestions = 42;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define AnswerToAllQuestions 42&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If defining a constant array do not use a pointer as data type. Instead use the data type and append the array symbol with undefined length, &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;, behind the name. Otherwise you also define a variable to some const data. That variable could mistakenly be assigned a new pointer to, without the compiler complaining about. And accessing the array would have one indirection, because first the value of the variable needs to be read.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct!&lt;br /&gt;
static const char SomeString[] = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
static const char* SomeString = &amp;quot;Example&amp;quot;;&lt;br /&gt;
// Wrong!&lt;br /&gt;
#define SomeString &amp;quot;Example&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Forward Declarations ===&lt;br /&gt;
&lt;br /&gt;
You will reduce compile times by forward declaring classes when possible instead of including their respective headers. For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;     // slow&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt; // slow&lt;br /&gt;
#include &amp;lt;QString&amp;gt;     // slow&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;  &lt;br /&gt;
  &lt;br /&gt;
The above should instead be written like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class QWidget;     // fast&lt;br /&gt;
class QStringList; // fast&lt;br /&gt;
class QString;     // fast&lt;br /&gt;
class SomeInterface&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    virtual void widgetAction( QWidget *widget ) =0;&lt;br /&gt;
    virtual void stringAction( const QString&amp;amp; str ) =0;&lt;br /&gt;
    virtual void stringListAction( const QStringList&amp;amp; strList ) =0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Iterators ===&lt;br /&gt;
&lt;br /&gt;
==== Prefer const iterators and cache end() ====&lt;br /&gt;
Prefer to use &amp;lt;tt&amp;gt;const_iterators&amp;lt;/tt&amp;gt; over normal iterators when possible. Containers, which are being implicitly shared often detach when a call to a non-const &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; methods is made ({{qt|QList}} is an example of such a container). When using a const_iterator also watch out that you are really calling the const version of &amp;lt;tt&amp;gt;begin()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt;. Unless your container is actually const itself this probably will not be the case, possibly causing an unnecessary detach of your container. So basically whenever you use const_iterator initialize them using &amp;lt;tt&amp;gt;constBegin()&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt; instead, to be on the safe side. &lt;br /&gt;
&lt;br /&gt;
Cache the return of the &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) method call before doing iteration over large containers. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QValueList&amp;lt;SomeClass&amp;gt; container;&lt;br /&gt;
&lt;br /&gt;
//code which inserts a large number of elements to the container&lt;br /&gt;
&lt;br /&gt;
QValueListConstIterator end( container.constEnd() );&lt;br /&gt;
&lt;br /&gt;
for ( QValueListConstIterator itr( container.constBegin() );&lt;br /&gt;
     itr != end; ++itr ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This avoids the unnecessary creation of the temporary &amp;lt;tt&amp;gt;end()&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;constEnd()&amp;lt;/tt&amp;gt;) return object on each loop iteration, largely speeding it up.&lt;br /&gt;
&lt;br /&gt;
Prefer to use pre-increment over post-increment operators on iterators as this avoids creating an unnecessary temporary object in the process.&lt;br /&gt;
&lt;br /&gt;
====Take care when erasing elements inside a loop====&lt;br /&gt;
&lt;br /&gt;
When you want to erase some elements from the list, you maybe would use code similar to this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator itEnd = m_activeTimers.end();&lt;br /&gt;
&lt;br /&gt;
for( ; it!=itEnd ; ++it )&lt;br /&gt;
{&lt;br /&gt;
    if(it.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(it.key());&lt;br /&gt;
        m_activeTimers.erase(it);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code will potentially crash because it is a dangling iterator after the call to erase().&lt;br /&gt;
You have to rewrite the code this way:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QMap&amp;lt;int, Job *&amp;gt;::iterator it = m_activeTimers.begin();&lt;br /&gt;
while (it != m_activeTimers.end())&lt;br /&gt;
{&lt;br /&gt;
    QMap&amp;lt;int, Job *&amp;gt;::iterator prev = it;&lt;br /&gt;
    ++it;&lt;br /&gt;
    if(prev.value() == job)&lt;br /&gt;
    {&lt;br /&gt;
        //A timer for this job has been found. Let's stop it.&lt;br /&gt;
        killTimer(prev.key());&lt;br /&gt;
        m_activeTimers.erase(prev);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This problem is also discussed in the [http://doc.trolltech.com/4.3/qmap-iterator.html#details Qt documentation for QMap::iterator] but applies to '''all''' Qt iterators&lt;br /&gt;
&lt;br /&gt;
=== memory leaks ===&lt;br /&gt;
&lt;br /&gt;
A very &amp;quot;popular&amp;quot; programming mistake is to do a &amp;lt;tt&amp;gt;new&amp;lt;/tt&amp;gt; without a &amp;lt;tt&amp;gt;delete&amp;lt;/tt&amp;gt; like in this program:&lt;br /&gt;
&lt;br /&gt;
'''mem_gourmet.cpp'''&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
class t&lt;br /&gt;
{&lt;br /&gt;
  public:&lt;br /&gt;
    t() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void pollute()&lt;br /&gt;
{&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  while (true) pollute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You see, ''pollute()'' instanciates a new object ''polluter'' of the class ''t''. Then, the variable ''polluter'' is lost because it is local, but the content (the object) stays on the heap. I could use this program to render my computer unusable within 10 seconds.&lt;br /&gt;
&lt;br /&gt;
To solve this, there are the following approaches:&lt;br /&gt;
* keep the variable on the stack instead of the heap:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
  t* polluter = new t();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
would become&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  t polluter();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* delete polluter using the complementing function to new:&lt;br /&gt;
&amp;lt;code cppqt&amp;gt; &lt;br /&gt;
  delete polluter;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A tool to detect memory leaks like this is [[Development/Tools/Valgrind|Valgrind]].&lt;br /&gt;
=== dynamic_cast ===&lt;br /&gt;
&lt;br /&gt;
You can only dynamic_cast to type T from type T2 provided &lt;br /&gt;
that:&lt;br /&gt;
&lt;br /&gt;
* T is defined in a library you link to (you'd get a linker error if this isn't the case, since it won't find &lt;br /&gt;
the vtable or RTTI info)&lt;br /&gt;
* T is &amp;quot;well-anchored&amp;quot; in that library. By &amp;quot;well-anchored&amp;quot; I mean that the vtable is not a COMMON symbol subject to merging at run-time by the dynamic linker. In other words, the first virtual member in the class definition must exist and not be inlined: it must be in a .cpp file.&lt;br /&gt;
* T and T2 are exported&lt;br /&gt;
&lt;br /&gt;
For instance, we've seen some hard-to-track problems in non-KDE C++ code we're linking with (I think NMM) because of that. It happened that:&lt;br /&gt;
* libphonon loads the NMM plugin&lt;br /&gt;
* NMM plugin links to NMM&lt;br /&gt;
* NMM loads its own plugins&lt;br /&gt;
* NMM's own plugins link to NMM&lt;br /&gt;
&lt;br /&gt;
Some classes in the NMM library did not have well-anchored vtables, so dynamic_casting failed inside the Phonon NMM plugin for objects created in the NMM's own plugins.&lt;br /&gt;
&lt;br /&gt;
== Program Design ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some common problems related to the design of Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Delayed Initialization ===&lt;br /&gt;
&lt;br /&gt;
Although the design of modern C++ applications can be very complex, one recurring problem, which is generally easy to fix, is not using the technique of [http://www.kdedevelopers.org/node/view/509 delayed initialization]. &lt;br /&gt;
&lt;br /&gt;
First, let us look at the standard way of initializing a KDE application: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
    ....&lt;br /&gt;
    KApplication a;&lt;br /&gt;
&lt;br /&gt;
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();&lt;br /&gt;
&lt;br /&gt;
    MainWindow *window = new MainWindow( args );&lt;br /&gt;
&lt;br /&gt;
    a.setMainWidget( window );&lt;br /&gt;
    window-&amp;gt;show();&lt;br /&gt;
&lt;br /&gt;
    return a.exec();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Notice that &amp;lt;tt&amp;gt;window&amp;lt;/tt&amp;gt; is created before the &amp;lt;tt&amp;gt;a.exec()&amp;lt;/tt&amp;gt; call that starts the event loop. This implies that we want to avoid doing anything non-trivial in the top-level constructor, since it runs before we can even show the window.&lt;br /&gt;
&lt;br /&gt;
The solution is simple: we need to delay the construction of anything besides the GUI until after the event loop has started. Here is how the example class MainWindow's constructor could look to achieve this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
MainWindow::MainWindow()&lt;br /&gt;
{&lt;br /&gt;
    initGUI();&lt;br /&gt;
    QTimer::singleShot( 0, this, SLOT(initObject()) );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initGUI()&lt;br /&gt;
{&lt;br /&gt;
    /* Construct your widgets here.  Note that the widgets you&lt;br /&gt;
     * construct here shouldn't require complex initialization&lt;br /&gt;
     * either, or you've defeated the purpose.&lt;br /&gt;
     * All you want to do is create your GUI objects and&lt;br /&gt;
     * QObject::connect&lt;br /&gt;
     * the appropriate signals to their slots.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MainWindow::initObject()&lt;br /&gt;
{&lt;br /&gt;
    /* This slot will be called as soon as the event loop starts.&lt;br /&gt;
     * Put everything else that needs to be done, including&lt;br /&gt;
     * restoring values, reading files, session restoring, etc here.&lt;br /&gt;
     * It will still take time, but at least your window will be&lt;br /&gt;
     * on the screen, making your app look active.&lt;br /&gt;
     */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
Using this technique may not buy you any overall time, but it makes your app ''seem'' quicker to the user who is starting it. This increased perceived responsiveness is reassuring for the user as they get quick feedback that the action of launching the app has succeeded.&lt;br /&gt;
&lt;br /&gt;
When (and only when) the start up can not be made reasonably fast enough, consider using a {{class|KSplashScreen}}.&lt;br /&gt;
&lt;br /&gt;
== Data Structures ==&lt;br /&gt;
&lt;br /&gt;
In this section we will go over some of our most common pet-peeves which affect data structures very commonly seen in Qt/KDE applications.&lt;br /&gt;
&lt;br /&gt;
=== Passing non-POD types ===&lt;br /&gt;
&lt;br /&gt;
Non-POD (&amp;quot;plain old data&amp;quot;) types should be passed by const reference if at all possible. This includes anything other than the basic types such as &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take, for instance, {{qt|QString}}. They should always be passed into methods as &amp;lt;tt&amp;gt;const {{qt|QString}}&amp;amp;&amp;lt;/tt&amp;gt;. Even though {{qt|QString}} is implicitly shared it is still more efficient (and safer) to pass const references as opposed to objects by value. &lt;br /&gt;
&lt;br /&gt;
So the canonical signature of a method taking QString arguments is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
void myMethod( const QString &amp;amp; foo, const QString &amp;amp; bar );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QObject ===&lt;br /&gt;
&lt;br /&gt;
If you ever need to delete a QObject derived class from within one of its own methods, do not ever delete it this way: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
   delete this;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will sooner or later cause a crash because a method on that object might be invoked from the Qt event loop via slots/signals after you deleted it.&lt;br /&gt;
&lt;br /&gt;
Instead always use &amp;lt;tt&amp;gt;{{QtMethod|QObject|deleteLater}}&amp;lt;/tt&amp;gt; which tries to do the same thing as &amp;lt;tt&amp;gt;delete this&amp;lt;/tt&amp;gt; but in a safer way.&lt;br /&gt;
&lt;br /&gt;
=== Empty QStrings ===&lt;br /&gt;
&lt;br /&gt;
It is common to want to see if a {{qt|QString}} is empty. Here are three ways of doing it, the first two of which are correct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring.isEmpty() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Correct&lt;br /&gt;
if ( mystring == QString() ) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Wrong! &amp;quot;&amp;quot;&lt;br /&gt;
if ( mystring == &amp;quot;&amp;quot; ) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While there is a distinction between &amp;quot;null&amp;quot; {{qt|QString}}s and empty ones, this is a purely historical artifact and new code is discouraged from making use of it.&lt;br /&gt;
&lt;br /&gt;
=== QString and reading files ===&lt;br /&gt;
&lt;br /&gt;
If you are reading in a file, it is faster to convert it from the local encoding to Unicode ({{qt|QString}}) in one go, rather than line by line. This means that methods like &amp;lt;tt&amp;gt;{{qt|QIODevice}}::readAll()&amp;lt;/tt&amp;gt; are often a good solution, followed by a single {{qt|QString}} instantiation.&lt;br /&gt;
&lt;br /&gt;
For larger files, consider reading a block of lines and then performing the conversion. That way you get the opportunity to update your GUI. This can be accomplished by reentering the event loop normally, along with using a timer to read in the blocks in the background, or by creating a local event loop. &lt;br /&gt;
&lt;br /&gt;
While one can also use &amp;lt;tt&amp;gt;qApp-&amp;gt;processEvents()&amp;lt;/tt&amp;gt;, it is discouraged as it easily leads to subtle yet often fatal problems.&lt;br /&gt;
&lt;br /&gt;
=== Reading QString from a KProcess ===&lt;br /&gt;
&lt;br /&gt;
{{class|KProcess}} emits the signals &amp;lt;tt&amp;gt;readyReadStandard{Output|Error}&amp;lt;/tt&amp;gt; as data comes in.&lt;br /&gt;
A common mistake is reading all available data in the connected slot and converting it to {{qt|QString}} right away: the data comes in arbitrarily segmented chunks, so multi-byte characters might be cut into pieces and thus invalidated. Several approaches to this problem exist:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Do you really need to process the data as it comes in? If not, just use &amp;lt;tt&amp;gt;readAllStandard{Output|Error}&amp;lt;/tt&amp;gt; after the process has exited. Unlike in KDE3, KProcess is now able to accumulate the data for you.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Wrap the process into a {{qt|QTextStream}} and read line-wise. This should work starting with Qt 4.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Accumulate data chunks in the slots and process them each time a newline arrives or after some timeout passes. [http://websvn.kde.org/trunk/KDE/kdevplatform/util/processlinemaker.cpp?view=markup Example code]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QString and QByteArray ===&lt;br /&gt;
&lt;br /&gt;
While {{qt|QString}} is the tool of choice for many string handling situations, there is one where it is particularly inefficient. If you are pushing about and working on data in {{qt|QByteArray}}s, take care not to pass it through methods which take {{qt|QString}} parameters; then make QByteArrays from them again.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QString myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QString mangleData( const QString&amp;amp; data ) {&lt;br /&gt;
    QByteArray str = data.toLatin1();&lt;br /&gt;
    // mangle &lt;br /&gt;
    return QString(str);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The expensive thing happening here is the conversion to {{qt|QString}}, which does a conversion to Unicode internally. This is unnecessary because, the first thing the method does is convert it back using &amp;lt;tt&amp;gt;toLatin1()&amp;lt;/tt&amp;gt;. So if you are sure that the Unicode conversion is not needed, try to avoid inadvertently using QString along the way. &lt;br /&gt;
&lt;br /&gt;
The above example should instead be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
QByteArray myData;&lt;br /&gt;
QByteArray myNewData = mangleData( myData );&lt;br /&gt;
&lt;br /&gt;
QByteArray mangleData( const QByteArray&amp;amp; data )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== QDomElement ===&lt;br /&gt;
&lt;br /&gt;
When parsing XML documents, one often needs to iterate over all the elements. You may be tempted to use the following code for that: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomElement e = baseElement.firstChild().toElement();&lt;br /&gt;
      !e.isNull();&lt;br /&gt;
      e = e.nextSibling().toElement() ) {&lt;br /&gt;
       ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
That is not correct though: the above loop will stop prematurely when it encounters a {{qt|QDomNode}} that is something other than an element such as a comment.&lt;br /&gt;
&lt;br /&gt;
The correct loop looks like: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code cppqt&amp;gt;&lt;br /&gt;
for ( QDomNode n = baseElement.firstChild(); !n.isNull();&lt;br /&gt;
      n = n.nextSibling() ) {&lt;br /&gt;
    QDomElement e = n.toElement();&lt;br /&gt;
    if ( e.isNull() ) {&lt;br /&gt;
        continue;&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Fresbeeplayer</name></author>	</entry>

	</feed>