简体   繁体   中英

Qt-> how to set ALT key active

Can someone please explain how to set the Alt key as active permanently? I'm trying to make an application for Ubuntu and I need to make it active. I want to add it to the code below in the if statement:

void MainWindow::on_checkBoxTitleBar_toggled(bool checked)
{
    settings->setValue("systemTitle", checked);
    ui->buttonMinimize->setVisible(!checked);
    ui->buttonClose->setVisible(!checked);


    if(!checked) {
        this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint );
        //here i whant the new code line for the ALT key
    } else {
        this->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint );
}

I'm new to C++, just don't really understand how C++ works, but I'm trying.

http://qt-project.org/doc/qt-5/QShortcut.html#details

Basically, when you create a menu item in QMainWindow and you use an & in your naming, it will create an automatic shortcut for you.

You can make explicit shortcuts and tie them to an existing QAction .

I'll put up some example code shortly.

Update: Something like the following should do it for you.

foreach(QAction * action, this->menuBar()->findChildren<QAction*>())
{
    foreach(QKeySequence * keySequence, action->shortcuts())
    {
        QString shortcutString = keySequence->toString();
        qDebug() << action->text() << "has shortcut" << shortcutString;
        if(shortcutString.contains("Alt+"))
        {
            action->setShortcut(QKeySequencekeySequence(shortcutString.remove("Alt+"));
            // you may want to also try setShortcuts()
            // if you want to support both with and without the alt key pressed
        }
    }
}

UPDATE: Solution according to comments, through example code.

Basically what happens is a global event notifier replaces all the keypresses with an Alt press beforehand, and modifies the keypress with an Alt Modifier.

application.h

#ifndef APPLICATION_H
#define APPLICATION_H

#include <QApplication>
#include <QEvent>
#include <QWidget>
#include <QCheckBox>
#include <QKeyEvent>
#include <QDebug>
#include <QKeySequence>
#include <QShortcutEvent>
#include <QWindow>
#include <QShortcut>

class Application : public QApplication
{
public:
    explicit Application(int &argc, char** argv): QApplication(argc, argv)
    {
        m_checkBox = 0;
    }
    void setCheckBoxLink(QCheckBox * checkBox)
    {
        m_checkBox = checkBox;
    }
private:
    bool notify(QObject *receiver_, QEvent *e)
    {
        if(m_checkBox == 0 || !m_checkBox->isChecked())
            return QApplication::notify(receiver_, e);

        QKeyEvent * ke, *ke2;
        QShortcutEvent * se;
        switch (e->type())
        {
        case QEvent::KeyPress:
//            qDebug() << Q_FUNC_INFO << e->type();
            ke = static_cast<QKeyEvent *>(e);
            if((ke->modifiers() & Qt::AltModifier) == 0)
            {
                qDebug() << "No Alt";
                ke2 = new QKeyEvent(ke->type(), 0, Qt::AltModifier);
                this->postEvent(receiver_, ke2);
                ke->setModifiers(Qt::AltModifier);
            }
            else
            {
                qDebug() << "Alt";
            }
            break;
        case QEvent::KeyRelease:
        default:
            break;
        }
        return QApplication::notify(receiver_, e);
    }

    QCheckBox * m_checkBox;
};

#endif // APPLICATION_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QVBoxLayout>
#include "application.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QShortcut>
#include <QMainWindow>
#include <QStatusBar>
#include <QDebug>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:

    MainWindow(QWidget *parent = 0) : QMainWindow(parent)
    {
        this->setCentralWidget(new QWidget());
        this->centralWidget()->setLayout(new QVBoxLayout());

        QCheckBox * checkBox = new QCheckBox("Alt Key");

        this->centralWidget()->layout()->addWidget(checkBox);

        ((Application*)qApp)->setCheckBoxLink(checkBox);

        QMenu * menu = new QMenu("E&xample");
        this->menuBar()->addMenu(menu);
        menu->addAction("Slot &1", this, SLOT(on_slot1()));
        menu->addAction("Slot &2", this, SLOT(on_slot2()));
        menu->addAction("Slot &3", this, SLOT(on_slot3()));
        menu->addAction("Slot &4", this, SLOT(on_slot4()));
        menu->addAction("Slot &5", this, SLOT(on_slot5()));
        menu->addAction("Slot &6", this, SLOT(on_slot6()));
        menu->addAction("Slot &A", this, SLOT(on_slotA()));
        menu->addAction("Slot &B", this, SLOT(on_slotB()));
        menu->addAction("Slot &C", this, SLOT(on_slotC()));
        menu->addAction("Slot &D", this, SLOT(on_slotD()));
        menu->addAction("Slot &E", this, SLOT(on_slotE()));
        menu->addAction("Slot &F", this, SLOT(on_slotF()));
    }

    ~MainWindow(){}

public slots:
    void on_slot1(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slot2(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slot3(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slot4(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slot5(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slot6(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slotA(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slotB(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slotC(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slotD(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slotE(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
    void on_slotF(){ qDebug() << Q_FUNC_INFO; this->statusBar()->showMessage(Q_FUNC_INFO); }
};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include "application.h"

int main(int argc, char *argv[])
{
    Application a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

Hope that helps.

You might be able to do it by capturing and modifying the X11 keyboard events, but you really don't want to do that: it's non-portable, it's error prone and just plain ugly.

What you can do, however, is overload the keyPressEvent() function from QWidget. Say you want to capture the 'S' key in your MainWindow to active a save (QMainWindow is derived from QWidget, in case you're wondering). The following might work:

void MyMainWindow::keyPressEvent(QKeyEvent *event)
{
  if (event->key() == Qt::Key_S)
  {
    save();
  }
  else
  {
    QMainWindow::keyPressEvent(event); // process other keys normally
  }
}

This captures both 's' and 'S'; add checks for Shift/Control as you see fit.

Note that this will grab all 's' keys in your main window; typing a name in an input box would be problematic. But dialogs should be safe since they are not derived from QMainWindow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM