vsTASKER 7 Tutorial
×
Menu
Index

Qt

 
 
In this sample, we will create a simple Qt Window with one button to activate a logic with one lamp (that turns green when a logic starts).
 
In Qt Creator, create a Qt Gui Application:
Click Next until the end of the setup.
 
Now, in main.cpp file, add the following:
 
#include "vt_rtc.h"
Vt_RTC* vt_rtc = NULL;
 
// =========================================================================
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
...
 
in mainwindow.cpp file, add the following:
 
#include "QDebug"
#include "vt_rtc.h"
#include "myTestQt.h"  // vsTasker database header
 
then change the MainWindow constructor with the following one:
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    vt_rtc = new Vt_RTC;
    autorun();
 
    connect(&loop_timer, SIGNAL(timeout()), this, SLOT(loopTimer()));
 
    loop_timer.setInterval(33); // 33 milliseconds, 30hz
    loop_timer.start();
 
    ui->setupUi(this);
}
 
and the destructor with this one:
 
MainWindow::~MainWindow()
{
    delete ui;
    delete vt_rtc;
}
 
Now, let's define the vsTASKER runtime loop (called above loopTimer):
 
void MainWindow::loopTimer()
{
    static int lb=0, s=0;
 
    if (++lb==30) { // low band 1hz, always useful for tracing
       lb = 0;
       qDebug() << ++s;
    }
 
    if (vt_rtc->isOn()) {
 
       vt_rtc->tic();  // mandatory, to call the runtime node cycle
 
       if (vt_rtc->isRunning()) {
          ui->startLogic->setEnabled(true); // allow button to be used
          // get the entity
          Vt_Entity* myEnt = vt_rtc->scenario->findEntity("myEnt");
          if (myEnt) { // find the logic
             Vt_Logic* start = myEnt->findLogic("myLogic");
             if (start->isEnabled()) ui->logic->setStyleSheet("background-color: rgb(0, 255, 0)"); // green
             else ui->logic->setStyleSheet("background-color: rgb(100, 100, 100)"); // gray
       }
       else ui->startLogic->setEnabled(false);  // must start the simulation first
    }
    else {
       exit(0);  // we have a problem !
    }
}
 
In mainwindow.h, add the following code emphasized in bold:
 
#include <QTimer>
 
private slots:
    void loopTimer();
private:
    Ui::MainWindow *ui;
    QTimer loop_timer;
 
 
Now, in vsTASKER, create a new database, called myTestQt and save it
 
Back in Qt Creator, right click on the myTest project and Add Existing Items...
Select in /gen/ the three files: myTestQt.h, myTestQt_code.cpp and myTestQt_intfc.cpp
 
Now, open the myTest.pro and add the following:
 
QMAKE_CXXFLAGS += -Zc:strictStrings-
 
INCLUDEPATH += . \
               $(VSTASKER_DIR)/include \
               $(VSTASKER_DIR)/include/engine \
               $(VSTASKER_DIR)/feature/include \
               $(VSTASKER_DIR)/sprite/include \
               $(VSTASKER_DIR)/plugins/include \
               $(VSTASKER_DIR)/model/include \
               $(VSTASKER_DIR)/src/template \
               $(VSTASKER_DIR)/gen
 
LIBS += -L$(VSTASKER_DIR)/Lib/vc90/ -lglut32 \
        -L$(VSTASKER_DIR)/Lib/vc90/ -lsdl \
        -L$(VSTASKER_DIR)/Lib/vc90/ -lsdl_image \
        -L$(VSTASKER_DIR)/Lib/vc90/ -lvstasker \
        -L$(VSTASKER_DIR)/Lib/vc90/ -lvstasker_sim \
        -L$(VSTASKER_DIR)/Lib/vc90/ -lspattern \
        -L$(VSTASKER_DIR)/Lib/vc90/ -lspzone \
        -L$(VSTASKER_DIR)/Lib/vc90/ -ltraj \
        -L$(VSTASKER_DIR)/Lib/vc90/ -lpath \
        -L$(VSTASKER_DIR)/Lib/vc90/ -lpoint \
        -lwsock32 \
        -luser32 \
        -lgdi32 \
        -lopengl32 \
        -lglu32 \
        -lfreetype \
        -lorbitcore \
        -lorbittools
 
 
 
Now, open the mainwindow.ui and add one button, called startLogic and one QWidget called logic and give it the following style-sheet: background-color: rgb(100, 100, 100);
You should then have:
 
Now, right click on the startLogic widget, Go to slot... clicked(), then add the following code:
 
void MainWindow::on_startLogic_clicked()
{
    if (vt_rtc->isRunning()) {
       // get car entity
       Entity* myEnt = (Entity*) vt_rtc->scenario->findEntity("myEnt");
       if (myEnt) myEnt->raiseEvent("start");
    }
}
 
Now, in vsTASKER, add one entity and name it myEnt
Create one logic "myLogic" with the following (one delay set with a fix time of 20 seconds and a Perform Quit action.
 
Give this logic to myEnt with an activation on event "start". Save but do not compile yet.
 
Back in Qt Creator, build and run in debug mode (good practice):
In vsTASKER GUI, start running the scenario.
 
On the UI window (below), click the Start Logic button and see the lamp changing from gray to green then after 20 seconds, back to gray (when the logic ends)
 
When things work, you can add the Qt Viewer on the vsTASKER database.
Add it normally, then set the Build Directory where Qt Creator stores generated code of your myTest project:
 
You can now start adding new logics and rebuild the window application like you would do with another viewer.
Use Qt Creator anytime you need to change the UI or change the way it must react with the simulation engine.