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  · 

UIFactory Class Reference
[QtBaseModule]

The UIFactory class provides a factory for various types of widgets used within the Qt Extended server. More...

    #include <UIFactory>

Static Public Members

  • QDialog * createDialog ( const QByteArray & dialogClassName, QWidget * parent = 0, Qt::WFlags f = 0 )
  • QWidget * createWidget ( const QByteArray & widgetClassName, QWidget * parent = 0, Qt::WFlags fl = 0 )
  • bool isAvailable ( const QByteArray & widgetClassName )

Macros


Detailed Description

The UIFactory class provides a factory for various types of widgets used within the Qt Extended server.

If a widget registers itself via UIFACTORY_REGISTER_WIDGET() an instance of that widget can be obtained via the UIFactory. The caller only refers to the class via its name without having to include the actual class definition for that widget. Thus by using this factory dependencies between various widget components within the server are reduced.

UIFactory vs. Qt Extended Server Widgets

The decision whether to use the UIFactory or the Qt Extended Server Widget mechanism depends on the use case. In general if UI components require extensive interfaces Qt Extended Server Widgets should be used. The following table gives a brief overview of the advantages and disadvantages of each concept:

UIFactoryServer Widgets
InterfacesInterface is defined by QDialog/QWidget.Abstract class interfaces such as QAbstractBrowserScreen are used.
FlexibilityThe caller is limited to functionality provided by QWidget and QDialog (some workarounds are possible and can be found here).The caller can access a range of methods exposed via the abstract interface.
IncludesNo includes required and therefore no additional dependencies created.Direct includes of abstract interface headers required which creates more dependencies.
MappingMaps a class name to an instance of that class.Maps an abstract interface name to an instance of that interface. More details can be found in the Server Widget documentation.
ReferencesEach call to UIFactory::createWidget() will create a new instance of the widget.Server widgets support the singleton pattern.

How to use UIFactory

The subsequent example defines and registers the new ExampleLabel widget.

    //in examplelabel.cpp
    class ExampleLabel : public QWidget
    {
        Q_OBJECT
    public:
        ExampleLabel( QWidget *parent = 0, Qt::WFlags fl = 0) {}
    };

    UIFACTORY_REGISTER_WIDGET( ExampleLabel );

Note that a widget must use the Q_OBJECT macro in order to be accessable via the UIFactory.

    //in exampleuser.cpp
    void ExampleUser::showExampleLabel()
    {
        QWidget *label = UIFactory::createWidget( "ExampleLabel", this, 0 );
        if ( label ) {
            label->show();
        }
    }

The caller accesses ExampleLabel via the interface provided by QWidget. Widgets such as ExampleLabel are usually delivered as part of a server component. If the component that provides ExampleLabel is not deployed as part of the server widget() returns a NULL pointer. Therefore it is always required to check the returned widget pointer for validity and it is the callers responsibility to handle the case of a non-existing ExampleLabel component.

Advanced usage

In some circumstances it may be required to access some additional methods or properties provided by a widget. A typical example would be a dialog that returns a more sophisticated error code than the integer value returned by QDialog::exec(). Nevertheless the caller would like to avoid having to include the dialog declaration.

The following SampleDialog demonstrates such an example. Usually SampleDialog::errorCode() would not be accessable via the QDialog interface.

    //in sampledialog.cpp
    class SampleDialog : public QDialog
    {
        Q_OBJECT
    public:
        SampleDialog( QWidget *parent = 0, Qt::WFlags fl = 0) {}

        QString notCallableMethod;
        Q_INVOKABLE QString errorCode();

    public slots:
        void setParamter( bool param1, int param2 );
    };
    //class definition for SampleDialog
    ...
    UIFACTORY_REGISTER_WIDGET( SampleDialog );

The following code demonstrates how other code can access SampleDialog methods via Qt's Meta-Object System.

    //in dialoguser.cpp
    void DialogUser::showSampleDialog()
    {
        QDialog *dlg = UIFactory::createDialog( "SampleDialog" );
        if ( dlg ) {
            QMetaObject::invokeMethod( dlg, "setParameter",
                    Qt::DirectConnection, Q_ARG(bool,true), Q_ARG(int,10) )
            QtopiaApplication::execDialog( dlg );

            QSlotInvoker returnCode( dlg, SLOT(errorCode()), 0 );
            QList<QVariant> args;
            QString returnString = returnCode->invoke( args ).toString();
            ...
        } else {
            qWarning("SampleDialog not available");
        }
    }

The meta system allows the invocation of slots or invokable methods via QMetaObject::invokeMethod(). QSlotInvoker is a convenience class with the same purpose. Its main difference is that the user doesn't need to know the exact type of the arguments. In general QMetaObject::invokeMethod() should be prefered over QSlotInvoker.

The only limitation of using Qt's meta system is that only slots and methods marked with the Q_INVOKABLE macros can be invoked via the meta system. In the case of the above SampleDialog only SampleDialog::errorCode() and SampleDialog::setParameter() can be invoked but not SampleDialog::notCallableMethod().

Note that accessing of slots and methods via Qt's Meta-Object System should only be used in limited cases. This approach is very error prone because programming error may only be discovered at runtime (and not at compile time). As soon as widgets are required that expose more sophisticated interfaces abstract server widgets should be considered.

See also QSlotInvoker and QtopiaApplication.


Member Function Documentation

QDialog * UIFactory::createDialog ( const QByteArray & dialogClassName, QWidget * parent = 0, Qt::WFlags f = 0 )   [static]

Returns a pointer to a new instance of dialogClassName. If no dialog with that name has been registered this function returns 0. The returned dialog is a child of parent, with widget flags set to f.

Each call to this function will create a new instance of dialogClassName. It is the callers responsibility to manage the life cycle of the returned dialog.

QWidget * UIFactory::createWidget ( const QByteArray & widgetClassName, QWidget * parent = 0, Qt::WFlags fl = 0 )   [static]

Returns a pointer to a new instance of widgetClassName. If no widget with that name has been registered this function returns 0. The returned widget is a child of parent, with widget flags set to fl.

Each call to this function will create a new instance of widgetClassName. It is the callers responsibility to manage the life cycle of the returned widget.

bool UIFactory::isAvailable ( const QByteArray & widgetClassName )   [static]

Returns true if widgetClassName is registered/available as a component that can be created via the UIFactory; otherwise false.

This function may be used to discover available widgets components at runtime and implement fall-back strategies if a particular widget is not available. Note that the list of widgets/components is created at static construction time. Therefore this function does not return reliable data before main() has been entered.


Macro Documentation

UIFACTORY_REGISTER_WIDGET ( ClassName )

Registers the widget ClassName so that other parts of the server can utilize the widget. The advantage of this macro is the fact that the user of ClassName must not include the class declaration for ClassName.

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 80
  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. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  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 6
Page suivante

Le Qt Developer Network au hasard

Logo

QML et les styles

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. 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