C++ Fortran绑定C++/Qt:QTimer问题“;QObject::startTimer:QTimer只能用于以QThread”;
我需要创建一个简单的Fortran程序来绑定一个复杂的Qt程序 我遇到的唯一问题是使用C++ Fortran绑定C++/Qt:QTimer问题“;QObject::startTimer:QTimer只能用于以QThread”;,c++,qt,fortran,qtimer,C++,Qt,Fortran,Qtimer,我需要创建一个简单的Fortran程序来绑定一个复杂的Qt程序 我遇到的唯一问题是使用QTimer(这是强制性的):当我使用Fortran中的QTimer调用函数时,我总是收到以下错误消息: QObject::startTimer:QTimer只能用于以QThread启动的线程 所有其他“复杂”的东西(QUdpSocket,类的利用…)都能正常工作 我怎样才能解决这个问题?在错误消息之后,我想我应该找到一个技巧来使用Fortran的QThread,但我不知道怎么做。(我还尝试从QThread继承
QTimer
(这是强制性的):当我使用Fortran中的QTimer
调用函数时,我总是收到以下错误消息:
QObject::startTimer:QTimer只能用于以QThread启动的线程
所有其他“复杂”的东西(QUdpSocket
,类的利用…)都能正常工作
我怎样才能解决这个问题?在错误消息之后,我想我应该找到一个技巧来使用Fortran的QThread
,但我不知道怎么做。(我还尝试从QThread
继承dummy,而不是QObject
:也不起作用)
下面是一个简单的例子。我现在给出输出,然后是源代码
直接启动C++/Qt程序时的输出
begin
hello
end
hello
hello
... // one "hello" per second
begin
QObject::startTimer: QTimer can only be used with threads started with QThread
end
启动f90程序时的输出
begin
hello
end
hello
hello
... // one "hello" per second
begin
QObject::startTimer: QTimer can only be used with threads started with QThread
end
main.cpp
#include <QApplication>
#include <QtGui>
#include "dummyclass.hpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DummyClass*ds = new DummyClass();
Q_UNUSED(ds);
return app.exec();
}
#include <QTimer>
#include <QObject>
#include <QApplication>
#include <QTextStream>
class dummyclass : public QObject // public QThread
{
Q_OBJECT
public:
DummyClass();
private:
QTimer timer;
private slots:
void hello();
}
#include "dummyclass.hpp"
DummyClass::DummyClass()
{
QTextStream(stdout) << "begin" << endl;
connect(&timer, SIGNAL(timeout()), this, SLOT(hello()));
timer.start(1000);
QTextStream(stdout) << "endl" << endl;
}
void DummyClass::hello()
{
QTextStream(stdout) << "Hello !" << endl;
}
虚拟_模块.f90
program dummy_main
use dummy_module
use, intrinsic :: ISO_C_Binding, only: C_CHAR, C_NULL_CHAR
type(dummy_type) :: dummy
call newDummy(dummy)
end program dummy_main
module dummy_module
use, intrinsic :: ISO_C_Binding, only: C_CHAR, C_NULL_CHAR
use, intrinsic :: ISO_C_Binding, only: C_ptr, C_NULL_ptr
implicit none
private
type dummy_type
private
type(C_ptr) :: object = C_NULL_ptr
end type dummy_type
!------------------------
! C function declarations
!------------------------
interface
function C_dummyClass__new_ () result(this) bind(C,name="dummyClass__new_")
import
type(C_ptr) :: this
end function C_dummyClass__new_
subroutine C_dummyClass__delete_ (this) bind(C,name="dummyClass__delete_")
import
type(C_ptr), value :: this
end subroutine C_dummyClass__delete_
end interface
interface newDummy
module procedure newDummy__new
end interface newDummy
interface deleteDummy
module procedure dummyClass__delete
end interface deleteDummy
public :: newDummy, deleteDummy
!------------------------------------------------------------------------------
CONTAINS
!------------------------------------------------------------------------------
!-------------------------------------------------
! Fortran wrapper routines to interface C wrappers
!-------------------------------------------------
subroutine dummyClass__new(this)
type(dummy_type), intent(out) :: this
this%object = C_dummyClass__new_()
end subroutine dummyClass__new
subroutine dummyClass__delete(this)
type(dummy_type), intent(inout) :: this
call C_dummyClass__delete_(this%object)
this%object = C_NULL_ptr
end subroutine dummyClass__delete
!------------------------------------------------------------------------------
end module dummy_module
汇编
g++ -fPIC -Wall -Wextra -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -lQtGui -lQtCore -lQtNetwork -lpthread -I/usr/include/QtGui -I/usr/include/Qt -I/usr/include/QtCore -I/usr/include/QtNetwork -c *.cpp
gfortran -c *.f90
gfortran -Wall -Wextra -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -lQtGui -lQtCore -lQtNetwork -lpthread -I/usr/include/QtGui -I/usr/include/Qt -I/usr/include/QtCore -I/usr/include/QtNetwork -o dummy *.o /home/me/build/dummy/src/moc_dummy_class_.cxx -lstdc++
(注意:虚拟类的moc文件已经由QtCreator生成)发生这种情况的原因很可能是因为fortran在没有初始化QApplication的情况下创建了自己的线程。如果没有在主线程上创建QApplication,Qt将无法正常工作!问题解决了我让DummyClass从QThread继承(而不是QObject)-我在DummyClass的“run”方法中调用QTimer::start-我在运行DummcClass::start()之前在DummyClass的构造函数中调用QApplication(不漂亮,我知道…),谢谢:)