Results 1 to 2 of 2

Thread: Custom item editor is now showing up.

  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Custom item editor is now showing up.

    I am trying to create a custom editor for a user datatype to show in a model-view . Here is want I have:

    Custom data type btRule (class), registered with Q_DECLARE_METATYPE(btRule)

    Model btRulesModel that returns btRule for editing:
    Qt Code:
    1. QVariant btRulesModel::data(const QModelIndex& index, int role) const
    2. {
    3. btRule& rul = _rules[index.row()];
    4. …
    5. if ( (role == Qt::EditRole) && (index.column() == 1))
    6. {
    7. v.setValue(rul);
    8. return v;
    9. }
    10. …
    11. }
    To copy to clipboard, switch view to plain text mode 

    I have class ModelParamViewer derived from QStyledItemDelegate. In the constructor I am registering custom editor
    Qt Code:
    1. ModelParamViewer::ModelParamViewer(QObject *parent)
    2. : QStyledItemDelegate(parent)
    3. {
    4. QItemEditorCreator<ParamEditor>* itemCreator =
    5. new QItemEditorCreator<ParamEditor>("btRules");
    6.  
    7. btRule br;
    8. v.setValue(br);
    9. this->setItemEditorFactory(factory);
    10. QItemEditorFactory* editorfactory = this->itemEditorFactory();
    11. editorfactory->registerEditor(v.type(), itemCreator);
    12. }:
    To copy to clipboard, switch view to plain text mode 

    And I am showing the editor in
    Qt Code:
    1. QWidget* ModelParamViewer::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem& option,
    3. const QModelIndex& index) const
    4. {
    5. …
    6. ParamEditor* wdg= new ParamEditor(parent);
    7. //QComboBox* wdg = new QComboBox(parent);
    8. return wdg;
    9. }
    To copy to clipboard, switch view to plain text mode 

    My custome editor is:
    Qt Code:
    1. class ParamEditor : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. ParamEditor (QWidget* parent = 0) : QWidget(parent)
    6. {
    7. QString ss("QSpinBox{background-color: #ABABAB;border: 1px solid black;}");
    8. setStyleSheet(ss);
    9. setMinimumWidth(200);
    10. QHBoxLayout* layout = new QHBoxLayout();
    11. setLayout(layout);
    12. QLabel* lbl = new QLabel"label 1", this);
    13. layout->addWidget(lbl);
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 
    Question:
    when I am in the edit mode the editor does not show up. However, when I am replacing editor with ComboBox – combobox shows up as the editor. I understand that ComboBox, just like QLabel, are some of the default editors for the regular datatypes. What am I doing wrong here?

  2. #2
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom item editor is now showing up.

    I recently used a custom QStyledItemDelegate and custom editor (derived from QWidget). I followed the example Star Delegate pretty closely. I didn't go down the route of an EditorFactory and by the look of your example it might not be necessary either.

  3. The following user says thank you to stefanadelbert for this useful post:

    oberlus (6th October 2011)

Similar Threads

  1. QGraphicsView and showing entire item in it
    By T4ng10r in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2010, 11:44
  2. Item Delegate editor background
    By LynneV in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2010, 21:33
  3. Replies: 2
    Last Post: 12th February 2010, 04:41
  4. Replies: 5
    Last Post: 10th August 2009, 10:50
  5. Showing Pixmap as a QListWidget Item
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 1st February 2007, 12:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.