我需要创建一个简单的Fortran程序以与复杂的Qt程序绑定。
我遇到的唯一问题是 QTimer
的使用(这是强制性的):当我从Fortran中使用QTimer
调用函数时,总是收到此错误消息:
QObject :: startTimer:QTimer仅可用于以QThread开头的线程。
所有其他“复杂”的东西(QUdpSocket
,利用类..)都可以正常工作。
我该如何解决这个问题?出现错误消息后,我认为我应该找到一种将QThread
用于Fortran的技巧,但是我不知道该怎么做。 (我还尝试从QThread
而不是QObject
继承哑元:也不起作用)
这是一个简单的例子。我现在给出输出以及之后的源代码。
直接启动C ++ / Qt程序时输出
begin
hello
end
hello
hello
... // one "hello" per second
启动f90程序时输出
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();
}
dummyclass.hpp
#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();
}
dummyclass.cpp
#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;
}
dummymain.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
dummy_module.f90
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生成)