C ++ Qt如何使用较小的更改创建GUI的副本

时间:2016-06-08 19:00:06

标签: c++ multithreading qt cmake

我有一个名为UINode的GUI,我想创建一个副本,只改变一些东西。 该项目由3个基本线程组成。 PingThread,RosThread和GuiThread。我试图复制粘贴项目文件夹并将其重命名为UINode2,但是当我尝试使用catkin_make编译它时,它会给我错误。 我很确定这是由相同标题引起的错误。

错误:

CMakeFiles/drone_gui2.dir/src/UINode/moc_tum_ardrone_gui.cxx.o:moc_tum_ardrone_gui.cxx:(.text+0x0): first defined here CMakeFiles/drone_gui2.dir/src/UINode2/moc_tum_ardrone_gui.cxx.o:(.rodata+0x100): multiple definition of `tum_ardrone_gui::staticMetaObject' CMakeFiles/drone_gui2.dir/src/UINode/moc_tum_ardrone_gui.cxx.o:(.rodata+0x100): first defined here CMakeFiles/drone_gui2.dir/src/UINode2/moc_tum_ardrone_gui.cxx.o: In function `tum_ardrone_gui::qt_metacast(char const*)': moc_tum_ardrone_gui.cxx:(.text+0x20): multiple definition of `tum_ardrone_gui::qt_metacast(char const*)' CMakeFiles/drone_gui2.dir/src/UINode/moc_tum_ardrone_gui.cxx.o:moc_tum_ardrone_gui.cxx:(.text+0x20): first defined here CMakeFiles/drone_gui2.dir/src/UINode2/moc_tum_ardrone_gui.cxx.o: In function `tum_ardrone_gui::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)': moc_tum_ardrone_gui.cxx:(.text+0x70): multiple definition of `tum_ardrone_gui::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)' CMakeFiles/drone_gui2.dir/src/UINode/moc_tum_ardrone_gui.cxx.o:moc_tum_ardrone_gui.cxx:(.text+0x70): first defined here CMakeFiles/drone_gui2.dir/src/UINode2/moc_tum_ardrone_gui.cxx.o: In function `tum_ardrone_gui::qt_metacall(QMetaObject::Call, int, void**)': moc_tum_ardrone_gui.cxx:(.text+0x6f0): multiple definition of `tum_ardrone_gui::qt_metacall(QMetaObject::Call, int, void**)' CMakeFiles/drone_gui2.dir/src/UINode/moc_tum_ardrone_gui.cxx.o:moc_tum_ardrone_gui.cxx:(.text+0x6f0): first defined here

代码:

#include "tum_ardrone_gui.h"
#include "RosThread.h"
#include "PingThread.h"

#include <QtGui>
#include <QApplication>
#include "ros/ros.h"

// this global var is used in getMS(ros::Time t) to convert to a consistent integer timestamp used internally pretty much everywhere.
// kind of an artifact from Windows-Version, where only that was available / used.
unsigned int ros_header_timestamp_base = 0;

int main(int argc, char *argv[])
{
    std::cout << "Starting drone_gui Node" << std::endl;

    // ROS
    ros::init(argc, argv, "drone_gui");

    RosThread t;
    PingThread p;

    // UI
    QApplication a(argc, argv);
    tum_ardrone_gui w;

    // make them communicate with each other
    t.gui = &w;
    w.rosThread = &t;
    p.gui = &w;
    p.rosThread = &t;
    w.pingThread = &p;

    // start them.
    t.startSystem();
    p.startSystem();
    w.show();

    // wait until windows closed....
    int ec = a.exec();

     // stop ROS again....
    t.stopSystem();
    p.stopSystem();

    std::cout << "Exiting drone_gui Node" << std::endl;

    return ec;
}

我的问题是:想象一下,我想要复制我的UINode 10次。我是否需要对代码进行硬编码并对每个线程名称,类等进行更改...还是有更好的方法?

1 个答案:

答案 0 :(得分:2)

我建议收集一个单独项目中常见的所有代码,并将其构建为库(静态或共享)。将差异实现为多态可能很方便,因此您在库中具有基类,并在特定项目的子类中实现或覆盖行为。

这样,您只需实现每个项目中的差异,并将所有项目链接到包含公共代码的库。

修改

也许所有这些都没有必要:您是否考虑过通过命令行参数控制应用程序的每个实例中的不同行为?

想象一下,无论出于何种目的,您都拥有TCP客户端。您是否会重新编译它以便与要与之通信的每个新主机一起使用?当然不是。相反,您可以将主机地址作为参数传递,例如: G。 -h 127.0.0.1 -p 1977或-h 127.0.0.1:1977。