仅在从命令行构建 Qt 项目时出错
Error only when building Qt project from command line
我有一个大问题。我创建了一个虚拟项目来隔离我的错误。
因为是一个有 6 个文件的项目,无法在此处添加所有代码,所以我创建了一个 github project 并在此处添加了所有代码,同时描述了这里的想法。你可以看看那里的代码。
main.cpp的源代码:
#include <QApplication>
#include <QObject>
#include "mainwindow.h"
#include "testclass.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
TestClass c;
QObject::connect(&w, &MainWindow::mySignal,
&c, &TestClass::mySlot);
w.show();
return a.exec();
}
Class MainWindow
有一个 Q_SIGNAL
,当我按下 from 上的按钮时会发出, mySlot
只是 Q_SLOT
in TestClass
(继承 QObject
)qDebug
是一条消息。
如果我从 QtCreator
构建项目,我的项目构建良好,但是当我从命令行构建它时,我收到一个奇怪的错误。
我从命令行做了什么:
$ qmake QMAKE_TEST.pro
$ make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o main.o main.cpp
In file included from main.cpp:4:0:
mainwindow.h: In function ‘int main(int, char**)’:
mainwindow.h:19:7: error: ‘void MainWindow::mySignal()’ is protected
void mySignal();
^
main.cpp:14:36: error: within this context
QObject::connect(&w, &MainWindow::mySignal,
^
main.cpp:15:41: error: no matching function for call to ‘QObject::connect(MainWindow*, void (MainWindow::*)(), TestClass*, void (TestClass::*)())’
&c, &TestClass::mySlot);
^
main.cpp:15:41: note: candidates are:
In file included from /usr/include/qt4/QtCore/qcoreapplication.h:45:0,
from /usr/include/qt4/QtGui/qapplication.h:45,
from /usr/include/qt4/QtGui/QApplication:1,
from main.cpp:1:
/usr/include/qt4/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/qt4/QtCore/qobject.h:204:17: note: no known conversion for argument 2 from ‘void (MainWindow::*)()’ to ‘const char*’
/usr/include/qt4/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/qt4/QtCore/qobject.h:217:17: note: no known conversion for argument 2 from ‘void (MainWindow::*)()’ to ‘const QMetaMethod&’
/usr/include/qt4/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/qt4/QtCore/qobject.h:337:13: note: no known conversion for argument 2 from ‘void (MainWindow::*)()’ to ‘const char*’
make: *** [main.o] Error 1
$
$
首先为什么说 mySignal
si protected
因为据我所知所有 Q_SIGNALS
都是 public?其次,如果我从 运行 从 Qt Creator
得到它,为什么它可以完美运行,而当我从命令行 运行 得到它时却出现错误?
有人可以帮我吗?
发现问题...我正在使用 qmake
版本 4 构建一个 Qt5.5
项目...
我有一个大问题。我创建了一个虚拟项目来隔离我的错误。 因为是一个有 6 个文件的项目,无法在此处添加所有代码,所以我创建了一个 github project 并在此处添加了所有代码,同时描述了这里的想法。你可以看看那里的代码。
main.cpp的源代码:
#include <QApplication>
#include <QObject>
#include "mainwindow.h"
#include "testclass.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
TestClass c;
QObject::connect(&w, &MainWindow::mySignal,
&c, &TestClass::mySlot);
w.show();
return a.exec();
}
Class MainWindow
有一个 Q_SIGNAL
,当我按下 from 上的按钮时会发出, mySlot
只是 Q_SLOT
in TestClass
(继承 QObject
)qDebug
是一条消息。
如果我从 QtCreator
构建项目,我的项目构建良好,但是当我从命令行构建它时,我收到一个奇怪的错误。
我从命令行做了什么:
$ qmake QMAKE_TEST.pro
$ make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o main.o main.cpp
In file included from main.cpp:4:0:
mainwindow.h: In function ‘int main(int, char**)’:
mainwindow.h:19:7: error: ‘void MainWindow::mySignal()’ is protected
void mySignal();
^
main.cpp:14:36: error: within this context
QObject::connect(&w, &MainWindow::mySignal,
^
main.cpp:15:41: error: no matching function for call to ‘QObject::connect(MainWindow*, void (MainWindow::*)(), TestClass*, void (TestClass::*)())’
&c, &TestClass::mySlot);
^
main.cpp:15:41: note: candidates are:
In file included from /usr/include/qt4/QtCore/qcoreapplication.h:45:0,
from /usr/include/qt4/QtGui/qapplication.h:45,
from /usr/include/qt4/QtGui/QApplication:1,
from main.cpp:1:
/usr/include/qt4/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/qt4/QtCore/qobject.h:204:17: note: no known conversion for argument 2 from ‘void (MainWindow::*)()’ to ‘const char*’
/usr/include/qt4/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/qt4/QtCore/qobject.h:217:17: note: no known conversion for argument 2 from ‘void (MainWindow::*)()’ to ‘const QMetaMethod&’
/usr/include/qt4/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/qt4/QtCore/qobject.h:337:13: note: no known conversion for argument 2 from ‘void (MainWindow::*)()’ to ‘const char*’
make: *** [main.o] Error 1
$
$
首先为什么说 mySignal
si protected
因为据我所知所有 Q_SIGNALS
都是 public?其次,如果我从 运行 从 Qt Creator
得到它,为什么它可以完美运行,而当我从命令行 运行 得到它时却出现错误?
有人可以帮我吗?
发现问题...我正在使用 qmake
版本 4 构建一个 Qt5.5
项目...