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. Propagate TAB key from delegate to custom editor

Propagate TAB key from delegate to custom editor

Scheduled Pinned Locked Moved Unsolved General and Desktop
delegateevent
5 Posts 4 Posters 1.6k 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.
  • CarloEsteC Offline
    CarloEsteC Offline
    CarloEste
    wrote on last edited by
    #1

    Hi to Everyone!
    I'm using a standard model/view pattern and delegates to provide a custom editor. I need to handle the Tab key, in specific I would like to cycle a set of data inside the editor. Unfortunately I'm not able to propagate the keyEvent to the editor, I read that the standard delegate eventFilter handles the Tab key so I reimplemented the bool eventFilter(QObject* , QEvent*)method returning false for the Tab key but the event doesn't reach the custom editor. How can I solve this issue?
    I provide an example code takenfrom the Qt examples - QSpinBoxDelegate, where I modified the file "delegate.cpp"

    class MyEditor : public QSpinBox
    {
    public:
        MyEditor(QWidget* parent = 0)
            : QSpinBox(parent)
        {
    
        }
    
    private:
        void keyPressEvent(QKeyEvent* e)
        {
            if(e->key() == Qt::Key_Tab)
            {
                qDebug() << Q_FUNC_INFO;
            }
            else
            {
                QSpinBox::keyPressEvent(e);
            }
        }
    };
    
    SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
        : QStyledItemDelegate(parent)
    {
    }
    
    bool SpinBoxDelegate::eventFilter(QObject *object, QEvent *event)
    {
        if(event->type() == QEvent::KeyPress)
        {
            QKeyEvent* keyEv = static_cast<QKeyEvent*>(event);
            if(keyEv->key() == Qt::Key_Tab)
            {
                return false;
            }
        }
    
            QStyledItemDelegate::eventFilter(object, event);
    
    }
    
    QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
        const QStyleOptionViewItem &/* option */,
        const QModelIndex &/* index */) const
    {
        MyEditor *editor = new MyEditor(parent);
        editor->setFrame(false);
        editor->setMinimum(0);
        editor->setMaximum(100);
    
        return editor;
    }
    

    Thank you in advance for any suggestions.
    Carlo

    1 Reply Last reply
    0
    • P Offline
      P Offline
      panosk
      wrote on last edited by
      #2

      @CarloEste

      Hello,

      Instead of this

      if(keyEv->key() == Qt::Key_Tab)
           {
               return false;
           }
      

      Try this:

      if(keyEv->key() == Qt::Key_Tab)
           {
               keyEv->ignore();
           }
      
      1 Reply Last reply
      0
      • CarloEsteC Offline
        CarloEsteC Offline
        CarloEste
        wrote on last edited by
        #3

        I tried, but it doesn't work.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          djszapi
          wrote on last edited by
          #4

          Hi,

          I am experiencing the same. Have you ever sorted this out what filters out the tab from your delegate editor?

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Just to understand, is the delegate (assume it's a delegate with 2 line edits) in editing mode (e.g. after you type in the first line edit you want to go in the second) or just in paint mode (e.g. you want to press tab and go in the first lineedit of the following cell)?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            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