Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Visual Studio Code in parallel with QtCode



  • Hi Comunity,
    I have an old app written in C++ using Visual Studio enviroment. I have to replace its GUI, so I have developed an small GUI in Qt.
    I have a point in that old program where the old GUI is created. I thought that just in that point should I the Qt gui in paralel start and the rest will be send signals to interface.

    I readed the next post for the threads https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

    In First place, I'm trying to write an small program where the MainWindow will moved into a new thread and after, furthermore code (a cout << "hello" << endl; for example) will executed.

    To follow the method in above link showed, I'm trying to build an object with the necessary calls to create the main window, thinking in moving that object into a new thread.

    here is the code:
    (main.cpp)

    #include "mainwindow.h"
    #include <QApplication>
    #include <QObject>
    
    class GuiApp : public QObject
    {
        Q_OBJECT
    
    public:
        explicit GuiApp(int argc, char *argv[]);
        ~GuiApp();
        int returnExec();
    
    private:
        QApplication *a;//QApplication a(argc, argv);
    };
    
    GuiApp::GuiApp(int argc, char *argv[])
    {
       a = new QApplication(argc, argv);
        MainWindow w;
        w.show();
        returnExec();
    }
    
    GuiApp::~GuiApp()
    {
        delete a;
    }
    
    int GuiApp::returnExec()
    {
        return a->exec();
    }
    
    //***********************************************
    //***********************************************
    int main(int argc, char *argv[])
    {
    
        GuiApp gApp(argc, argv);
        /*I intend write here the sentences to move gApp to the new thread
         *
         *
         */
    
        //The code after this point should be executed in parallel 
        cout  << "hello" << endl;
    }
    
    

    That's my idea, but I get the following linkage errors

    main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall GuiApp::metaObject(void)const " (?metaObject@GuiApp@@UBEPBUQMetaObject@@XZ)
    main.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall GuiApp::qt_metacast(char const *)" (?qt_metacast@GuiApp@@UAEPAXPBD@Z)
    main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall GuiApp::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@GuiApp@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
    debug\parallel_test.exe : fatal error LNK1120: 3 unresolved externals
    

    Is this the right way? Please Could anybody help me or to fix the linker errors (case that the right way ist) or show me the right form to make this?

    Any help oder suggestion will be good welcomed
    Thanks in advance.


  • Lifetime Qt Champion

    You should create your Visual Studio solution with the help of qmake and a .pro - file: http://doc.qt.io/qt-5/qmake-platform-notes.html and http://doc.qt.io/qt-5/qmake-tutorial.html



  • @Christian-Ehrlicher Thank you very much, that's a point to begin with the import process of the visual studio project. Any help with the paralel execution from the code?

    thanks in advance


  • Lifetime Qt Champion

    @Josz
    Hi
    you can try ( worked for me on win 10)
    https://forum.qt.io/topic/67285/ui-application-as-thread-cpu-load-almost-100/11
    all credits to mr kshegunov



  • This post is deleted!


  • @mrjj Sadly Did not work :_(. The loop run only when I close the MainWindow.
    I'm on Windows 7 with msvsc2015 32 bits and Qt 5.11.2. In my .pro I have CONFIG += c++11. Replacing for c++14 is the same

    here is my code

    #include "mainwindow.h"
    #include <QApplication>
    #include <thread>
    #include <iostream>
    
    using namespace std;
    
    void guiLoader(int argc, char *argv[]){
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        //return a.exec();
        QApplication::exec(); 
    }
    
    void loop(){
            while(1)
                cerr << "Parallel hello World" << endl;
    }
    
    int main(int argc, char *argv[])
    {
        thread guiLoaderThread(guiLoader, argc, argv);
        guiLoaderThread.join();
        loop();
    }
    
    


  • Didn't work because the position of "guiLoaderThread.join();". This sentence must be placed at the end of the code that you want to execute. Is like "The code will run until here"

    The next version work fine

    #include "mainwindow.h"
    #include <QApplication>
    #include <thread>
    #include <iostream>
    
    using namespace std;
    
    void guiLoader(int argc, char *argv[]){
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        //return a.exec();
        QApplication::exec();
    }
    
    void loop(){
            for(int i = 0; i < 100; i++)
                cerr << "helloworld parallel" << endl;
    }
    
    int main(int argc, char *argv[])
    {
        thread guiLoaderThread(guiLoader, argc, argv);
        loop();
        cerr << "last line to be executed" << endl;
        guiLoaderThread.join();
    
        return 11;
    }
    
    

Log in to reply