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 subclass: weird QLineEdit behavior and memory issues, what am I doing wrong?

QStyledItemDelegate subclass: weird QLineEdit behavior and memory issues, what am I doing wrong?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 1.3k Views 1 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    I have a TreeView in my program, and I have a QStyledItemDelegate subclass for customizing editor behavior. This is its complete listing:

    @CFileListItemDelegate::CFileListItemDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
    {
    }

    QWidget CFileListItemDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
    {
    QLineEdit * editor = dynamic_cast<QLineEdit
    >(QStyledItemDelegate::createEditor(parent, option, index));
    connect(editor, &QLineEdit::returnPressed, ={
    QMessageBox::warning(nullptr, "Test", "Item edited");
    });
    return editor;
    }

    void CFileListItemDelegate::setModelData(QWidget * /editor/, QAbstractItemModel * /model/, const QModelIndex & /index/) const
    {
    // Doing nothing since I only want to save changes when Enter is pressed
    }@

    When I press Enter in the editor, the message box is shown as expected. When I click "OK" in the message box to close it, the program crashes due to access violation. Qt is trying to dispatch a QKeyEvent to some QLineEdit whose this pointer is invalid (not a pointer to an already freed object).
    There are no QLineEdits in my UI except for the editor. Any idea what's going on?

    And a second question. If I connect to editingFinished instead of returnPressed, the crash does not occur but I get 2 message boxes one after another. Why?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Violet Giraffe
      wrote on last edited by
      #2

      P. S. regarding my second question: the first time the slot is called because of KeyPressEvent that the editor receives when I press Enter, the second one originates in the FocusOut event. This behavior seems weird.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

        I'll appreciate any ideas on the memory issue.

        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