Results 1 to 2 of 2

Thread: QPushButton as editor in QTreeView

  1. #1
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPushButton as editor in QTreeView

    I am working on a project and I need something like a property editor for it.
    So I use TreeView for it. I defined my own classes derived from QAbstractItemModel and QStyledItemDelegate. Looks great.

    for editing simple values I use QLineEditor`s etc.
    But now I need editor for color and here's what I do:
    1. I define QPushButton as editor
    2. I click on a cell ad my button appears
    3. I click on the button, it opens QColorDialog and I select a color.
    4. I close color dialog and emit closeEditor and here it crashed! As I understand, when QBushButton(or any else editor) loses focus it removed by TreeView.

    Code snippet to show my idea:
    Qt Code:
    1. QWidget *PropertyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. PropertyItem * item = (PropertyItem *)(index.internalPointer());
    4. if(item)
    5. {
    6. QWidget * editor;
    7. switch(item->GetPropertyType())
    8. {
    9. ...
    10. case PropertyItem::PropertyTypeColor:
    11. editor = new QPushButton(parent);
    12. ((QPushButton *)editor)->setText("...");
    13. connect(((QPushButton *)editor),SIGNAL(clicked()),this,SLOT(colorTypeClicked()));
    14. return editor;
    15. break;
    16.  
    17. default:
    18. return QStyledItemDelegate::createEditor(parent,option,index);
    19. }
    20. }
    21. return QStyledItemDelegate::createEditor(parent,option,index);
    22. }
    23. void PropertyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    24. {
    25. PropertyItem * item = static_cast<PropertyItem*>(index.internalPointer());
    26. if(item)
    27. {
    28. switch(item->GetPropertyType())
    29. {
    30. ...
    31. case PropertyItem::PropertyTypeColor:
    32. currentValue = item->GetValue();
    33. break;
    34. default: QStyledItemDelegate::setEditorData(editor,index);
    35. }
    36. }
    37. }
    38. void PropertyDelegate::colorTypeClicked()
    39. {
    40. QWidget * editor = qobject_cast<QWidget *>(sender());
    41. QColor current = currentValue.value<QColor>();
    42. currentValue = QColorDialog::getColor(current,NULL,tr("Select color"));
    43. editor->setFocus();
    44. emit commitData(editor);
    45. emit closeEditor(editor); // crashed here
    46. }
    To copy to clipboard, switch view to plain text mode 

    So my question - how can I keep it from deleting? Or, if my way looks wrong, any good idea to edit color in property editor.

    Thanks for advice!

    P.S. I've searched the forum, I've googled it but I've not found some usefull code

  2. #2
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton as editor in QTreeView

    OK, I answer to my own post.
    I did it with event filter:

    inside PropertyDelegate::createEditor() :
    Qt Code:
    1. ...
    2. case PropertyItem::PropertyTypeColor:
    3. editor = new QPushButton(parent);
    4. ((QPushButton *)editor)->setText("...");
    5. connect(((QPushButton *)editor),SIGNAL(clicked()),this,SLOT(colorTypeClicked()));
    6. editor->installEventFilter(const_cast<PropertyDelegate*>(this)); /// this line to install custom event listener
    7. return editor;
    8. break;
    To copy to clipboard, switch view to plain text mode 

    and this function to ignore focus losing:
    Qt Code:
    1. bool PropertyDelegate::eventFilter(QObject *object, QEvent *event)
    2. {
    3. if(event->type() == QEvent::FocusOut)
    4. {
    5. if(QString(object->metaObject()->className()) == "QPushButton") {
    6. return true;
    7. }
    8. }
    9. return QStyledItemDelegate::eventFilter(object,event);
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Designing a Node Editor (Hint: Blender Node Editor)
    By Mind Calamity in forum Qt Programming
    Replies: 4
    Last Post: 5th October 2011, 16:22
  2. Replies: 0
    Last Post: 22nd February 2010, 09:30
  3. Not an editor, nor IDE ;)?
    By whp in forum Qt-based Software
    Replies: 0
    Last Post: 27th February 2007, 12:32
  4. Replies: 3
    Last Post: 26th September 2006, 12:16
  5. RTF editor
    By prakash in forum Qt Programming
    Replies: 10
    Last Post: 14th April 2006, 17:33

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.