How to connect a Key_Enter signal with a slot ?
Solved
General and Desktop
-
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 ! =) -
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()));