[Qt-interest] item delegates

Graham Labdon Graham.Labdon at avalonsciences.com
Wed Mar 9 12:52:06 CET 2011


Hi
I am implementing a Item Delegate for my tree based model.
I need to create a different editor depending upon the item being edited.
At the moment I am using switch statements to decide which editor to supply, but as my model grows so do the switch statements.

                QWidget* editor;

                QModelIndex parentIndex = index.model()->parent(index);

                switch (index.model()->parent(index).row())
                {
                                case 0:
                                {
                                                switch (index.row())
                                                {
                                                                case 0:
                                                                case 1:
                                                                {
                                                                                editor = new QLineEdit(parent);
                                                                }
                                                                break;

                                                                case 2:
                                                                {
                                                                                QStringList list;
                                                                                Common::Enumerations::surveyUnits().getDisplayStrings(list);
                                                                                editor = new QComboBox(parent);
                                                                                ((QComboBox*)editor)->addItems(list);
                                                                }
                                                                break;
                                                }
                                }
                                break;

                                case 1:
                                {
                                                editor = new QLineEdit(parent);
                                                ((QLineEdit*)editor)->setValidator(new QIntValidator());
                                }
                                break;
                }
                return editor;

Is there a better way to achieve this?

TIA
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110309/dab445f5/attachment.html 


More information about the Qt-interest-old mailing list