[Interest] QInputDialog as item delegate?

Max Paperno max-l at wdg.us
Wed Nov 13 18:08:12 CET 2019


Hi Matthew,

I have this in a delegate and it seems to work well.  I don't know about "best way."  This one intercepts the double-click event which would typically open an editor. It could also work with a single click if you prefer (I personally found that annoying from a user perspective).  This particular delegate also re-implements the paint() method (to show a color swatch) and a sizeHint(), but I don't see how that would matter. Otherwise this is the only other method in it.

bool ColorDelegate::editorEvent(QEvent *e, QAbstractItemModel *mdl, const QStyleOptionViewItem &opt, const QModelIndex &idx)
{
   if (e->type() == QEvent::MouseButtonDblClick && idx.data().canConvert<QColor>()) {
     if ((idx.flags() & Qt::ItemIsEditable) && static_cast<QMouseEvent*>(e)->button() == Qt::LeftButton) {
       QColorDialog *dlg = new QColorDialog(const_cast<QWidget*>(opt.widget));
       dlg->setColor(idx.data().value<QColor>());
       dlg->setModal(true);
       dlg->setAttribute(Qt::WA_DeleteOnClose);
       connect(dlg, &QColorDialog::colorSelected, this, [mdl, idx](const QColor &c) {
         mdl->setData(idx, QVariant::fromValue(c));
       });
       dlg->show();
     }
     return true;
   }
   return QStyledItemDelegate::editorEvent(e, mdl, opt, idx);
}

HTH,
-Max

On 11/12/2019 3:45 PM, Matthew Woehlke wrote:
> I have a QTreeView. For one of the columns, rather than editing the data
> in-place, I want to pop up a QTextEdit. (For now, I'm hoping I'll be
> able to use QInputDialog, but I may end up needing to roll my own.)
>
> Is it reasonable to execute the dialog (QDialog::exec()) in an override
> of QAbstractItemDelegate::createEditor, or do I need to hook
> itemActivated or some such? (Maybe QAbstractItemDelegate::editorEvent
> would be better?)
>
> What is the best way to trigger the editor?
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20191113/ff007e64/attachment.html>


More information about the Interest mailing list