x610
x610

Reputation: 131

C++ QT OSX Qt::META+Qt::Key_Tab shortcut bind

I’m trying to bind Qt::META + Qt::Key_Tab shortcut in QTabWidget to switch tabs (like it works in chrome or many other applications). I have tried every single solution found in google, but this shortcut combination is not working.

I have tried:

QWidget::event/QWidget::eventFilter catches Shift+Tab, Alt+Tab, but not Ctrl(META)+Tab. When I press Ctrl I see my qDebug output, when I press Ctrl + Tab nothing happens.

Can somebody explain me what is wrong and so special with this particular key combination in QT on OSX?

Doesn't matter what widget, I have created clean GUI project with no other widgets in it - still the same.

Some information:

BTW, In Qt Creator I’m not able to set Ctrl+Tab shortcut either, thats really ridiculous.

Note: It works great on Windows, it doesn't work on OSX!

I appreciate any help.

Simple code with QAction:

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


    QAction *pAction = new QAction(this);
    QKeySequence keySequence = Qt::META + Qt::Key_Tab; // Not working
    // or
    QKeySequence keySequence = Qt::ALT + Qt::Key_Tab; // Works Alt+Tab
    // or
    QKeySequence keySequence = QKeySequence(Qt::Key_Meta, Qt::Key_Tab); // Not working
    // or
    QKeySequence keySequence = QKeySequence(Qt::META, Qt::Key_Tab); // Not working
    pAction->setShortcut(keySequence);

    connect(pAction, SIGNAL(triggered()), this, SLOT(shortcut_NextTab()));

    addAction(pAction);

}

And slot function:

void MainWindow::shortcut_NextTab()
{
    qDebug() << "LOL";
}

Expecting to see LOL in Application output, when pressing Ctrl+Tab.

Upvotes: 7

Views: 1411

Answers (2)

Tom Panning
Tom Panning

Reputation: 4772

This seems to be a bug in Qt on Cocoa. See QTBUG-8596 and QTBUG-12232. The first bug report has a comment that says that it works if you add the QAction to the menu. I was experiencing the same problem as you, and that solution worked for me.

Upvotes: 3

djechlin
djechlin

Reputation: 60768

In this line:

QKeySequence keySequence = Qt::Key_Meta + Qt::Key_Tab;

You are just adding integers. Per QT documentation:

QKeySequence::QKeySequence ( int k1, int k2 = 0, int k3 = 0, int k4 = 0 )
Constructs a key sequence with up to 4 keys k1, k2, k3 and k4. The key codes are listed in Qt::Key and can be combined with modifiers (see Qt::Modifier) such as Qt::SHIFT, Qt::CTRL, Qt::ALT, or Qt::META.

Meaning:

  • if you want a sequence, you need to use two-argument constructor to QKeySequence, not just add two integers together (which is what you are doing) and use one-argument constructor.
  • If you want a modifier - which I assume means holding a key down - use QT::Modifier, not Qt::Key_*.

Upvotes: -1

Related Questions