Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Custom QStyledItemDelegate - strange behavior on closing



  • Hello mates,

    Maybe you will have a hint what might be wrong.
    I've reimplemented the delegate, and as editor it uses a QWidget which contains QComboBox and QPushButton ("Cancel"); - the button was meant to abort edition without need of pressing ESC. I connected its clicked() signal to the slot below:

    void TagsDelegate::cancelEdition()
    {
        emit closeEditor(static_cast<QWidget*>(sender()->parent()), QAbstractItemDelegate::RevertModelCache);
    
    //    QKeyEvent key(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier);
    //    eventFilter(sender()->parent(), &key);
    }
    

    and now what's wrong - edition is cancelled, but the table view sets new row as current index/selected index. This is not happening if the editor was closed by pressing ESC, marked row in that case stays the same. However, after analysis of the available source code about handling ESC key press I've found nothing different than the emit closeEditor() I'm using :/

    So my question is - what might cause such behavior??


  • Lifetime Qt Champion

    Hi
    That is kinda odd.
    Try sending esc to it to see if that does the same
    something like

    void SendKey(Qt::Key key) {
    auto FOCUSOBJ = QGuiApplication::focusObject();
    QString keyStr(QKeySequence(key).toString());
    QKeyEvent pressEvent = QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, keyStr);
    QKeyEvent releaseEvent = QKeyEvent(QEvent::KeyRelease, key, Qt::NoModifier);
    QCoreApplication::sendEvent(FOCUSOBJ, &pressEvent);
    QCoreApplication::sendEvent(FOCUSOBJ, &releaseEvent);
    }



  • I've already tried something similar:

    void TagsDelegate::cancelEdition()
    {
        //emit closeEditor(static_cast<QWidget*>(sender()->parent()), QAbstractItemDelegate::RevertModelCache);
    
       QKeyEvent *key = new QKeyEvent(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier);
       qApp->postEvent(static_cast<QWidget*>(sender()->parent()), key);    
    }
    

    and without success :/ Your code:

    #include <QKeyEvent>
    #include <QApplication>
    void SendKey(Qt::Key key)
    {
        auto FOCUSOBJ = QGuiApplication::focusObject();
        QString keyStr(QKeySequence(key).toString());
        QKeyEvent pressEvent = QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, keyStr);
        QKeyEvent releaseEvent = QKeyEvent(QEvent::KeyRelease, key, Qt::NoModifier);
        QCoreApplication::sendEvent(FOCUSOBJ, &pressEvent);
        QCoreApplication::sendEvent(FOCUSOBJ, &releaseEvent);
    }
    void TagsDelegate::cancelEdition()
    {
        SendKey(Qt::Key_Escape);
        //emit closeEditor(static_cast<QWidget*>(sender()->parent()), QAbstractItemDelegate::RevertModelCache);
    
        //QKeyEvent *key = new QKeyEvent(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier);
        //qApp->postEvent(static_cast<QWidget*>(sender()->parent()), key);
    }
    

    outputs the same result :/

    However, I've discovered something more - problem seems to be connected to the editor:
    0_1565944075548_TagsEditor.png
    If I don't give focus to the combobox pressing ESC closes the editor and stays on the row. However is any of the editor's widgets get focus then even ESC key moves down to the next row.
    Hmmm, that actually reminds me a part of documentation about giving strong focus to the editor, will double check it.


  • Lifetime Qt Champion

    ok super.
    So its not related to close it "from inside" or similar.
    The strong focus is worth checking out.



  • Nope :/ that's not focus-related issue:

    class TagRowEditor : public QWidget
    {
      public:
        TagRowEditor(QWidget *parent = nullptr) : QWidget(parent)
        {
            button = new QPushButton("Cancel", this);
            button->setFocusPolicy(Qt::StrongFocus);
    
            comboBox = new QComboBox(this);
            comboBox->setEditable(true);
            QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            comboBox->setSizePolicy(sizePolicy);
            comboBox->setFocusPolicy(Qt::StrongFocus);
    
            horizontalLayout = new QHBoxLayout(this);
            horizontalLayout->setSpacing(2);
            horizontalLayout->setContentsMargins(0, 0, 0, 0);
            horizontalLayout->addWidget(comboBox);
            horizontalLayout->addWidget(button);
    
            //setFocusPolicy(Qt::StrongFocus);
        }
    
        QComboBox *comboBox;
        QPushButton *button;
    
    private:
        QHBoxLayout *horizontalLayout;
    };
    

  • Lifetime Qt Champion

    Hi
    If i want to try to reproduce it this evening, is
    that code enough to see the issue ?



  • Hmm might be not enough. Catch full implementation of the TagsDelegate I'm using.
    [1_1565950005075_tagsdelegate.h](Uploading 100%)
    [0_1565950005075_tagsdelegate.cpp](Uploading 100%)
    Install the delegate on a column of a QTableView, in case of might be important - I have set selection behavior = selectRows, and selectionMode = singleSelection

    DAMMIT, i got error "You don't have You do not have enough privileges for this action."


  • Lifetime Qt Champion

    @masterblb
    hi upload here is broken.
    Im wont be able to get them.
    maybe use
    https://paste.ofcode.org/




Log in to reply