How to run a C++ code in Qt?
-
Previously I tried a simple project to print out "Hello world" in one of my previous posts. The C++ code was written in MSVC 2013 gives output. Here is one of my previous posts related to getting output of C++ code written in MCVS 2013 in Qt5.5.
Here is my previous post:
"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"I could get "Hello worlds" with QLabel using Qt5.5 and MCVS 2013. The only difference is that I didnt change <tchar.h> in stdafx.h. Now, when I run it, it builds it, but the run button is not active. Here is the code: Qt code
Is there any change when I am working with MCVS 2015 and Qt 5.6 beta? Some of my Qt 5.5. works, but some of them dont work. -
What is the content of your *.pro file?
-
Thanks it worked today, the run button was active and gave "Hello world" in widget.
Just I am doing the same with another longer C++ code. I copied all .h and .cpp files to the created Qt project folder and added using
"Add existing file" to Qt .h and .cpp files. But instead of main.cpp in Qt, I copied the .cpp code in my PS C++ project. When I run it it gives more than 10 errors like"\\home.org.a\ab\data\Documents\Qt projects\PS7\main.cpp:1060: error: C2664: 'void PSError(char *)': cannot convert argument 1 from 'const char [37]' to 'char *'' in main.cpp file.
I changed the the #include <tchar.h> to #include <char.h> in "stdafx.h" file, but the error decreased to two including ```
"\\home.org.a\ab\data\Documents\Qt projects\PS7\stdafx.h:11: error: C1083: Cannot open include file: 'char.h': No such file or directory"
PS. May I ask why I cannot see my previous posts in this page? I need sometimes to look at them, how can I do that?
-
Just to be clear, are you trying to build MSVC generate code with a MinGW build of Qt ?
If so why not just import the code you wrote in your VS project in your Qt project and dump the VS specific files that you anyway will not use in your Qt project ?If you have the infinite scroll activated, just scroll up otherwise you have a at the bottom a page selector.
-
@SGaist
I think I wrote by mistake. I am trying to run my C++ project using Qt Widget project.
I have these files VS I copied them in my Qt project folder, and added stdafx.cpp, stdafx.h, targetver.h and O_PS .H to the Qt. Instead of adding "O_PS .cpp" I copied its content to the "main.cpp" in Qt. For "po .cpp" also I just added
#include "stdafx.h" to the "mainwindow.cpp". Now I have these files in Qt: QtThe errors were explained in the previous post.
-
See here: http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio
stdafx.h seems to be generated by VS, so you should not use it with MinGW -
Please take a look at error messages you get:
"\\home.org.a\ab\data\Documents\Qt projects\PS7\main.cpp:1060: error: C2664: 'void PSError(char *)': cannot convert argument 1 from 'const char [37]' to 'char *'' in main.cpp file.
Now check the code lines mentioned in these error messages. Error message you posted tells you that you are passing a const char array to a function that takes a non-const char*. This is not allowed.
-
But previously it did not give that error, when I did the same with different versions of Qt5.5 and MCVS 2013. The number of errors
cannot convert argument 1 from 'const char [37]' to 'char *''
is about 12-13.
However, when I change the#include <tchar.h>
to
#inclu```de <char.h>
in "stdafx.h" file, the error decreased to two including
\\home.org.a\ab\data\Documents\Qt projects\PS7\stdafx.h:11: error: C1083: Cannot open include file: 'char.h': No such file or directory
errors.
-
I did the same process to read the C++ project in Qt Creator, but this time the error is ```
:-1: error: dependent '..\C++' does not exist.
These are my C++ project files:
O_PS.H
stdafx.h
targetver.h
O_PS.CPP
ps.cpp
stdafx.cppand I built a mainwindow Qt creator project (PS8) and copied all the files in the Qt folder except "ps.cpp" and "O_PS.CPP". The copied ones were added to the Qt project using "Add Existing Files". There are some differences including:
1- The "ps.cpp" contains only ```#include "stdafx.h"
which was added to "mainwindow.cpp".
2- "O_PS.CPP" content was copied in the "main.cpp" of the Qt project. The Qt PS8 project now includes these files. -
I'm going to repeat myself but: why not just start from a clean Qt project, copy the code you need to run over in that project and go on ?
You have some core logic in that visual studio project but rather than just take the current working code and move it over to your Qt project with maybe some adaptations, you are trying to grab every bits of that project and mix it with your Qt project when there's no real need.
-
@SGaist
I almost did the same, except I added the files "stdafx.h", "targetver.h", "stdafx.cpp", and "O_PS .H", which I think are needed. Because in this C++ code, only the "O_PSA .H" and "O_PS .CPP" include code.
The content of "stdafx.h" is #include```"targetver.h" #include <stdio.h> #include <tchar.h>
The content of "targetver.h" is ```
#include <SDKDDKVer.h>
The content of "stdafx.cpp" is ```
#include "stdafx.h"
And the content of "po .cpp" is ```
#include "stdafx.h"
But, if you mean I just copy the "O_PSA .H" and "O_PS .CPP" codes in "mainwindow.h" and "main.cpp", (here is the Qt project it gives many errors, and I think because all cpp and header files are necessary.
-
You have to fix the includes in your O_PS* files so add the missing
#include <stdio.h>