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);
}
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.
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.
The QWidget class is the base class of all user interface objects.
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?
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With