Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

notesdemo.cpp Example File
content/notesdemo/notesdemo.cpp

    /****************************************************************************
    **
    ** This file is part of the Qt Extended Commercial Package.
    **
    ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    **
    ** $QT_EXTENDED_DUAL_LICENSE$
    **
    ****************************************************************************/

    #include "notesdemo.h"
    #include <QStackedLayout>
    #include <QDocumentSelector>
    #include <QTextEdit>
    #include <QTextDocument>
    #include <QTimeString>
    #include <QtDebug>

    /*!
        \class NotesDemo
        \brief The NotesDemo application is a simple text editor demonstrating the use of a QDocumentSelector.

        It is comprised of a QDocumentSelector and a QTextEdit in a dialog with a stacked layout so only
        one widget is visible at a time.  The document selector is initially visible and selecting a document
        or creating a new document will display the document in the text editor.  Accepting or canceling the
        editor will return to the document selector, saving the document if it was accepted, and accepting or
        canceling the document selector will exit the application.
    */

    /*!
        Constructs a NotesDemo dialog which is a child of \a parent and has the given window
        \a flags.
     */
    NotesDemo::NotesDemo( QWidget *parent, Qt::WindowFlags flags )
        : QDialog( parent, flags )
    {
        // Create a new document selector which lists documents with the MIME type text/plain
        // sorted so the most recently edited documents appear first.
        documentSelector = new QDocumentSelector;

        documentSelector->setFilter( QContentFilter::mimeType( "text/plain" ) );
        documentSelector->setSortMode( QDocumentSelector::ReverseChronological );

        // Enable the new document option so a 'New' document selection appears at the start of the
        // documents list and in the context menu.
        documentSelector->enableOptions( QDocumentSelector::NewDocument );

        // Connect to the newSelected() and documentSelected() signal so we're notified when the user
        // selects a document.
        connect( documentSelector, SIGNAL(newSelected()),
                 this, SLOT(newDocument()) );
        connect( documentSelector, SIGNAL(documentSelected(QContent)),
                 this, SLOT(openDocument(QContent)) );

        // Construct the text editor widget.
        editor = new QTextEdit;

        // Create a new stacked layout and add the document selector and text editor widgets to it.
        // As the layout is given the dialog as a parent it is automatically set as the layout for
        // the dialog, and the widgets added to it are re-parented to the dialog.  The document
        // will be the initial widget shown as it was added to the layout first.
        layout = new QStackedLayout( this );

        layout->addWidget( documentSelector );
        layout->addWidget( editor );
    }

    /*!
        Creates a new text document and displays the text editor.
    */
    void NotesDemo::newDocument()
    {
        // Set the current document to a new QContent, and set it's name and type.
        // We're just using the time and date for the name but another application
        // may want to prompt the user for a name, or use the first bit of text in
        // the document.
        currentDocument = QContent();

        currentDocument.setName( "Note " + QTimeString::localYMDHMS( QDateTime::currentDateTime() ) );
        currentDocument.setType( "text/plain" );

        // Display the editor.
        layout->setCurrentWidget( editor );
    }

    /*!
        Opens a text \a document selected from the QDocumentSelector and displays the text editor.
    */
    void NotesDemo::openDocument( const QContent &document )
    {
        // Sets the current document to the one selected.
        currentDocument = document;

        // Read in the text from the document, if the read is successful display the text editor.
        if ( readContent( editor->document(), &currentDocument ) ) {
            layout->setCurrentWidget( editor );
        }
    }

    /*!
        Closes the currently displayed widget.  If that is editor widget and the \a result
        is QDialog::Accepted the changes made in the editor will be saved.

        Closing the editor widget will return the document selector to the display, and closing
        the document selector will close the application.
    */
    void NotesDemo::done( int result )
    {
        if ( layout->currentWidget() == editor ) {
            // The current widget is the editor so finish editing the document and return to
            // the document selector. If the dialog was accepted write the changes to the
            // document, and commit the document QContent.
            if ( result == QDialog::Accepted ) {
                if ( !writeContent(editor->document(), &currentDocument ) ) {
                    qWarning() << "Writing the content failed";
                } else if ( !currentDocument.commit() ) {
                    qWarning() << "Committing the new content failed";
                }
            }

            editor->document()->clear();

            layout->setCurrentWidget( documentSelector );
        } else {
            // The current widget is the document selector, so close the dialog and the application.
            QDialog::done( result );
        }
    }

    /*!
        Reads the contents of the text document \a content into \a document.

        Returns true if the read was successful and false otherwise.
    */
    bool NotesDemo::readContent( QTextDocument *document, QContent *content )
    {
        // Attempt to open the content in read-only mode.  If the open succeeds QContent
        // will construct a new I/O device and return a pointer to it, the caller takes
        // ownership of the I/O device and is responsible for deleting it.
        QIODevice *ioDevice = content->open( QIODevice::ReadOnly );
        if ( !ioDevice ) {
            qWarning() << "Could not open the new content object to read from!!";
            return false;
        }
        QByteArray bytes = ioDevice->readAll();

        // Convert the string from an 8-bit ASCII byte array and set it as the plain text
        // content of the document.
        document->setPlainText( QString::fromAscii( bytes ) );

        // Close the I/O device and destroy it.
        ioDevice->close();
        delete ioDevice;

        return true;
    }

    /*!
        Writes the contents of \a document to the text document \a content.  It is left up to the
        calling code to \l {QContent::commit()}{commit} the changes in \a content to the document
        system.

        Returns true if the write was successful and false otherwise.
    */
    bool NotesDemo::writeContent( QTextDocument *document, QContent *content )
    {
        // Attempt to open the content in write-only mode.  If the open succeeds QContent
        // will construct a new I/O device and return a pointer to it, the caller takes
        // ownership of the I/O device and is responsible for deleting it.
        QIODevice *ioDevice = content->open( QIODevice::WriteOnly );
        if ( !ioDevice ) {
            qWarning() << "Could not open the new content object to write to!!";
            return false;
        }

        // Gets the plain text content of the text document and converts it to an 8-bit
        // ASCII byte array before writing it to the I/O device.
        // (This assumes that the notes are short enough to fit into memory. For longer
        // documents, use QTextDocument::begin(), QTextDocument::end() and QTextDocument::findBlock(int).)
        int bytesWritten = ioDevice->write( document->toPlainText().toAscii() );

        // Close the I/O device and delete it.
        ioDevice->close();
        delete ioDevice;

        if ( bytesWritten < 0 ) {
            qWarning() << "Error while trying to create a new notes object!!";
            return false;
        } else {
            return true;
        }
    }

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 94
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 42
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 7
Page suivante

Le Qt Quarterly au hasard

Logo

Un plugin pour QImage

Qt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt qtextended4.4
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web