参考自两位博主文章:

(9条消息) Qt+STK项目配置_zxl_1996的博客-CSDN博客_qt stk

(9条消息) QT5-STK二次开发实例_未完城的博客-CSDN博客_qt stk

本章后面会介绍我出现的和大家可能会出现的各种出错

QT项目

创建一个QT项目,需要带ui文件。

环境依赖注入

我安装的是stk12版本的,我看其他博主,9,11,13都有成功的,(有13吗??)。文件在该STK目录下:

C:\Program Files\AGI\STK12\CodeSamples\CodeSamples\CommonFiles\CppIncludes

这里我的CodeSamples文件是一个压缩包,我解压了,可能会重复一层。

很多。我直接就把这个文件夹拷贝到了项目文件目录中。

编程

创建STK类

stk.h

#include "CppIncludes/AgStkUtil.tlh"

using namespace STKUtil;//引用相应命名空间

#include "CppIncludes/AgVGT.tlh"

#include "CppIncludes/AgSTKGraphics.tlh"

#include "CppIncludes/AgStkObjects.tlh"

using namespace STKObjects;

#include "CppIncludes/STKX.tlh"

using namespace STKXLib;

顺序不能错!!!

stk.cpp

#include "stk.h"

#include "CppIncludes/AgStkUtil.tli"

#include "CppIncludes/AgSTKGraphics.tli"

#include "CppIncludes/AgStkObjects.tli"

#include "CppIncludes/STKX.tli"

顺序不能错!!!

顺序不能错!!! 顺序错了会出现一大堆错误。

创建QSTKEarth类

qstkearth.h

#ifndef QSTKEARTH_H

#define QSTKEARTH_H

#include "STK.h"

#include

#include

#include

#include

class QSTKEarth : public QWidget

{

Q_OBJECT

public:

static QSTKEarth &getInstance()

{

if(instance==NULL)

{

QMutexLocker locker(&mutex);

if(NULL==instance)

instance=new QSTKEarth;

}

return *instance;

}

bool enableControl;

~QSTKEarth();

private:

static QMutex mutex;

static QAtomicPointer instance;

QSTKEarth(const QSTKEarth &);

QSTKEarth(QWidget *parent = 0);

IAgStkObjectRootPtr m_pRoot;

IAgSTKXApplicationPtr m_app;

public:

void PauseSTK();

void StartSTK();

void FasterSTK();

void SlowerSTK();

void ResetSTK();

void NewScenario();

void LoadScenario();

void UnloadStkScence();

};

#endif // QSTKEARTH_H

qstkearth.cpp

#include "QSTKEarth.h"

#include

#include

#include

#include

QMutex QSTKEarth::mutex;

QAtomicPointer QSTKEarth::instance=0;

QSTKEarth::QSTKEarth(QWidget *parent) : QWidget(parent)

{

::CoInitialize(NULL);

// Create a new instance of Automation Object Model Root Object

HRESULT ha=m_app.CreateInstance(__uuidof(AgSTKXApplication));

if(FAILED(ha))

{

QMessageBox::warning(this,QString::fromLocal8Bit("SYSTEM:"),QString::fromLocal8Bit("FAILED"));

}

HRESULT hr = m_pRoot.CreateInstance(__uuidof(AgStkObjectRoot));

if(FAILED(hr))

{

QMessageBox::warning(this,QString::fromLocal8Bit("SYSTEM:"),QString::fromLocal8Bit("FAILED"));

}

enableControl=false;

}

QSTKEarth::~QSTKEarth()

{

m_pRoot.Release();

m_app.Release();

//::CoUninitialize();

}

void QSTKEarth::NewScenario()

{

Q_ASSERT(m_app!=NULL);

STKXLib::IAgSTKXApplicationPtr pSTKXapp(m_app);

pSTKXapp->ExecuteCommand("Unload / *");

pSTKXapp->ExecuteCommand("New / Scenario ScenOne");

enableControl=true;

}

void QSTKEarth::LoadScenario()//加载场景

{

Q_ASSERT(m_pRoot!=NULL);

m_pRoot->CloseScenario();

m_pRoot->LoadScenario(_bstr_t("..\\data\\Scenario1.sc"));

enableControl=true;

}

void QSTKEarth::PauseSTK()

{

if(enableControl)

{

Q_ASSERT(m_app != NULL);

STKXLib::IAgSTKXApplicationPtr pSTKXapp(m_app);

pSTKXapp->ExecuteCommand("Animate * Pause");

// pSTKXapp->Pause();//也可以直接调用类成员函数

}

}

void QSTKEarth::FasterSTK()

{

if(enableControl)

{

Q_ASSERT(m_app != NULL);

STKXLib::IAgSTKXApplicationPtr pSTKXapp(m_app);

pSTKXapp->ExecuteCommand("Animate * Faster");

}

}

void QSTKEarth::SlowerSTK()

{

if(enableControl)

{

Q_ASSERT(m_app != NULL);

STKXLib::IAgSTKXApplicationPtr pSTKXapp(m_app);

pSTKXapp->ExecuteCommand("Animate * Slower");

}

}

void QSTKEarth::ResetSTK()

{

if(enableControl)

{

Q_ASSERT(m_pRoot != NULL);

STKObjects::IAgAnimationPtr pAnimation( m_pRoot );

pAnimation->Rewind();

}

}

void QSTKEarth::UnloadStkScence()//卸载场景

{

Q_ASSERT(m_app!=NULL);

STKXLib::IAgSTKXApplicationPtr pSTKXapp(m_app);

pSTKXapp->ExecuteCommand("UnloadMulti / */Satellite/*");

pSTKXapp->ExecuteCommand("UnloadMulti / */Missile/*");

pSTKXapp->ExecuteCommand("Unload / *");

enableControl=false;

}

UI

拖入此组件,然后双击。图我不贴了,如果像上面第一位博主一样的话,恭喜你成功了,如果和第二位博主一样,我这里详细介绍一下。

打开注册表

查找AGI Globe Control 12,我这里搜索的是12,按自己版本号。

复制自己这个名字STKX12.VOControl, 版本11可能搜索到的很长的名字,一样复制下来即可。

普通文本编辑器打开ui

下面第一个是3d的,第二个是2d map,我这里没有做2d的控件,以防下次使用,先放这里。

STKX12.VOControl

STKX12.2DControl

多了3行代码手动输进去。string标签自己的名字。

5 按钮事件

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include "qstkearth.h"

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent)

, ui(new Ui::MainWindow)

{

ui->setupUi(this);

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::on_pushButton_clicked()

{

QSTKEarth *m_stkEarth = &QSTKEarth::getInstance();

m_stkEarth->NewScenario();

}

ok,到这里就结束了。到这里如果可以运行出stkEngine,然后点击按钮就可以出现stk 3d模块了。

1.如果你出现了一大堆错误请查看include的顺序

如果你是LNG2019 不可解析的外部符号

尝试在pro文件中添加

CONFIG += c++11 qaxcontainer

还有错误请在构建中清除项目-重新qmake-再次构建就没有错误了。

3.拖动更改QAxWidget控件都会让ui文件改变,如果你发现运行之后找不到控件,请再次看一下ui文件的文本模式,加上3行代码。

最后就欣赏连接的喜悦!!!有问题评论讨论,博主也正在学习欢迎讨论后续知识。

好文阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。