Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStyledItemDelegate with editor-dialog

QStyledItemDelegate with editor-dialog

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 2.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Andy314A Offline
    Andy314A Offline
    Andy314
    wrote on last edited by Andy314
    #1

    Hello,
    I want to open an editor-dialog when I activate the editing in a QTableView-cell. Therefore I created a ItemDelegate:

    class TMemoEditDelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    public:
        TMemoEditDelegate(QObject* parent):QStyledItemDelegate(parent) {}
        bool editorEvent(QEvent *event, QAbstractItemModel *model,
                                       const QStyleOptionViewItem &option,
                                       const QModelIndex &index)
        {
    
            //InfoDlg("edit it");
            TTextEditForm d;
            d.Text = index.data().toString();
            bool ok = d.execBool();
            if(ok)
            {
                model->setData(index, d.Text);
            }
            return QStyledItemDelegate::editorEvent(event,model,option,index);
        }
    };
    

    The editing works, but the problem is the edit-trigger. In the QTableView I have set only the "DoubleClicked" edit trigger.
    But a simple click on a unselected row in the specific column opens the dialog.
    Therefore no simple selection is possible. In the other columns the edit-trigger works correctly.
    Whats the problem ?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      The short (and not so correct) answer is - check the type of the event. You should only open the dialog if event->type() == QEvent::MouseButtonDblClick. Right now you're responding to all of them - press, release, dblclick

      The longer answer is that you're entirely bypassing the editor mechanism so the edit trigger is completely ignored. The proper way to do it is to implement createEditor, not editorEvent. This way figuring out if the editor should be shown is done by Qt according to the edit trigger setting. Also instead of manually calling data() and setData() you should implement setEditorData and setModelData.

      Andy314A 1 Reply Last reply
      0
      • Chris KawaC Chris Kawa

        The short (and not so correct) answer is - check the type of the event. You should only open the dialog if event->type() == QEvent::MouseButtonDblClick. Right now you're responding to all of them - press, release, dblclick

        The longer answer is that you're entirely bypassing the editor mechanism so the edit trigger is completely ignored. The proper way to do it is to implement createEditor, not editorEvent. This way figuring out if the editor should be shown is done by Qt according to the edit trigger setting. Also instead of manually calling data() and setData() you should implement setEditorData and setModelData.

        Andy314A Offline
        Andy314A Offline
        Andy314
        wrote on last edited by Andy314
        #3

        Hi @Chris-Kawa,
        the event-filtering works. Maybe a good way to implement other edit-triggers (F12 for example).
        Nevertheless I will try the proper way. In the createEditor I can generate a dialog and give this back as a QWidget. Then this widget is under control of the QTableView. If I click in an other cell the editor closes and I read in setModelData the data from the dialog before.
        So, what can I do if the dialog must have a Ok and Cancel button to accept or discard the data ?
        (In my current version I use even a modal dialog.)

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          If I click in an other cell the editor closes

          If the dialog is modal you shouldn't be able to click on anything else when it's open.
          This works as expected (modal dialog) for me:

          QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const override
          {
              QDialog* d = new QDialog(parent);
              d->setModal(true);
              return d;
          }
          
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved