How to run a C++ code in Qt?


  • Lifetime Qt Champion

    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.



  • @SGaist
    The algorithm code gives result in MSVC2013.


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    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.



  • @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;
    

  • Lifetime Qt Champion

    Because para returns a double and not a string.

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



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    I don't see why not.

    Note that your link is pointing to an invalid page



  • @SGaist
    The MVS 2015 was updated automatically and after that it and Qt are not working anymore.
    First it gave error "...Please save your solution before managing NuGet packages". After that "the project file was unloaded". But when I tried other projects the error changed to "Failed to locate: "CL.exe. The system cannot find the file specified". I tried to install some components VS 2015: "Control Panel > Programs >Programs and Features > Microsoft Visual Studio > 'modify' > Visual C++ components and install", but it shows this window: here


  • Qt Champions 2018

    Did you try the first option in this window?



  • @jsulm
    Yes, it worked and now MSVC 2015 is working, but not Qt. 5.5.
    It gives error "Qt Creator needs a compiler set up to build. Configure a compiler in the kit options." in "Issues and in "Complie Output" the error "Configuration is faulty. Check the Issues view for details. Error while building/deploying project Circle2 (kit: Desktop Qt 5.5.1 MSVC2013 64bit) When executing step "Make". "

    I installed "standalone SDK" from here here Downloaded and a folder "Windows kits" was built in the same folder. I copied it in "Visual studio 14" folder, and run the Qt again, but this is Build & Run. No active "Desktop Qt5.5. MCVS 2013 64 bit".

    I dont know what the problem is? The kit should be MCVs 2105?


  • Lifetime Qt Champion

    I didn't realizeā€¦ You changed Visual Studio's version in between. On Windows you can't mix and match compilers and libraries, Visual Studio's compilers are not compatible one with the other. So since you have the MSVC2013 Qt package, you have to use Visual Studio 2013 with it.

    If you want to use Visual Studio 2015, you can either build Qt yourself, or go with the 5.6 beta which has VS2015 packages.



  • @SGaist said:
    Thanks, which one do you recommend? I spent a lot of time on lunching and problem with running my programs until now because of components and compilers. If I uninstall 5.5 and install 6.5. beta, does Qt 6.5. work conveniently with Qt5.5. projects?


  • Lifetime Qt Champion

    You're too much in the future ;) It's 5.6

    No problem using Qt 5.6 for project started with 5.5. Forward compatibility is one of Qt's guaranties.



  • @SGaist
    I was going to uninstall it by opening "Maintenancetool.exe" > "Remove all components" but it gives this error in 92%...


  • Lifetime Qt Champion

    Unusual, but why not just update ?



  • @SGaist
    I was going to work with Qt 5.6 beta with MVS2015. To have Qt 5.6 beta can I just update Qt 5.5? I clicked on update components in "Maintenancetool.exe" , is this the component I need?


  • Lifetime Qt Champion

    No, that's Qt Creator. It's the IDE.

    IIRC, the 5.6 beta installer is available through your Qt Account page



  • @SGaist
    If you mean through this link, the Qt5.5 should not be uninstalled first?


  • Lifetime Qt Champion

    There's also that link indeed. Since it's an offline installer, I don't remember whether it will overwrite the Qt folder or not



  • @SGaist
    Is the installation way different from Qt5.5? Why does Qt5.6 give this error, while I installed Qt5.5 in the same folder?


  • Lifetime Qt Champion

    Don't put Qt in a path with white spaces, that will save you much trouble.



  • @SGaist
    If you mean "program files'' it was the same folder I installed previous Qt5.5 and didnt give any message. By default Qt5.6 beta selects the C drive to install.


  • Lifetime Qt Champion

    That's what I mean. Avoid white spaces it's the best thing to do on Windows.



  • @SGaist
    Just to be sure which one I need to install?Qt5.6


  • Lifetime Qt Champion

    The second one unless you are planning to use WinRT



  • @SGaist
    I dont have any idea about that, I was just running a C++ code in Qt and get outputs in UI... how can I know if it was installed before in Qt5.5? Moreover, there is a "_64" difference between the two: qt-opensource-windows-x86-msvc2015_64-5.6.0-beta.exe 17-Dec-2015 and
    qt-opensource-windows-x86-msvc2015-5.6.0-beta.exe


  • Lifetime Qt Champion

    The _64 is to be used if you want to build a 64 bit version of your application. That also requires a 64 bit OS to run.

    The Beta doesn't installed itself in 5.5 since it's the 5.6 beta



  • @SGaist
    My system is windows 7 64-bit, and I am going to run a C++ code using Qt with MCVS 2015.
    Just to prevent from over or re-installation again, could you please tell me which one I need to install?


  • Lifetime Qt Champion

    If you plan to only run your application on 64bit Windows, then take the 64bit version otherwise the 32.



  • @SGaist
    I installed the first one here to be able to run my projects using MCVS 2015 compiler. It worked last week. I think I have still the older Qt 5.5. and couldn't uninstall it.
    Now I am again trying to run my project, but cannot find the Qt5.6 beta. when I open the Qt shortcut on my desktop, it has only the MCVS 2013 as compiler.
    How can I run projects using Qt 5.6 beta shown here and get rid of Qt 5.5?
    In addition, when I open the "MaintenanceTool.exe" to update it, this error appears.


  • Lifetime Qt Champion

    Qt 5 beta should be installed somewhere like C:\Qt\5.6.0. In any case you are probably using the Qt 5.5 installed Qt Creator. Either add a Qt Version and a Kit in the Preferences of Qt Creator using your Qt 5.6 beta or start the Qt Creator from Qt 5.6 beta.

    If you don't what that mix, just remove both and re-install only Qt 5.6 beta.

    There's nothing to update in a beta version.



  • @SGaist said:

    Yes, Qt 5.6 beta is in C:\Qt\5.6.0. This is my Qt5.6 beta that I sent in previous post. But may I ask how to start the Qt Creator from Qt 5.6 beta?
    Could you please let me know how to "add a Qt Version and a Kit in the Preferences of Qt Creator using Qt 5.6 beta "?

    I was going to uninstall Qt 5.5. before installing Qt 5.6. beta, but it gave this error asked in one of my previous posts.


  • Lifetime Qt Champion

    Like written on the dialog, don't install Qt in "Program Files", the spaces always poses problems.

    Then use the Qt Creator from Qt 5.6 so you don't have to configure anything special, it should already have it auto-detected.



  • @SGaist said:

    The Qt 5.6 beta was installed in C:\Qt\5.6.0, not program file.
    Qt 5.5 was previously installed in program files, but it worked properly. When I open Qt 5.6., this folders appear, I dont know where the Qt Creator from Qt 5.6 is.
    When I open the Qt creator on desktop, it only shows this compiler, not "Desktop Qt 6.5. MSVC 2015".
    I am wondering last week, I could run it immediately after I installed Qt5.6. beta.


  • Qt Champions 2018

    QtCreator should be in C:\Qt\5.6.0\Tools
    You can create a Kit with the right compiler and Qt version in QtCreator under Tools/Options/Build & Run



  • @jsulm
    Thanks, could you tell me the exact path of 5.6 beta creator in tools here?


Log in to reply