gpt4 book ai didi

c++ - 错误:QAction::triggered protected ——无法连接信号

转载 作者:搜寻专家 更新时间:2023-10-31 02:22:37 24 4
gpt4 key购买 nike

我在 Kubuntu 上使用 Qt 5 Widgets 构建了一个带有菜单的应用程序,在该菜单中我将操作与我的类 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 block 中,它确实出现在 protected block 下方,但这些信号应该是公开的。

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

最佳答案

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


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

关于c++ - 错误:QAction::triggered protected ——无法连接信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30151127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com