首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:QAction::已触发保护-无法连接信号

错误:QAction::已触发保护-无法连接信号
EN

Stack Overflow用户
提问于 2015-05-10 11:58:46
回答 1查看 1.5K关注 0票数 3

我在Kubuntu上使用Qt 5小部件构建了一个应用程序,在这个应用程序中,我用类StateMachine中的一个函数连接了这个操作

代码语言:javascript
运行
复制
QObject::connect(
    ui->actionOpen, &QAction::triggered, &sm, &StateMachine::open_file);

很抱歉,我不能分享这里的所有代码,这是一个商业项目。

在Kubuntu上使用g++ 4.9可以很好地工作。我现在试着在openSUSE 13.1上编译它,它只有g++ 4.8,我在那里找不到Qt 5的头。因此,我尝试用QT4.8构建它,QT4.8在默认存储库中是可用的。然后我得到了这个错误:

代码语言:javascript
运行
复制
/usr/include/QtGui/qaction.h: In constructor ‘MainWindow::MainWindow(QWidget*)’:
/usr/include/QtGui/qaction.h:228:10: error: ‘void QAction::triggered(bool)’ is protected
     void triggered(bool checked = false);
          ^
/project/gui/mainwindow.cpp:26:35: error: within this context
         ui->actionOpen, &QAction::triggered, &sm, &StateMachine::open_file);
                                   ^
/project/gui/mainwindow.cpp:26:75: error: no matching function for call to ‘MainWindow::connect(QAction*&, void (QAction::*)(bool), StateMachine*, void (StateMachine::*)())’
         ui->actionOpen, &QAction::triggered, &sm, &StateMachine::open_file);
                                                                           ^
/project/gui/mainwindow.cpp:26:75: note: candidates are:
In file included from /usr/include/QtCore/QObject:1:0,
                 from /project/gui/AxisState.hpp:9,
                 from /project/gui/projectionview.h:7,
                 from /project/gui/mainwindow.h:5,
                 from /project/gui/mainwindow.cpp:3:
/usr/include/QtCore/qobject.h:204:17: note: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
     static bool connect(const QObject *sender, const char *signal,
                 ^
/usr/include/QtCore/qobject.h:204:17: note:   no known conversion for argument 2 from ‘void (QAction::*)(bool)’ to ‘const char*’
/usr/include/QtCore/qobject.h:217:17: note: static bool QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
     static bool connect(const QObject *sender, const QMetaMethod &signal,
                 ^
/usr/include/QtCore/qobject.h:217:17: note:   no known conversion for argument 2 from ‘void (QAction::*)(bool)’ to ‘const QMetaMethod&’
/usr/include/QtCore/qobject.h:337:13: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
 inline bool QObject::connect(const QObject *asender, const char *asignal,
             ^
/usr/include/QtCore/qobject.h:337:13: note:   no known conversion for argument 2 from ‘void (QAction::*)(bool)’ to ‘const char*’
In file included from /usr/include/QtGui/QAction:1:0,
                 from /project/gui/ui_mainwindow.h:13,
                 from /project/gui/mainwindow.cpp:5:
/usr/include/QtGui/qaction.h:228:10: error: ‘void QAction::triggered(bool)’ is protected
     void triggered(bool checked = false);
          ^

我查看了QAction 4.8文档,看起来triggered(bool)是一个公共信号。然后我查看了/usr/include/QtGui/qaction.h文件,发现它如下所示:

代码语言:javascript
运行
复制
protected:                                                                         
    bool event(QEvent *);                                                          
    QAction(QActionPrivate &dd, QObject *parent);                                  

public Q_SLOTS:                                                                    
#ifdef QT3_SUPPORT                                                                 
    inline QT_MOC_COMPAT void setOn(bool b) { setChecked(b); }                     
#endif                                                                             
    void trigger() { activate(Trigger); }                                          
    void hover() { activate(Hover); }                                              
    void setChecked(bool);                                                         
    void toggle();                                                                 
    void setEnabled(bool);                                                         
    inline void setDisabled(bool b) { setEnabled(!b); }                            
    void setVisible(bool);                                                         

Q_SIGNALS:                                                                         
    void changed();                                                                
    void triggered(bool checked = false);                                          
    void hovered();     

因此,它位于Q_SIGNALS块中,它确实出现在protected块下面,但是这些信号应该是公开的。

我怎样才能让这个程序编译?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-10 12:15:02

看上去就像在QT4.x中使用Qt5信号时隙连接语法一样。只需尝试使用QT4.x连接语法:

QObject::connect(ui->actionOpen, SIGNAL(triggered()), &sm, SLOT(open_file()));

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30151127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档