Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. How to run a C++ code in Qt?

How to run a C++ code in Qt?

Scheduled Pinned Locked Moved Installation and Deployment
123 Posts 5 Posters 108.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS SGaist

    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.

    RelaR Offline
    RelaR Offline
    Rela
    wrote on last edited by
    #31

    @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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #32

      You need to install the debugger for MSVC

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      RelaR 1 Reply Last reply
      0
      • SGaistS SGaist

        You need to install the debugger for MSVC

        RelaR Offline
        RelaR Offline
        Rela
        wrote on last edited by
        #33

        @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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #34

          MSVC debugger can only be obtained from Microsoft website, here

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          RelaR 2 Replies Last reply
          0
          • SGaistS SGaist

            MSVC debugger can only be obtained from Microsoft website, here

            RelaR Offline
            RelaR Offline
            Rela
            wrote on last edited by
            #35

            @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.

            1 Reply Last reply
            0
            • SGaistS SGaist

              MSVC debugger can only be obtained from Microsoft website, here

              RelaR Offline
              RelaR Offline
              Rela
              wrote on last edited by
              #36

              @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?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #37

                Did you implement anything in MainWindow ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                RelaR 1 Reply Last reply
                0
                • SGaistS SGaist

                  Did you implement anything in MainWindow ?

                  RelaR Offline
                  RelaR Offline
                  Rela
                  wrote on last edited by Rela
                  #38

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

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #39

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    RelaR 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      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.

                      RelaR Offline
                      RelaR Offline
                      Rela
                      wrote on last edited by Rela
                      #40

                      @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.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #41

                        Looks like there's no error there. But I meant the other way around. Run just your algorithm code. The error is there somewhere. And you should put a break point at the start so you can go step by step until it breaks.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        RelaR 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Looks like there's no error there. But I meant the other way around. Run just your algorithm code. The error is there somewhere. And you should put a break point at the start so you can go step by step until it breaks.

                          RelaR Offline
                          RelaR Offline
                          Rela
                          wrote on last edited by
                          #42

                          @SGaist
                          The algorithm code gives result in MSVC2013.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #43

                            Then there's something fishy going on with MinGW or you're lucky with Visual Studio. Without any code I can't possibly help you find why it's crashing only with MinGW.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            RelaR 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Then there's something fishy going on with MinGW or you're lucky with Visual Studio. Without any code I can't possibly help you find why it's crashing only with MinGW.

                              RelaR Offline
                              RelaR Offline
                              Rela
                              wrote on last edited by
                              #44

                              @SGaist
                              The above output was the debug using "Desktop Qt 5.5.1 MSVC2013 64 bit". I didnt debug with MinGW. The problem is the way I use to get the results in UI.
                              I tried a simple project to print out "Hello world", the C++ code written in MSVC gives output. Could you please take a look at my files:
                              Here are the content of C++ all files.
                              main.cpp
                              Bucky9.cpp
                              stdafx.cpp
                              targetver.h
                              stdafx.h
                              Now I created a Qt widget project, and I added the C++ code stdafx.h, targetver.h, and stdfx.cpp files using "Add existing Files" to the header and source. I didnt add Bucky9.cpp and main.cpp. Instead, I coppied the main.cpp C++ codes in the Qt main.cpp file with a little changes of all files.
                              (Changes: 1- Bucky9 is not included in Qt.
                              2- In stdafx.h #include <char.h> instead of <tchar.h>
                              3- Including #include "stdafx.h" in mainwindow.cpp.

                              Here are the content of all files in Qt project:
                              main.cpp
                              mainwindow.cpp
                              stdafx.cpp
                              stdafx.h
                              targetver.h
                              mainwindow.h

                              When I debug it it start debugging and when I click on Run it gives the "Hello world" in "Application output" here
                              Now I just want to get the "Hello world" in "mainwindow.ui" instead of in "Application output". Could you tell me how to do that?

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #45

                                First thing: don't use cout since it will send the text to the console.

                                You can simply put a QLabel with hello world as text as central widget of your MainWindow class.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                RelaR 2 Replies Last reply
                                0
                                • SGaistS SGaist

                                  First thing: don't use cout since it will send the text to the console.

                                  You can simply put a QLabel with hello world as text as central widget of your MainWindow class.

                                  RelaR Offline
                                  RelaR Offline
                                  Rela
                                  wrote on last edited by
                                  #46

                                  @SGaist
                                  I am going to print the result of the C++ code in UI. For "Hello world" here is the Qt using QLabel written in "main.cpp" after "Debug". The C++ files (stdf.h, targetver.h, stdafx.cpp) were added, but I am not sure if the "Hello world" is the result of C++ code or not.

                                  Again here is my C++ project in MSVC2013.
                                  After I created Qt widget, I added "O_PS .H ", "stdaf.h ", "targetver.h ", " po .cpp", and "stdaf.cpp ". The code was copied in "#main.cpp" to have one main function. As input data, a model.txt file also a path of text file was added in the run "Arguments" of "Desktop Qt 5.5.1 MSVC2013 64 bit. It gave error that it cannot find the headers files, and I copied all of them in the Qt project folder. Here is the "Application Output".
                                  In MSVC, it gives some values as outputs, I added a label to ui and wrote QLabel instead of one of the values "cout", like "Hello world". It gives this error. Here is the line that error show, the cout is comment and instead Qlabel was used.

                                  // QLabel* label=new QLabel(para(i,0));
                                          label->setAlignment(Qt::AlignCenter);
                                          QWidget* widget=new QWidget;
                                          QHBoxLayout* layout=new QHBoxLayout;
                                           layout->addWidget(label);
                                           widget->setLayout(layout);
                                           widget->show();
                                          //for(i= 0; i<para.IDim(); i++)
                                               //cout << para(i,0) << "\t" <<para(i,1) << endl;
                                  
                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #47

                                    Because para returns a double and not a string.

                                    You should use QString::number(para(i,0))

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    RelaR 1 Reply Last reply
                                    0
                                    • SGaistS SGaist

                                      Because para returns a double and not a string.

                                      You should use QString::number(para(i,0))

                                      RelaR Offline
                                      RelaR Offline
                                      Rela
                                      wrote on last edited by Rela
                                      #48

                                      @SGaist
                                      This code in "main.cpp" outputs the area and prime of a circle in which QString::number(); is used to return float.

                                      #include "mainwindow.h"
                                      #include <QApplication>
                                      
                                      #include <QString>
                                      #include <QHBoxLayout>
                                      #include <QLabel>
                                      #include <QWidget>
                                      
                                      #include "stdafx.h"
                                      #include <iostream>
                                      #include <iomanip>
                                      using namespace std;
                                      
                                      int main(int argc, char *argv[])
                                      {
                                          QApplication a(argc, argv);
                                      
                                          int radius = 3; //radius of circle
                                              float square; //area of circle
                                              float edge; //perimeter of circle
                                           square = 3.14 * radius*radius;
                                           edge = 2 * 3.14 *radius;
                                      
                                              //cout << "Circle are= " << area << endl;
                                              //cout << "Circle perimeter= " << perime;
                                      
                                              // Show area
                                              QString area = QString::number(square);
                                              QLabel* arlabel=new QLabel(area);
                                              // create horizontal layout
                                              arlabel->setAlignment(Qt::AlignCenter);
                                              QHBoxLayout* arlayout=new QHBoxLayout;
                                              // and add label to it
                                              arlayout->addWidget(arlabel);
                                              QWidget* arwidget=new QWidget;
                                              // set layout to widget
                                              arwidget->setLayout(arlayout);
                                              arwidget->show();
                                      
                                              // Show perime
                                              QString perime = QString::number(edge);
                                              QLabel* pelabel=new QLabel(perime);
                                              pelabel->setAlignment(Qt::AlignCenter);
                                              // create horizontal layout
                                              QHBoxLayout* pelayout=new QHBoxLayout;
                                              // and add label to it
                                              pelayout->addWidget(pelabel);
                                              QWidget* pewidget=new QWidget;
                                              // set layout to widget
                                              pewidget->setLayout(pelayout);
                                              pewidget->show();
                                          //MainWindow w;
                                          //w.show();
                                          return a.exec();
                                      }
                                      
                                      

                                      I created two "QLabel" in "Mainwindow.ui", but it opens two separate "Mainwindow.ui" and shows area and prime in each of them. But I want to have both on one "Mainwindow.ui".

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #49

                                        No, currently it shows two widgets: arwidget and pewidget.

                                        MainWindow.ui is innocent here since you don't even show MainWindow.

                                        If you only want to show one widget, create a QWidget, put a layout on it and add arwidget and pewidget to it. You don't even need MainWindow.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        RelaR 1 Reply Last reply
                                        0
                                        • SGaistS SGaist

                                          No, currently it shows two widgets: arwidget and pewidget.

                                          MainWindow.ui is innocent here since you don't even show MainWindow.

                                          If you only want to show one widget, create a QWidget, put a layout on it and add arwidget and pewidget to it. You don't even need MainWindow.

                                          RelaR Offline
                                          RelaR Offline
                                          Rela
                                          wrote on last edited by Rela
                                          #50

                                          @SGaist
                                          Hi, last week I created one widget and it worked. Today, I was goign to run it again, but the files format has changed. I think MSV was updated. First time MVS 2015 gave error "Severity Code Description Project File Line Suppression State Error Solution is not saved. Please save your solution before managing NuGet packages. 0".
                                          Should I click on Restore NuGet Packages?

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved