Arrow keys on QT

Asked

Viewed 92 times

2

When we want to use Keyboard arrows in C++ we use the Ncurses or conio. h libraries, depending on the operating system.

But there is a way to do this using the QT libraries ?

  • Yes: http://doc.qt.io/qt-5/qkeyevent.html. "Key Events are sent to the widget with Keyboard input Focus when Keys are pressed or Released." Example here: http://programmingexamples.wikidot.com/qt-events#toc3

  • 1

    conio. h is not C++. It is a very old legacy of MS-DOS https://en.wikipedia.org/wiki/Conio.h

1 answer

3


Yes, using QKeySequence with QAction for example if you need to fire a SLOT specific, example in MainWindow.h would have the following slot:

private slots:
    void meuEvento();

And in the MainWindow.cpp would have this:

#include <QKeySequence>
#include <QAction>
#include <QDebug>

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

    QKeySequence seq = QKeySequence(tr("Ctrl+S"));

    QAction *act = new QAction(this);

    QObject::connect(act, SIGNAL(triggered()), this, SLOT(meuEvento()));

    this->addAction(act);

    act->setShortcut(seq);
}

void MainWindow::meuEvento()
{
    qDebug() << "TESTE";
}

In the example the third parameter is the this of QObject::connect, which refers to the class the slot is in meuEvento, but of course it can point out slots of other objects.

You can use a string with signal + as a separator QKeySequence(tr("Ctrl+S")) or use the constant, example:

QKeySequence(Qt::CTRL + Qt::Key_S);

More constant: http://doc.qt.io/archives/qt-4.8/qkeysequence.html#standard-shortcuts


I also developed a small library for more personal use, but which I share aims to "decrease the code writing a little":

Download at https://github.com/brcontainer/qt-helper/archive/master.zip, then extract the part that is important to you, in case the folder that will be used is the:

/application/keysequence/

In it there are 3 files:

  • keysequence.h
  • keysequence.cpp
  • keysequence.pri

Put the folder keysequence within your project and add this to your .pro

include($$PWD/keysequence/keysequence.pri)

So if you want to add an event to Mainwindow, do this:

#include "keysequence.h"

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

    KeySequence::widget(this, "Ctrl+W", this, SLOT(meuEvento()));
}

If you go to a specific widget do this:

ui->setupUi(this);

KeySequence::widget(ui->textField, "Ctrl+W", this, SLOT(meuEvento()));

In the Ctrl+W examples running the event, you can customize this or use QKeySequence::StandardKey

  • 1

    @Bacco and I thinking it was my mess and that I was rusty kkkkkkkkk, it’s been almost a year since I actually work with Qt. Blz, I’ll also do about Qkeyevent! : D Until later

  • 1

    Very good for a change! ) +1

  • I receiving a compliment from @Luizvieira I must be dreaming, it means I’m on the right track! : D thank you so much!

  • 1

    Ah, stop it! If I complimented you, it’s because you deserved it in my opinion. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.