Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QWidget how to receive keyPressEvent inside child widgets

Tags:

c++

qt

qt4

qwidget

I have one main Widget and inside this main widget I have QListWidget and two buttons. I have override the keyPressEvent inside the main widget (inherited from QWidget). I can receive the keyPress events when focus is not on QListWidget, but when focus is inside the QListWidget I am unable to receive these keyPress events. Below is the code I have used to achieve this:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setFocusPolicy(Qt::StrongFocus);

    ui->listWidget->addItem(new QListWidgetItem("Item1"));
    ui->listWidget->addItem(new QListWidgetItem("Item2"));
    ui->listWidget->addItem(new QListWidgetItem("Item3"));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    qDebug() << "event->key(): " << event->key();

    QWidget::keyPressEvent(event);
}
like image 428
gmuhammad Avatar asked Oct 14 '14 19:10

gmuhammad


People also ask

What is QWidget * parent?

The tree-like organization of QWidgets (and in fact of any QObjects ) is part of the memory management strategy used within the Qt-Framework. Assigning a QObject to a parent means that ownership of the child object is transferred to the parent object. If the parent is deleted, all its children are also deleted.

How do I change my QWidget position?

Use QWidget::move() to set the position, QWidget::resize() to set the size and reimplement the parent's resizeEvent() handler if you need to re-position the widgets if their parent resizes. Show activity on this post. You just need to create your widget, indicate its parent QWidget and then display it.

What is QWidget class window?

The QWidget class is the base class of all user interface objects.

How do you get children of QWidget?

You can use the findChild() function with the object name to get a specific child. You can also use findChildren() to get all the children that have the same name and then iterate through the list using foreach() or QListIterator . how do I use the QListIterator?


Video Answer


2 Answers

In header:

protected:

     bool eventFilter(QObject *obj, QEvent *event);

In constructor:

qApp->installEventFilter(this);

Filter:

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{   
    if (event->type() == QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
           qDebug() << "key " << keyEvent->key() << "from" << obj; 
    }
    return QObject::eventFilter(obj, event);
}

With this event filter you can catch all key press events and check who is emit event (obj)

For example:

if ( obj == ui->listWidget )
//event from QListWidget

Accordingly to your comment:

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{ 
    if (event->type() == QEvent::KeyPress)
    {
        if(obj == ui->listWidget)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if(keyEvent->key() == Qt::Key_Up)
                qDebug() << "Up";
            if(keyEvent->key() == Qt::Key_Down)
                qDebug() << "Down";
        }
    }
    return QObject::eventFilter(obj, event);
}
like image 86
Kosovan Avatar answered Sep 25 '22 19:09

Kosovan


You need to use event filters.

Sometimes an object needs to look at, and possibly intercept, the events that are delivered to another object. For example, dialogs commonly want to filter key presses for some widgets; for example, to modify Return-key handling.

The QObject::installEventFilter() function enables this by setting up an event filter, causing a nominated filter object to receive the events for a target object in its QObject::eventFilter() function. An event filter gets to process events before the target object does, allowing it to inspect and discard the events as required. An existing event filter can be removed using the QObject::removeEventFilter() function.

like image 23
Max Go Avatar answered Sep 24 '22 19:09

Max Go