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. How to reuse editor widget for custom item delegate
Forum Update on Monday, May 27th 2025

How to reuse editor widget for custom item delegate

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 455 Views
  • 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.
  • N Offline
    N Offline
    namniav
    wrote on 27 Jul 2022, 15:02 last edited by namniav
    #1

    I want to reuse the editor widget for custom item delegate.

    void QAbstractItemDelegate::destroyEditor(QWidget *editor, const QModelIndex &index) const
    says

    Called when the editor is no longer needed for editing the data item with the given index and should be destroyed. The default behavior is a call to deleteLater on the editor. It is possible e.g. to avoid this delete by reimplementing this function.

    I subclass from QStyledItemDelegate and reimplement destroyEditor like

    editor->setParent(nullptr);
    editor->hide();
    editor->setEnabled(false);
    m_idleEditor = editor;
    

    All works well except that when the editor has focus and I close the window, the app crashes. [Edit: The crashing also happens when the editor has focus and the entire window loses focus.]

    What's the problem and how can I completely detach the editor without deleting it?

    I am using Qt6.3 on windows 10.

    Here's the reduced complete demo to reproduce:

    #include <QDebug>
    #include <QApplication>
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QTreeView>
    #include <QComboBox>
    
    #include <QStandardItem>
    #include <QStandardItemModel>
    #include <QStyledItemDelegate>
    
    class ComboDelegate: public QStyledItemDelegate {
    public:
        ComboDelegate(QObject* parent): QStyledItemDelegate(parent) {}
    
        ~ComboDelegate() {
            qDebug() << "entering " << __func__;
            idle->deleteLater();
            qDebug() << "exiting " << __func__;
        }
    
        QComboBox* createEditor(QWidget* parent,
                                const QStyleOptionViewItem& option,
                                const QModelIndex& index)
        const override
        {
            qDebug() << "entering " << __func__;
            auto cb = new QComboBox(parent);
            cb->addItem("One");
            cb->addItem("Two");
            cb->setEditable(true);
            qDebug() << "exiting " << __func__;
            return cb;
        }
    
        void setEditorData(QWidget* editor, const QModelIndex& index)
        const override
        {
            qDebug() << "entering " << __func__;
            auto cb = qobject_cast<QComboBox*>(editor);
            cb->setCurrentText(index.data(Qt::DisplayRole).toString());
            qDebug() << "exiting " << __func__;
        }
    
        void setModelData(QWidget* editor,
                          QAbstractItemModel* model,
                          const QModelIndex& index)
        const override
        {
            qDebug() << "entering " << __func__;
            auto cb = qobject_cast<QComboBox*>(editor);
            model->setData(index, cb->currentText(), Qt::DisplayRole);
            qDebug() << "exiting " << __func__;
        }
    
        void destroyEditor(QWidget* editor, const QModelIndex& index)
        const override
        {
            qDebug() << "entering " << __func__;
            auto cb = qobject_cast<QComboBox*>(editor);
            cb->setParent(nullptr); // How to completely detach the editor from treeview ?
            cb->hide();
            cb->setEnabled(false);
            idle = cb;
            qDebug() << "exiting " << __func__;
        }
    
    private:
        mutable QComboBox* idle{nullptr};
    };
    
    int main(int argc, char** argv) {
        QApplication app(argc, argv);
    
        QWidget widget;
    
        auto box = new QVBoxLayout(&widget);
    
        auto model = new QStandardItemModel(&widget);
        model->appendRow(new QStandardItem("One"));
        model->appendRow(new QStandardItem("Two"));
    
        auto tree = new QTreeView(&widget);
        box->addWidget(tree);
        tree->setModel(model);
        tree->setItemDelegate(new ComboDelegate(&widget));
    
        widget.show();
    
        tree->edit(model->index(0, 0));
    
        return app.exec();
    
        /*
          Then just close the window when the combobox has focus,
          the app crashes.
    
          If you click on blank to close the combobox before closing
          the window, the app can exit normally.
        */
    
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 27 Jul 2022, 18:12 last edited by
      #2

      Hi,

      What does your stack trace look like ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 2 Replies Last reply 28 Jul 2022, 01:21
      0
      • S SGaist
        27 Jul 2022, 18:12

        Hi,

        What does your stack trace look like ?

        N Offline
        N Offline
        namniav
        wrote on 28 Jul 2022, 01:21 last edited by
        #3

        @SGaist

        34493037-ce93-4557-af46-b2a15e1a8732-image.png

        .  0  Id: 5314.3a74 Suspend: 1 Teb: 0000005a`6e313000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6e4f8970 00007ff8`72caf426 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x25a81
        0000005a`6e4f89b0 00007ff8`72d8fbf7 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x24f7d
        0000005a`6e4f89f0 00007ff8`72cafa09 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x10574e
        0000005a`6e4f8b90 00007ff8`7332c259 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x25560
        0000005a`6e4f8bc0 00007ff8`733afd97 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x6a1db0
        0000005a`6e4f93b0 00007ff8`7989d7ab Qt6Widgetsd!QLineEdit::`default constructor closure'+0x7258ee
        0000005a`6e4f93f0 00007ff8`72cbcfb9 Qt6Cored!QFileInfo::filePath+0x14f13e
        0000005a`6e4f9470 00007ff8`72cba2a7 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x32b10
        0000005a`6e4f94d0 00007ff8`7989c1b7 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x2fdfe
        0000005a`6e4fa350 00007ff8`7989985b Qt6Cored!QFileInfo::filePath+0x14db4a
        0000005a`6e4fa430 00007ff8`72cbdf72 Qt6Cored!QFileInfo::filePath+0x14b1ee
        0000005a`6e4fa460 00007ff8`72cb6572 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x33ac9
        0000005a`6e4fa580 00007ff8`72da802c Qt6Widgetsd!QLineEdit::`default constructor closure'+0x2c0c9
        0000005a`6e4fa830 00007ff8`72da9aa5 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x11db83
        0000005a`6e4fa870 00007ff8`72da9c89 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x11f5fc
        0000005a`6e4fa8f0 00007ff8`72daa336 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x11f7e0
        0000005a`6e4fa9c0 00007ff8`72d91219 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x11fe8d
        0000005a`6e4faac0 00007ff8`72d91346 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x106d70
        0000005a`6e4fab00 00007ff8`72da6216 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x106e9d
        0000005a`6e4fab40 00007ff8`72de968c Qt6Widgetsd!QLineEdit::`default constructor closure'+0x11bd6d
        0000005a`6e4fabe0 00007ff8`7a899685 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x15f1e3
        0000005a`6e4fac40 00007ff8`72de94ca Qt6Guid!QTextCharFormat::setFontLetterSpacingType+0x27c398
        0000005a`6e4faf60 00007ff8`72cbcfeb Qt6Widgetsd!QLineEdit::`default constructor closure'+0x15f021
        0000005a`6e4fb030 00007ff8`72cba2ca Qt6Widgetsd!QLineEdit::`default constructor closure'+0x32b42
        0000005a`6e4fb090 00007ff8`7989c1b7 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x2fe21
        0000005a`6e4fbf10 00007ff8`7989c04b Qt6Cored!QFileInfo::filePath+0x14db4a
        0000005a`6e4fbff0 00007ff8`7a7a7b39 Qt6Cored!QFileInfo::filePath+0x14d9de
        0000005a`6e4fc020 00007ff8`7a7a8c46 Qt6Guid!QTextCharFormat::setFontLetterSpacingType+0x18a84c
        0000005a`6e4fc080 00007ff8`7a8a62b3 Qt6Guid!QTextCharFormat::setFontLetterSpacingType+0x18b959
        0000005a`6e4fc120 00007ff8`7aea79e2 Qt6Guid!QTextCharFormat::setFontLetterSpacingType+0x288fc6
        0000005a`6e4fc1a0 00007ff8`79cb77ef Qt6Guid!QTextCharFormat::setFontLetterSpacingType+0x88a6f5
        0000005a`6e4fc1d0 00007ff8`7aea798b Qt6Cored!QFileInfo::filePath+0x569182
        0000005a`6e4ff430 00007ff8`798c1aec Qt6Guid!QTextCharFormat::setFontLetterSpacingType+0x88a69e
        0000005a`6e4ff480 00007ff8`798c1dd4 Qt6Cored!QFileInfo::filePath+0x17347f
        0000005a`6e4ff4d0 00007ff8`798995fa Qt6Cored!QFileInfo::filePath+0x173767
        0000005a`6e4ff5c0 00007ff8`7a7a047a Qt6Cored!QFileInfo::filePath+0x14af8d
        0000005a`6e4ff6a0 00007ff8`72cb700c Qt6Guid!QTextCharFormat::setFontLetterSpacingType+0x18318d
        0000005a`6e4ff6d0 00007ff7`a99c1c01 Qt6Widgetsd!QLineEdit::`default constructor closure'+0x2cb63
        0000005a`6e4ff700 00007ff7`a99c3fd9 temptest!main(int argc = 0n1, char ** argv = 0x0000016b`87b49cd0)+0x501 [D:\temptest-qt\temptest.cpp @ 92]
        0000005a`6e4ff8e0 00007ff7`a99c3e7e temptest!invoke_main(void)+0x39 [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 79]
        0000005a`6e4ff930 00007ff7`a99c3d3e temptest!__scrt_common_main_seh(void)+0x12e [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 288]
        0000005a`6e4ff9a0 00007ff7`a99c406e temptest!__scrt_common_main(void)+0xe [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 331]
        0000005a`6e4ff9d0 00007ff8`fc557034 temptest!mainCRTStartup(void * __formal = 0x0000005a`6e312000)+0xe [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp @ 17]
        0000005a`6e4ffa00 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6e4ffa30 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
           1  Id: 5314.2058 Suspend: 1 Teb: 0000005a`6e315000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6e5ffab8 00007ff8`fd562dc7 ntdll!ZwWaitForWorkViaWorkerFactory+0x14
        0000005a`6e5ffac0 00007ff8`fc557034 ntdll!TpReleaseCleanupGroupMembers+0x747
        0000005a`6e5ffdc0 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6e5ffdf0 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
           2  Id: 5314.3b88 Suspend: 1 Teb: 0000005a`6e317000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6e6ff6e8 00007ff8`fd562dc7 ntdll!ZwWaitForWorkViaWorkerFactory+0x14
        0000005a`6e6ff6f0 00007ff8`fc557034 ntdll!TpReleaseCleanupGroupMembers+0x747
        0000005a`6e6ff9f0 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6e6ffa20 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
           3  Id: 5314.50d8 Suspend: 1 Teb: 0000005a`6e319000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6e7ff8b8 00007ff8`fd562dc7 ntdll!ZwWaitForWorkViaWorkerFactory+0x14
        0000005a`6e7ff8c0 00007ff8`fc557034 ntdll!TpReleaseCleanupGroupMembers+0x747
        0000005a`6e7ffbc0 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6e7ffbf0 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
           4  Id: 5314.454c Suspend: 1 Teb: 0000005a`6e31b000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6e8ff888 00007ff8`fd562dc7 ntdll!ZwWaitForWorkViaWorkerFactory+0x14
        0000005a`6e8ff890 00007ff8`fc557034 ntdll!TpReleaseCleanupGroupMembers+0x747
        0000005a`6e8ffb90 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6e8ffbc0 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
           5  Id: 5314.4ca0 Suspend: 1 Teb: 0000005a`6e31d000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6e9ff758 00007ff8`fd562dc7 ntdll!ZwWaitForWorkViaWorkerFactory+0x14
        0000005a`6e9ff760 00007ff8`fc557034 ntdll!TpReleaseCleanupGroupMembers+0x747
        0000005a`6e9ffa60 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6e9ffa90 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
           6  Id: 5314.2db0 Suspend: 1 Teb: 0000005a`6e31f000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6eaffa88 00007ff8`fac7cbc0 ntdll!ZwWaitForMultipleObjects+0x14
        *** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\WINDOWS\System32\CRYPT32.dll - 
        0000005a`6eaffa90 00007ff8`fb37bee7 KERNELBASE!WaitForMultipleObjectsEx+0xf0
        0000005a`6eaffd80 00007ff8`fc557034 CRYPT32!CertFreeCTLContext+0x397
        0000005a`6eaffdc0 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6eaffdf0 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
           7  Id: 5314.2a90 Suspend: 1 Teb: 0000005a`6e323000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6ebff5d8 00007ff8`fac7cbc0 ntdll!ZwWaitForMultipleObjects+0x14
        *** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\WINDOWS\System32\combase.dll - 
        0000005a`6ebff5e0 00007ff8`fd1ab776 KERNELBASE!WaitForMultipleObjectsEx+0xf0
        0000005a`6ebff8d0 00007ff8`fd1ab5f0 combase!CoRevokeInitializeSpy+0x1516
        0000005a`6ebffb60 00007ff8`fd1ab400 combase!CoRevokeInitializeSpy+0x1390
        0000005a`6ebffbb0 00007ff8`fd1ab389 combase!CoRevokeInitializeSpy+0x11a0
        0000005a`6ebffc10 00007ff8`fc557034 combase!CoRevokeInitializeSpy+0x1129
        0000005a`6ebffc40 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6ebffc70 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
           8  Id: 5314.298 Suspend: 1 Teb: 0000005a`6e325000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6ecff868 00007ff8`fd562dc7 ntdll!ZwWaitForWorkViaWorkerFactory+0x14
        0000005a`6ecff870 00007ff8`fc557034 ntdll!TpReleaseCleanupGroupMembers+0x747
        0000005a`6ecffb70 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6ecffba0 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
           9  Id: 5314.498 Suspend: 1 Teb: 0000005a`6e327000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6edff528 00007ff8`fd562dc7 ntdll!ZwWaitForWorkViaWorkerFactory+0x14
        0000005a`6edff530 00007ff8`fc557034 ntdll!TpReleaseCleanupGroupMembers+0x747
        0000005a`6edff830 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6edff860 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
          10  Id: 5314.3860 Suspend: 1 Teb: 0000005a`6e329000 Unfrozen
        Child-SP          RetAddr           Call Site
        0000005a`6eeffa28 00007ff8`ec532751 ntdll!ZwWaitForMultipleObjects+0x14
        0000005a`6eeffa30 00007ff8`fc557034 WINMM!PlaySoundW+0xe01
        0000005a`6eeffbb0 00007ff8`fd562651 KERNEL32!BaseThreadInitThunk+0x14
        0000005a`6eeffbe0 00000000`00000000 ntdll!RtlUserThreadStart+0x21
        
        1 Reply Last reply
        0
        • S SGaist
          27 Jul 2022, 18:12

          Hi,

          What does your stack trace look like ?

          N Offline
          N Offline
          namniav
          wrote on 28 Jul 2022, 01:31 last edited by
          #4

          The complete demo above should be able and easy to reproduce the crashing.

          And I found that the crashsing happens when the editor has focus and the entire window loses focus. No need to close the window.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            namniav
            wrote on 28 Jul 2022, 06:39 last edited by
            #5

            I think this is possibly a bug of Qt and have filed a report at https://bugreports.qt.io/browse/QTBUG-105231.

            1 Reply Last reply
            0

            1/5

            27 Jul 2022, 15:02

            • Login

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