How to run a C++ code in Qt?



  • I have a C++ code, which gives output using MVS2010 in command prompt, and going to develop a stand-alone GUI for it using Qt5.4. I created a new project in Qt creator:
    “New Project” >> “Qt Widgets Application” >> “MainWindow” and added .cpp and .h files of the C++ code using “Add existing Files”. Here is the project files I have:
    Qt creator files
    Then I built it using “Desktop Qt5.4 MinGW”. This is shown in the “Application Output”:
    link text
    When I build it with “Desktop Qt5.5 MSVC2010 OpenGL “ it gives two errors:
    link text

    I have studied some tutorials, none of them showed how to work with a C++ code in Qt. Does it mean that only I can build the GUI with “Desktop Qt5.4 MinGW” and is it possible to run the code with Qt or see the output in "Design" of Qt?


  • Lifetime Qt Champion

    Hi,

    Please show the complete error log.

    Qt is a C++ library, so there's nothing special to do regarding that question.



  • @SGaist
    Hi, here is the Applicatio Output when I use “Desktop Qt5.4 MinGW” to run:Application Output
    Here are the "Compile Output" and "Issues" when I use “Desktop Qt5.5 MSVC2010 OpenGL“: Compile Output
    Issues

    PS I noticed that the C++ code outputs results in MVS2010 reading a model text file as input data, while I don't have such that file like this model.txt in Qt. Is there any way to enter that model data in Qt?


  • Lifetime Qt Champion

    For you working version with MinGW, go to the Run part of the project panel, you can there add the path to your model.txt file in the argument line edit.



  • @SGaist
    Which one of these Variables I need to choose to add the path? Argument insert variable
    I copied the text file called "M8" in the project directory and chose "CurrentProject:Path", and entered the path "%{C:\Qt\Bucky\PS\ps1}" in "Argument", but it gives this error:

    • Starting C:\Qt\Bucky\PS\build-ps1-Desktop_Qt_5_4_2_MinGW_32bit-Release\release\ps1.exe...
      Invalid parameter passed to C runtime function.
      Invalid parameter passed to C runtime function.
      terminate called after throwing an instance of 'std::bad_alloc'
      what(): std::bad_alloc
      C:\Qt\Bucky\PS\build-ps1-Desktop_Qt_5_4_2_MinGW_32bit-Release\release\ps1.exe exited with code 3

  • Lifetime Qt Champion

    Just pass the path to your file (including the file name)



  • @SGaist
    It is not possible just pass the path, because when I go to Run part of the project panel and click on the "Argument" option ("AB..."), It asks the variable, and the window is opened. Argument


  • Lifetime Qt Champion

    Don't click on the icon, write the path by hand to your file



  • @SGaist
    I did it like this: File path, but it gives the same error:
    Starting C:\Qt\Tutorials\PS\build-ps1-Desktop_Qt_5_4_2_MinGW_32bit-Release\release\ps1.exe...
    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function.
    terminate called after throwing an instance of 'std::bad_alloc'
    what(): std::bad_alloc
    C:\Qt\Tutorials\PS\build-ps1-Desktop_Qt_5_4_2_MinGW_32bit-Release\release\ps1.exe exited with code 3


  • Lifetime Qt Champion

    You're missing the file extension



  • @SGaist
    Thanks, it worked and gives the results in "Application Output". Now, I want to enter the text file data using "mainwindow.ui" instead of reading the text file. Does it need to change the code or I should just add some functions to the "mainwindow.cpp" for Objects in "mainwindow.ui"?
    For this model data, these were defined in cpp file:
    "bool ReadModelData(istream &in, Master &_MS)"

    and the ReadModelData is called with:

    "fin.open(argv[1],ios::in);
    fin >> nof_people;
    for(i=0; i<nof_people; i++) {
    ReadModelData(fin,s);"
    ...
    in header file:
    "bool ReadModelData(istream &, Master &);"


  • Lifetime Qt Champion

    Add a function in your MainWindow class that takes the path as a const QString & then use QFile to read it and put the content in your text widget.

    Note that you have the nice and handy QCommandLineOption and QCommandLineParser classes to handle command line option you pass to your application.



  • This post is deleted!


  • @SGaist
    Hi,
    I have installed the newer version of Qt5.5.1 and trying to do the same (I have a C++ code, which gives output using MVS2013, and going to develop a stand-alone GUI for it using Qt5.5. I created a new project in Qt creator: ).
    “New Project” >> “Qt Widgets Application” >> “MainWindow” and added .cpp and .h files of the C++ code using “Add existing Files”.
    Using previous version Qt5.4.1, I could add all .cpp and .h files from my C++ code using “Add existing Files” with right click on "Headers" and "Sources". It showed the added files like this:
    Add existing Files
    But now when I do the same procedure, it doesn't show the new file added.
    Not added
    When I run it using "MSVC2013 64bit" , it gives this error: MSVC2013
    Using "MinGW 32bit" , it gives this error: MinGW
    Is there another way to add ready C++ code to the Qt5.5.1?


  • Moderators

    The paths in the error messages look strange: many blanks.
    Can you post the content of the *.PRO file?



  • @jsulm
    Sorry for blank spaces, just I have to delete some project names.
    Now, I have added the headers and cpp files from another path and it shows them:Added existing files
    The reads a text file data, and I add its path in "Argument" of " MinGW 32 bit" and it gives the results in Application Output.
    Does it matter from which path I choose the .h and .cpp files, or in which directory the Qt project is saved?
    However, when I use "Desktop Qt 5.5.1 MSVC2013 64bit" it gives this error: Compile Outputs
    Issues


  • Lifetime Qt Champion

    You have two main functions that are being compiled. Aren't you trying to include too much files in your new project ?



  • @SGaist
    I have these header and cpp files in my C++ code. C++ code
    and add these files to the PS4 Qt project using “Add existing Files”: PS4 Qt

    I also copy the text model (M7) file in the Qt directory and the code reads the data file. I gives the path of the model file using "Argument" in “Desktop Qt5.5 MinGW" run. Previously, I had Qt5.4 and MSVC 2010 and the results including some numbers appeared in "Application Output". Now, using Qt5.5 and MSVC 2013, the “Desktop Qt5.5 MinGW" works without error, but the "Application Output" shows this result, which are zero, instead of some non-zero numbers (cpu and z). Application output

    However, when I compile with "Desktop Qt MSVC2013 64bit", it gives these errors: Issues
    Compile output
    Even if it gives the results using “Desktop Qt5.5 MinGW", it is ok, but it does not.


  • Lifetime Qt Champion

    Again, you have multiple main function, that's the first problem to solve. Why do you have more than one ?



  • @SGaist
    You mean the "main.cpp" and "mainwindow.cpp" ?
    If so, this is the same I did before with Qt5.4.1. There were the same main functions and gave the results in "Application Output".
    I create a new project in Qt creator:
    “New Project” >> “Qt Widgets Application” >> (Class name) “MainWindow” & (Base class) "QMainWindow" >> "Next" and "Finish" this application window:New Project
    After that I added .cpp and .h files of the C++ code using “Add existing Files”.


  • Moderators

    SGaist means the C/C++ main function:

    int main(int argc, char** argv)
    {
    ...
    }
    

    An application can only have one main function, as the compiler error says you have two of them.



  • I have one "int main(int argc, char argv[])
    { ..."* function in "O_PS .cpp" file, and one main function in "main.cpp" file. Qt


  • Moderators

    @Rela You should remove one of them.



  • @jsulm
    Could you explain how? The main function of "O_PS .cpp" is my C++ code, do I delete:
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
    

    } in "main.cpp" ?


  • Moderators

    I don't know which of them you have to remove. I don't know your code and I don't know what you want to do. You have to decide which of them is the right one and then just delete the other. You just cannot have more than one entry point (more than one main function) in a C/C++ program.



  • @jsulm
    I definitely need my C++ code, because everything to get the results is in the code, but when I create a new project in Qt, the "main.cpp" file is also created. If I right click on it and "Remove File" do I need add this code somewhere?

    I had Qt5.4.1 before, and it gave results in "Application output" previously using “Desktop Qt5.4 MinGW” with the same main functions.


  • Lifetime Qt Champion

    If you already have a main function that does everything then no. Just keep this in mind: having your main function in a file not named main.cpp is pretty unusual for an application, not forbidden, just unusual. Beware of maintenance nightmare.



  • @SGaist

    I created another project file, and added the C++ files, but instead of adding the code .cpp file I copied it in "main.cpp" file. The only change I made was that copied the

    QApplication a(argc, argv);
        MainWindow w;
        w.show();
    

    After
    int main(int argc, char *argv[])
    {
    . So, now there is only one main function in "main.cpp".

    After that I added the text file by "Argument", using MinGW 32bit, gives this "Runtime Library" error: MinGW 32bit I read maybe its because MinGW is 32bit and I need to use 64 bit as compiler.
    And used "Desktop Qt5.5.1 MSVC 2013 64 bit" , it gives this error: MSVC 64bit


  • Lifetime Qt Champion

    Run it through the debugger, you're doing something fishy in your code. Throwing a bad alloc means that you are trying to allocated more memory than it's available. It might pass on 64bit (not the same memory limits) but if it does, it means that you are doing something else that is wrong and makes your application crash.



  • This post is deleted!


  • @SGaist
    When I click on MSVC to debug MSVC and "F5", it gives this error: No engine
    When I use MinGW and "F5", it gives this error: MinGW


  • Lifetime Qt Champion

    You need to install the debugger for MSVC



  • @SGaist
    In "Maintenance Tool.exe" these components were chosen. components Do I need to mark "Windows Runtime" also?
    In "Build & Run", there is one debugger, if it should be added manually, from which folder I have to select it? Debugger


  • Lifetime Qt Champion

    MSVC debugger can only be obtained from Microsoft website, here



  • @SGaist
    Thanks, should I install SDK or WDK?
    I did search,but it is said that CDB goes with Visual C++, but there is not download link for CDB.



  • @SGaist
    I installed Windows SDK and chose all components including "debugging tools for windows". Now, in Tools -> Options -> Build & Run -> Debuggers, I see the two paths for CDB. In "Kit" I chose "Desktop Qt5.5.1 MSVC2013 64bit" and from "Debugger" combo, the CDB.
    I debug the program with it, and it doesn't give the previous error, but first open the mainwindow.ui and then shows "Debugging starts" in "Application Output" and Compile Output.
    As I wrote before, instead of adding the (C++code.cpp) file I copied the code in "main.cpp" file to have one main fucntion. The only change I made was that copied the

    QApplication a(argc, argv);
        MainWindow w;
        w.show();
    

    After
    int main(int argc, char *argv[])
    {
    in my C++ code. How can I see the results (which are some numbers) in mainwindow.ui?


  • Lifetime Qt Champion

    Did you implement anything in MainWindow ?



  • @Rela
    Yes, its an algorithm here written in C++.


  • Lifetime Qt Champion

    I can't really read it because of all the whited out parts. But the first thing I'd do is check that you don't do anything funky in there. Remove what's not related to your algorithm from main and try to run that. Once it's good, bring back the Qt parts.



  • @SGaist
    Sorry for white space, I have to remove some function names. The algorithm is 2820 lines, and I think each line is needed to output the result. Do you mean i remove the algorithm completely and just remain
    #include "mainwindow.h"
    #include <QApplication>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();}

    in main.cpp
    I did that and the here is the output.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.