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 connect a Key_Enter signal with a slot ?
Forum Update on Monday, May 27th 2025

How to connect a Key_Enter signal with a slot ?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 873 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.
  • C Offline
    C Offline
    coilo
    wrote on last edited by
    #1

    Hello guys,

    My GUI contains a QTreeWidget, a QtableWidget, and a QPushbutton, and I would like when an item is selected in the tree to add it in the table when I press enter with my key.

    For the moment all the functions work but I have to click on the add button to add it and I would like to be able to press on my keyboard directly to trigger the event of the add button.

    I saw that you have to create an event filter for this and I could find this on the internet :

    bool MainWindow::eventFilter(QObject* obj, QEvent* event)
    {
        if (event->type()==QEvent::KeyPress)
        {
            QKeyEvent* key = static_cast<QKeyEvent*>(event);
            if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) //Enter or return was pressed
            {
               QObject::connect(obj,SIGNAL(activated()),this,SLOT(add_item())); //Here is the problem i have the slot "add_item" which work well, but i don't know what to write for the signal. My signal would be when key_enter is pressed but i dont know how to write
    
            }
            else
            {
                return QObject::eventFilter(obj, event);
            }
            return true;
        }
        else {
            return QObject::eventFilter(obj, event);
        }
        return false;
    }
    
    

    I have written in the comment code where is the problem.
    Do you have any suggestion ?
    Thanks a lot for your help ! =)

    1 Reply Last reply
    0
    • C Offline
      C Offline
      coilo
      wrote on last edited by
      #2

      Okay i have solved my problem, i went with the QShortcut solution which is more easier.
      Here is the code which work:

         QShortcut *returnShortcut = new QShortcut(QKeySequence("Return"), ui->tableWidget);
         QObject::connect(returnShortcut, SIGNAL(activated()), btn_add, SLOT(click()));
      
      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