Couldn't find "Keypress" in QTableView?

I am using QTableView

to render some specific images. The user can click on the TableView images and that image will be enlarged in another larger window like QLabel

. I can make this possible using mouse clicks in the TableView, but I would like to enable it for the keyboard up " and down> keys . I mean that after the user clicks on one of the images listed in the TableView, if the user will switch to other images with the up and down keyboard keys , I want to detect a key press and connect it to QLabel

that enlarges that particular selected image.

So, I mean what I really want to discover keypress

on QTableView

. So far I have not succeeded. I set eventfilter

in viewPort

QTableView

, and in the function eventfilter

I can detect mousebuttonpress

, but I cannot detect keypress

.

To show how I'm getting closer to implementation, I made a simple program to test with QTableView

and keypress

. Below I have given the implementation code for mainWindow

this simple program.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStandardItemModel>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->numberTable->viewport()->installEventFilter(this);
    connect(ui->FillUp, SIGNAL(clicked()), this, SLOT(fillUp()));
}

void MainWindow::fillUp()
{
    model = new QStandardItemModel(3, 3, this);
    int counter = 0;
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            QStandardItem * itm = new QStandardItem;
            counter++;
            itm->setText(QString::number(counter));
            model->setItem(i, j, itm);
        }
    }
    ui->numberTable->setModel(model);
    ui->numberTable->show();
}

bool MainWindow::eventFilter(QObject * obj, QEvent * ev)
{
    if(obj == ui->numberTable->viewport())
    {
        if(ev->type() == QEvent::MouseButtonPress)
        {
            qDebug()<<"Mouse button pressed!\n";
        }
        else if(ev->type() == QEvent::KeyPress)
        {
            qDebug()<<"Key button pressed\n";
        }
    }
    return QObject::eventFilter(obj, ev);
}

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

      

Programs display "mouse button pressed" but no exit for a key press. Could you please let me know where I am making a mistake?

thank

+3


source to share


1 answer


I have the same problem. Using your idea, print the type of event I get: "Paint Widget" (12) instead of "KeyPress" (6).

bool AR_Principal::eventFilter(QObject * obj, QEvent * ev){
    qDebug() << ev->type();
    if(obj == ui->tableView->viewport())
    {
        if(ev->type() == QEvent::MouseButtonPress)
        {
            qDebug()<<"Mouse button pressed";
        }
        else if(ev->type() == QEvent::KeyPress)
        {
            qDebug()<<"Key button pressed";
        }
        else if(ev->type() == QEvent::Paint)
        {
            qDebug()<<"Paint widget" ;
        }
    }
    return QObject::eventFilter(obj, ev);
}

      

If the QEvent :: Paint event is used, this works. Or, as in the other answer, add:

ui->tableView->installEventFilter(this);

      

And don't use the condition:



if(obj == ui->tableView->viewport())

      

But a more efficient solution:

connect(ui->tableView->selectionModel(), SIGNAL(currentChanged (const QModelIndex & , const QModelIndex & )), SLOT(selectedItem(const QModelIndex &)));

      

If selectedItem (const QModelIndex &) is a private slot function where you can do anything with the selected data (using their index)

+1


source







All Articles