简体   繁体   English

Qt->如何设置ALT键为活动状态

[英]Qt-> how to set ALT key active

Can someone please explain how to set the Alt key as active permanently? 有人可以解释一下如何将Alt键永久设置为活动状态吗? I'm trying to make an application for Ubuntu and I need to make it active. 我正在尝试为Ubuntu创建一个应用程序,我需要将其激活。 I want to add it to the code below in the if statement: 我想将其添加到if语句中的以下代码中:

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. 我是C ++的新手,只是不太了解C ++的工作原理,但我正在尝试。

http://qt-project.org/doc/qt-5/QShortcut.html#details 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. 基本上,当您在QMainWindow创建菜单项并在名称中使用&时,它将为您创建一个自动快捷方式。

You can make explicit shortcuts and tie them to an existing QAction . 您可以创建明确的快捷方式,并将其绑定到现有的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. 基本上,发生的事情是全局事件通知程序预先将所有按键替换为Alt按键,并使用Alt修饰符修改按键。

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 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. 您可能可以通过捕获和修改X11键盘事件来做到这一点,但是您确实不想这样做:它是不可移植的,容易出错,而且很丑陋。

What you can do, however, is overload the keyPressEvent() function from QWidget. 但是,您可以做的是从QWidget重载keyPressEvent()函数。 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). 假设您要捕获MainWindow中的'S'键以激活保存(如果您想知道,QMainWindow是从QWidget派生的)。 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'; 这同时捕获了“ s”和“ S”; add checks for Shift/Control as you see fit. 根据需要添加对Shift / Control的检查。

Note that this will grab all 's' keys in your main window; 注意,这将在您的主窗口中获取所有的 's'键。 typing a name in an input box would be problematic. 在输入框中输入名称会出现问题。 But dialogs should be safe since they are not derived from QMainWindow. 但是对话框应该是安全的,因为它们不是从QMainWindow派生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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