PDA

View Full Version : Adding an editor to the already existing list of QItemEditorFactory



schall_l
18th March 2009, 15:46
Hi,

the Color Editor Factory example from the QT Documentation shows how to use the ColorListEditor as the default editor for a data from type QVariant::Color.

I noticed that by using the code from this example, the already existing default editors for a variety of data types (bool, double, int, QString,....) are not available anymore as the new created factory will contain ONLY the editor for the type QVariant::Color.



QItemEditorFactory *factory = new QItemEditorFactory;
QItemEditorCreatorBase *colorListCreator = new QStandardItemEditorCreator<ColorListEditor>();
factory->registerEditor(QVariant::Color, colorListCreator);
QItemEditorFactory::setDefaultFactory(factory);



So I would like to know if this is the correct way to actually add the ColorListEditor to the already existing list of editors:



QItemEditorFactory *factory = (QItemEditorFactory *)QItemEditorFactory::defaultFactory();
QItemEditorCreatorBase *colorListCreator = new QStandardItemEditorCreator<ColorListEditor>();
factory->registerEditor(QVariant::Color, colorListCreator);
QItemEditorFactory::setDefaultFactory(factory);

wysota
19th March 2009, 13:39
Could you provide a minimal compilable example reproducing the problem?

schall_l
11th May 2009, 16:40
I would like to know if there is a possiblity to actually add the ColorListEditor to the already existing list of editors.

The color editor factory example provided with Qt is a compilable example.

In the example provided with Qt, if I do put the line ItemEditorFactory::setDefaultFactory(factory); from the function Window::Window() in comment I can edit the names (Alice, Neptun, Ferdinand) AND the color using the QLineEdit Editor.

If I leave this line out of the comment I can ONLY edit the color using the colorListEditor widget but I loose the possibility to edit the names (Alice, Neptun, Ferdinand).

What I would like to do is to use all standard editors for all the types that are already recognized (f.e. QLineEdit for QString) and use in addition custom editors for the other types (ColorListEditor for QVariant::color).

In other words: I would like to be able to edit the names (Alice, Neptun, Ferdinand) by using QLineEdit and the color by using the custom editor for QVariant::Color without the need to add lines like:
QItemEditorCreatorBase *stringCreator = new QStandardItemEditorCreator<QLineEdit>();
factory->registerEditor(QVariant::String, stringCreator);

talk2amulya
11th May 2009, 20:00
thats a very good catch! but i dont know how you are able to edit both data types when commenting setDefaultFactory()...anyways, couple of ways to resolve your issue:

1. using delegate: dont set this factory as default, just call setItemEditorFactory(QItemEditorFactory *) on QStyledItemDelegate, Qt's right hand for drawing views. thus when QVariant::Type is Color, it will create your custom editor, for all other QVariant types, it will use default factory.

2. derive from QItemEditorFactory, implement createEditor(..) in a way that if type is Color, it will use ColorEditor, otherwise it will call default factory's createEditor(..). next, set this new class as the default editor factory.

i think second one will work better for you, i.e. without interfering with any delegate.

schall_l
25th May 2009, 20:49
Thank you talk2amulya, the second one did effectively worked for me.

Here is the code if someone is interested in:


class IdentifiersEditor : public QItemEditorFactory
{
public:
inline IdentifiersEditor() {}
QWidget *createEditor(QVariant::Type type, QWidget *parent) const;
};


QWidget *
IdentifiersEditor::createEditor(QVariant::Type type, QWidget *parent) const {
switch (type) {
case QVariant::Color: {
ColorListEditor *cb = new ColorListEditor(parent);
cb->setFrame(false);
return cb;
}
default:
return QItemEditorFactory::createEditor(type, parent);
break;
}
return 0;
}

aamer4yu
26th May 2009, 05:32
I have a doubt. You will still need to set your factory somewhere right ?
Also in the example in post #1, you create a QItemEditorFactory and thats why you dont get the default editors with it.

Why cant you obtain the factory from item delegate and register you editor ??
QItemDelegate::itemEditorFactory and QStyledItemDelegate::itemEditorFactory :rolleyes:

Wont it be easier ?

shentian
29th May 2009, 18:38
Hi

I had the same problem. schall_l's idea to subclass QItemEditorFactory is basically right, but it doesn't work, since QItemEditorFactory ist a base class with no functionality. You have to use the previously installed default factory (which is an instance of the private class QDefaultItemEditorFactory).


class ItemEditorFactory : public QItemEditorFactory
{
public:
explicit ItemEditorFactory(const QItemEditorFactory* standardFactory);
virtual QWidget* createEditor(QVariant::Type type, QWidget* parent) const;
static void installAsStandardFactory();
private:
const QItemEditorFactory* m_standardFactory;
};


ItemEditorFactory::ItemEditorFactory(const QItemEditorFactory* standardFactory)
: QItemEditorFactory()
, m_standardFactory(standardFactory)
{
}

QWidget* ItemEditorFactory::createEditor(QVariant::Type type, QWidget* parent) const
{
switch (type)
{
case QVariant::Color:
{
ColorListEditor *cb = new ColorListEditor(parent);
cb->setFrame(false);
return cb;
}
default:
return m_standardFactory->createEditor(type, parent);
}
}

static void ItemEditorFactory::installAsStandardFactory()
{
QItemEditorFactory::setDefaultFactory(new ItemEditorFactory(QItemEditorFactory::defaultFacto ry()));
}


Just call ItemEditorFactory::installAsStandardFactory() in your code.

Happy Qoding. :)

DarkForceOne
15th May 2014, 19:29
Shentian's code works perfectly in Qt4. However, the declaration of the virtual function createEditor(...) is slightly different in Qt5:



QWidget* ItemEditorFactory::createEditor(int userType, QWidget* parent) const