&QT间期GT;如何设置ALT键激活

时间:2014-07-28 19:15:52

标签: c++ qt

有人可以解释如何将 Alt 键永久设置为活动状态吗? 我试图为Ubuntu申请,我需要让它活跃起来。 我想将它添加到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 );
}

我是C ++的新手,只是不太了解C ++是如何运作的,但我正在尝试。

2 个答案:

答案 0 :(得分:0)

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

基本上,当您在QMainWindow中创建菜单项并在命名中使用&时,它会为您创建一个自动快捷方式。

您可以制作明确的快捷方式,并将其与现有的QAction绑定。

我很快就会提供一些示例代码。

更新:以下内容应该为您完成。

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
        }
    }
}

更新:解决方案根据评论,通过示例代码。

基本上发生的事情是全局事件通知程序事先用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

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

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

    return a.exec();
}

希望有所帮助。

答案 1 :(得分:0)

你可以通过捕获和修改X11键盘事件来做到这一点,但你真的不想这样做:它是不可移植的,它容易出错,而且很简单。

然而,您可以做的是从QWidget重载keyPressEvent()函数。假设您想捕获MainWindow中的'S'键以激活保存(QMainWindow派生自QWidget,以防您想知道)。以下可能有效:

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

它捕获了's'和'S';根据需要添加Shift / Control检查。

请注意,这会在主窗口中抓取所有的'键;在输入框中键入名称会有问题。但是对话应该是安全的,因为它们不是来自QMainWindow。