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. Custom QStyledItemDelegate - strange behavior on closing

Custom QStyledItemDelegate - strange behavior on closing

Scheduled Pinned Locked Moved Unsolved General and Desktop
qitemdelegate
9 Posts 2 Posters 1.0k 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.
  • M Offline
    M Offline
    MasterBLB
    wrote on 16 Aug 2019, 08:08 last edited by
    #1

    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??

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 16 Aug 2019, 08:15 last edited by
      #2

      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);
      }

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MasterBLB
        wrote on 16 Aug 2019, 08:34 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 16 Aug 2019, 08:47 last edited by
          #4

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MasterBLB
            wrote on 16 Aug 2019, 09:05 last edited by
            #5

            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;
            };
            
            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 16 Aug 2019, 09:30 last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MasterBLB
                wrote on 16 Aug 2019, 10:05 last edited by MasterBLB
                #7

                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."

                M 1 Reply Last reply 16 Aug 2019, 10:06
                0
                • M MasterBLB
                  16 Aug 2019, 10:05

                  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."

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 16 Aug 2019, 10:06 last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MasterBLB
                    wrote on 16 Aug 2019, 10:12 last edited by MasterBLB
                    #9

                    Ok, this paste page works:
                    .h https://paste.ofcode.org/uiz7UuRsAYhWJVzN2r76MQ
                    .cpp https://paste.ofcode.org/GsvM6EBgtXyqk2YuYKBCgs

                    1 Reply Last reply
                    1

                    4/9

                    16 Aug 2019, 08:47

                    • Login

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