IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Color Editor Factory Example

This example shows how to create an editor that can be used by a https://.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Color Editor Factory Example▲

Image non disponible

When editing data in a https://, https://, or https://, editors are created and displayed by a https://. https://, which is the default delegate used by Qt's https://, uses a https:// to create editors for it. A unique instance provided by https:// is by default installed on all item delegates.

An item editor factory contains a collection of https:// instances, which are specialized factories that produce editors for one particular https:// data type (all models in Qt store their data in https://s). An editor can be any Qt or custom widget.

In this example, we will create an editor (implemented in the ColorListEditor class) that can edit the https:// data type and be used by https://s. We do this by creating a new https:// that produces ColorListEditors and register it with a new factory, which we set as the default editor item factory (the unique factory instance). To test our editor, we have implemented the Window class, which displays a https:// in which https://s can be edited.

Window Class Implementation▲

In the Window class, we create the item editor creator base for our color editor and add it to the default factory. We also create a https:// in which our editor can be tested. It is filled with some data and displayed in a window.

We take a closer look at the constructor:

 
Sélectionnez
Window::Window()
{
    QItemEditorFactory *factory = new QItemEditorFactory;

    QItemEditorCreatorBase *colorListCreator =
        new QStandardItemEditorCreator<ColorListEditor>();

    factory->registerEditor(QVariant::Color, colorListCreator);

    QItemEditorFactory::setDefaultFactory(factory);

    createGUI();
}

The https:// is a convenience class that inherits https://. Its constructor takes a template class, of which instances are returned from https://(). The creator uses a constructor that takes a https:// as its only parameter; the template class must provide this. This way, there is no need to subclass https://.

After the new factory has been set, all standard item delegates will use it (i.e, also delegates that were created before the new default factory was set).

The createGUI() function sets up the table and fills it with data.

ColorListEditor Definition▲

The ColorListEditor inherits https:// and lets the user select a https:// from its popup list.

 
Sélectionnez
class ColorListEditor : public QComboBox
{
    Q_OBJECT
    Q_PROPERTY(QColor color READ color WRITE setColor USER true)

public:
    ColorListEditor(QWidget *widget = 0);

public:
    QColor color() const;
    void setColor(QColor c);

private:
    void populateList();
};

https:// manages the interaction between the editor and the model, i.e., it retrieves data to edit from the model and store data from the editor in the model. The data that is edited by an editor is stored in the editor's user data property, and the delegate uses Qt's https:// to access it by name. We declare our user data property with the https:// macro. The property is set to be the user type with the USER keyword.

ColorListEditor Implementation▲

The constructor of ColorListEditor simply calls populateList(), which we will look at later. We move on to the color() function:

 
Sélectionnez
QColor ColorListEditor::color() const
{
    return qvariant_cast<QColor>(itemData(currentIndex(), Qt::DecorationRole));
}

We return the data that is selected in the combobox. The data is stored in the https:// as the color is then also displayed in the popup list (as shown in the image above).

 
Sélectionnez
void ColorListEditor::setColor(QColor color)
{
    setCurrentIndex(findData(color, int(Qt::DecorationRole)));
}

The findData() function searches the items in the combobox and returns the index of the item that has color in the Qt::Decoration role.

 
Sélectionnez
void ColorListEditor::populateList()
{
    QStringList colorNames = QColor::colorNames();

    for (int i = 0; i < colorNames.size(); ++i) {
        QColor color(colorNames[i]);

        insertItem(i, colorNames[i]);
        setItemData(i, color, Qt::DecorationRole);
    }
}

Qt knows some predefined colors by name. We simply loop through these to fill our editor with items.

Further Customization of Item View Editors▲

You can customize Qt's https:// in many ways. The procedure shown in this example is usually sufficient to provide custom editors. Further customization is achieved by subclassing https:// and https://. It is also possible to subclass https:// if you don't wish to use a factory at all.

Possible suggestions are:

  • If the editor widget has no user property defined, the delegate asks the factory for the property name, which it in turn asks the item editor creator for. In this case, you can use the https:// class, which takes the property name to use for editing as a constructor argument.

  • If the editor requires other constructors or other initialization than provided by https://, you must reimplement https://().

  • You could also subclass https:// if you only want to provide editors for certain kinds of data or use another method of creating the editors than using creator bases.

In this example, we use a standard https:// data type. You can also use custom types. In the https://, we show how to store a custom data type in a https:// and paint and edit it in a class that inherits https://.

List of Files▲

Files:

List of Files▲

  • itemviews/coloreditorfactory/coloreditorfactory.pro

  • itemviews/coloreditorfactory/colorlisteditor.cpp

  • itemviews/coloreditorfactory/colorlisteditor.h

  • itemviews/coloreditorfactory/main.cpp

  • itemviews/coloreditorfactory/window.cpp

  • itemviews/coloreditorfactory/window.h

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.