快捷方式未被触发

时间:2013-01-15 16:06:56

标签: qt qt4

这是我的班级声明:

class Browser : public QWidget {    
    Q_OBJECT
    public:
        Browser(QWidget *parent = 0);
        QStringList loadSettings(QString settings_file);
    private slots:
        void toggleFullscreen();               
    private:     
        void createActions();
        QAction *aToggleFullscreen;            
};

相关和定义:

void Browser::toggleFullscreen() {
    out << "fullscreen!" << endl;
}   
void Browser::createActions() {
    aToggleFullscreen = new QAction(this);
    aToggleFullscreen->setShortcut(tr("F11"));    
    connect(aToggleFullscreen, SIGNAL(triggered()), this, SLOT(toggleFullscreen()));    
}

我正在createActions()致电Browser::Browser

我没有运行时警告说插槽不存在等等。但如果我点击F11,仍然没有触发任何内容。

我也试过了:

aToggleFullscreen->setShortcut(QString("F11"));

我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

您忘记将您的操作实际添加到浏览器中,因此它永远不会被触发。这应该有效:

void Browser::createActions() {
    aToggleFullscreen = new QAction(this);
    aToggleFullscreen->setShortcut(tr("F11"));    
    connect(aToggleFullscreen, SIGNAL(triggered()), this, SLOT(toggleFullscreen()));
    addAction(aToggleFullscreen);    
}