all.west
all.west

Reputation: 81

Emit a signal when the user changes of row with the keyboard (Key_Up and Key_Down) in QTableWidget

I don't know how to emit a signal when the user changes of row with the keyboard by pressing up or down arrow key in QTableWidget. And after, I will have to use this signal to make some changes to my video.

How can I implement that?

Upvotes: 1

Views: 1708

Answers (3)

IAmInPLS
IAmInPLS

Reputation: 4125

Subclass the class QTableWidget by adding two signals :

class myTableWidget: public QTableWidget
{
  public:
    myTableWidget() {}
    ~myTableWidget() {}

  private:
   void keyPressEvent(QKeyEvent* event)
   {
        if(event->key() == Qt::Key_Up)
            emit keyUpPressed();
        else if(event->key() == Qt::Key_Down)
            emit keyDownPressed();    
        else
            QWidget::keyPressEvent(event);
    }

    signals :
        keyUpPressed();
        keyDownPressed();
}; 

In your class where you use the video, add two slots (and let's say your class is named yourClass) :

public slots :
    void onKeyUpPressed();
    void onKeyDownPressed();

Now you can use the signals and slots connection in your main class like this :

myTableWidget* table = new myTableWidget();
connect(table, SIGNAL(keyUpPressed()), this, SLOT(onKeyUpPressed()));
connect(table, SIGNAL(keyDownPressed()), this, SLOT(onKeyDownPressed()));

Then, in your slots, you can process your video.

void yourClass::onKeyUpPressed()
{
    // do something here
}

void yourClass::onKeyDownPressed()
{
    // do something else here
}

An alternative is to install an event filter.

EDIT : from your comment, your class inherits QStyledItemDelegate so you can just override the function eventFilter like this :

bool myTableWidget::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::KeyPress) 
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_Up)
            emit keyUpPressed();
        else if(event->key() == Qt::Key_Down)
            emit keyDownPressed(); 
    }
    // standard event processing
    return QStyledItemDelegate::eventFilter(obj, event);
}

Then, it is the same for the slots in yourClass.

Upvotes: 1

Shtol Krakov
Shtol Krakov

Reputation: 1280

To catch a signal from key event you can inherit from QTableWidget and override method keyPressEvent(QKeyEvent *event), for example:

.H file:

class CustomTableWidget : public QTableWidget
{
    Q_OBJECT
protected:
    void keyPressEvent(QKeyEvent *event);
public:
    explicit CustomTableWidget(QWidget *parent = 0);
    ~CustomTableWidget();

signals:
    void upEvent(const QModelIndex &index);
    void downEvent(const QModelIndex &index);

.CPP file:

void CustomTableWidget::keyPressEvent(QKeyEvent *event)
{
    switch(event->key()) {
    case Qt::Key_Up: emit upEvent(currentIndex());
        break;
    case Qt::Key_Down: emit downEvent(currentIndex());
        break;
    default: QTableWidget::keyPressEvent(event);
    }
}

Upvotes: 1

David
David

Reputation: 1520

You probably want to connect to to signal currentCellChanged this will deal with both mouse clicks and keyboard movement and give you the indices of the newly selected cell.

Upvotes: 1

Related Questions