3

I build an application using Qt 5 Widgets on Kubuntu with a menu where I have connected the action with a function in my class StateMachine:

QObject::connect(
    ui->actionOpen, &QAction::triggered, &sm, &StateMachine::open_file);

I am sorry that I cannot share all the code here, it is a commercial project.

This works fine with g++ 4.9 on Kubuntu. I now tried to compile this on openSUSE 13.1 which only has g++ 4.8 and I could not find headers for Qt 5 there. So I just tried to build it with Qt 4.8 which is available there in the default repository. Then I get this error:

/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);
          ^

I looked into the QAction 4.8 documentation and it looks like triggered(bool) is a public signal. Then I looked into the file /usr/include/QtGui/qaction.h and found that it looks like this:

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();     

So it is in a Q_SIGNALS block, which does appear below a protected block, but those signals should be public.

How can I get this program to compile?

Martin Ueding
  • 8,245
  • 6
  • 46
  • 92

1 Answers1

2

Look's like you are using Qt5 signal slot connection syntax with Qt 4.x. Just try to use Qt 4.x connection syntax:

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

Nick Bondarenko
  • 6,211
  • 4
  • 35
  • 56