Error: QAction::triggered is protected – cannot connect a signal

Error: QAction::triggered is protected – cannot connect a signal

我在 Kubuntu 上使用 Qt 5 Widgets 构建了一个带有菜单的应用程序,在该菜单中我将操作与 class StateMachine:

中的一个函数相关联
QObject::connect(
    ui->actionOpen, &QAction::triggered, &sm, &StateMachine::open_file);

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

这适用于 Kubuntu 上的 g++ 4.9。我现在尝试在只有 g++ 4.8 的 openSUSE 13.1 上编译它,我在那里找不到 Qt 5 的头文件。所以我只是尝试使用默认存储库中可用的 Qt 4.8 来构建它。然后我得到这个错误:

/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 documentation,看起来 triggered(bool) 是一个 public 信号。然后我查看了文件 /usr/include/QtGui/qaction.h,发现它看起来像这样:

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 块下方,但这些信号应该是 public.

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

看起来您正在使用 Qt5 signal slot connection syntax 和 Qt 4.x。 只需尝试使用 Qt 4.x 连接语法:

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