Non Qt console app in Qt Creator - qt

I want to create the simple console app below in Qt Creator:
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "Hello WOrld";
return 0;
}
I've seen some possible duplicates on SO, I have ticked the "Run in Terminal" option in Run Settings. A console window does pop up on CTRL+R, but it does not display "Hello World", simply "Press Enter to exit".
The above is by creating an Empty Project.
I have tried creating a "Qt Console Application" which generates the code below. This does work fine, but I want the simple non Qt version above.
#include <QtCore/QCoreApplication>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout << "Hello World";
return a.exec();
}

Besides ticking "Run in Terminal" you need to add "CONFIG += console" to your .pro file (if you are using qmake).
TEMPLATE = app
CONFIG += console
SOURCES += main.cpp

After trying Qt again after a long time, it now works. The project file has "CONFIG -=qt" by default. I'm not sure if this alone would have solved the problem back then, but it is the only difference I can see.
Full .pro file:
TEMPLATE = app
CONFIG += console
CONFIG -= qt
SOURCES += main.cpp

The only fault I can see with that example is that the stream is not flushed (please change the std::cout line to:
std::cout << "Hello World" << std::endl;
However, that is unlikely to be the issue you have, although the following example that I've found at http://www.richelbilderbeek.nl/CppQtHelloWorldConsole.htm implies that it is indeed a buffer handling issue where QtCreator makes some assumptions regarding buffering. (Note that that url adds a std::cin.get() call, which forces the application to pause, and thus, you should certainly see some output).

If you stumbled over this thread, because your application instantly exits and the console just shows "Press enter to exit":
This is how your application behaves, if you launch it from QtCreator and it can't find dependent DLLs (very unhelpful, by the way). To find out what exactly is missing, you can start your application without QtCreator. Dependent DLLs have to be in one of the locations listed here http://msdn.microsoft.com/de-de/library/7d83bc18.aspx.

Related

moc failing with 'Undefined interface' with Qt 5.10 in a Docker container

A very simple Qt project fails to build for me with Qt 5.10 in a Docker container (with an image derived from opensuse:tumbleweed). The project is as follows:
sh-4.4# cat test.pro
TEMPLATE = app
TARGET = test
INCLUDEPATH += .
INCLUDEPATH += sub
HEADERS = obj.h sub/iface.h
SOURCES = obj.cpp main.cpp
sh-4.4# cat sub/iface.h
#pragma once
#include <QtPlugin>
class Interface
{
public:
virtual ~Interface () {}
};
Q_DECLARE_INTERFACE (Interface, "org.meh.interface/1.0")
sh-4.4# cat obj.h
#pragma once
#include <QObject>
#include <sub/iface.h>
class Obj : public QObject
{
Q_OBJECT
Q_INTERFACES (Interface)
};
sh-4.4# cat obj.cpp
#include "obj.h"
sh-4.4# cat main.cpp
int main() {}
In this case moc complains as follows:
obj.h:9: Error: Undefined interface
Everything is fine in another container with Qt 5.9, and everything is also fine with Qt 5.10 when the project is built in openSUSE Build Service (which uses something else instead of Docker). Some quick googling did not reveal any relevant bugreports for recent Qt versions.
What could be wrong?
Running moc under strace shows Operation not permitted on various statx calls, which sheds some light on why exactly it fails (also, related to this question). This pull request is hopefully going to fix this.
Did you try to run the container with the --privileged (see Which capabilities are needed for statx to stop giving EPERM)?

How to include a .dll using Qt Creator

I've followed the steps described in https://wiki.qt.io/How_to_link_to_a_dll
but, somehow I still get undefined reference errors.
This is what I did:
add to my .pro file the library folder path via INCLUDEPATH+=
add to my .pro file the .dll via LIBS+=
include the .dll (in my case "okFrontPanelDLL.h") via #include in my code
I don't know if it matters, but my library is taken from: http://intantech.com/files/RhythmStim_API_Release_170328.zip
and the extracted folder contains a single .dll and multiple source and header files (do i have to add all sources and headers from the library via SOURCES+= and HEADERS+=?).
Currently, I can declare a variable based on a class defined in the library
okCFrontPanel *dev;
but accessing functions defined for the class, e.g. calling the constructor like
dev = new okCFrontPanel;
leads to an undefined reference error.
edit: I tried direcly adding the source and header files form the library into my Sources folder instead of linking the library and the code works fine, so there is (probably) at least nothing wrong with how I am trying to use the functionalities of the library.
edit2: further information:
OS: Win 7 64 bit
Qt version: 5.9.0
compiler: MinGW 32bit
file location: /[PROJECT FOLDER]/mylibrary
#include <QCoreApplication>
#include "myLibrary/okFrontPanelDLL.h"
int main(int argc, char *argv[])
{
// QCoreApplication a(argc, argv);
// return a.exec();
printf("hello world\n");
okCFrontPanel *dev;
dev = new okCFrontPanel;
// dev->BoardModel;
// If only one Opal Kelly board is plugged in to the host computer, we can use this.
dev->OpenBySerial();
// Set XEM6010 PLL to default configuration to produce 100 MHz FPGA clock.
dev->LoadDefaultPLLConfiguration();
// Upload RhythmStim bitfile which is compiled from RhythmStim Verilog code.
dev->ConfigureFPGA("mylibrary/main.bit");
printf("omg dis is working\n");
}

How to use QWS_MOUSE_PROTO inside code in Qt

I am using QT to make an application for Embedded linux device. When I started my application, mouse & keyboard was not working. From searching about this problem, I came to know that we need to run below command before starting the application:
export QWS_MOUSE_PROTO="USB:/dev/input/event-mouse"
export QWS_KEYBOARD="USB:/dev/input/event-keyboard"
After running above commands, I was able to use mouse and keyboard in my application. But this looks a bit odd because whenever I need to run my application, I have to run those commands. Also I will be setting my application to auto run after the boot so in that case I won't be able to run those commands. So I was wondering if I can include these commands somewhere in my code so that they automatically run and then application starts. Can anyone please guide me here. Please help. Thanks.
Instead of running your application, you could run a script:
#! /usr/bin/env bash
export QWS_MOUSE_PROTO="USB:/dev/input/event-mouse"
export QWS_KEYBOARD="USB:/dev/input/event-keyboard"
my_application
You can also set the environment variables inside of your application, before Qt is started. Use setenv:
#include <cstdlib>
int main(int argc, char ** argv) {
// Set default values if none are set.
setenv("QWS_MOUSE_PROTO", "USB:/dev/input/event-mouse", 0);
setenv("QWS_KEYBOARD", "USB:/dev/input/event-keyboard", 0);
QApplication app(argc, argv);
...
return app.exec();
}
External QWS_MOUSE_PROTO and QWS_KEYBOARD will override the internal defaults since the override parameter is set to zero. This is the desired behavior.

Qt and using an existing win32 dll

I want to do some development work using Qt. I have built several small apps and followed
some tutorials. All's fine and seems straight forward.
The development to be done involves using existing code that is contained in win32 dlls. I want to reuse this code with minimum fuss and link them into my Qt app. I have the headers, libs and the dlls so I'll be linking at compile time not dynamically at runtime.
I have tried to do this but Qt always complains always complains with link errors similar to:
main.obj:-1: error: LNK2019: unresolved external symbol _imp_Add referenced in function _main
No matter how I tweak the .pro file it always complains.
I've spent many hours googling and found snippets of info. I could not find one answer that told the whole story. What I'm after is a set of steps, a tutorial like sequence that needs to be followed. There may even be an example in the Qt installation examples but I've been unable to find it.
Here is the simple 'knock-up' I've been trying to get to work in order to move on to the main development. It's based on the MS tutorial dll MathsFunc.
The win32 dll:
// Visual Studio 2005
//Funcs.h
#ifdef MATHFUNCS_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif
// Returns a + b
MATHFUNCSDLL_API double Add(double a, double b);
#ifdef __cplusplus
} /* Assume C declarations for C++ */
#endif
//Funcs.cpp
#include "Funcs.h"
double Add(double a, double b)
{
return a + b;
}
The Qt app that imports the dll.
//main.cpp
#include <QCoreApplication>
#include "../../mathfuncs/funcs.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
double dResult = Add(1.0,2.0);
printf("1 + 2 = %f\n",dResult);
return a.exec();
}
//The Qt project file .por
#-------------------------------------------------
#
# Project created by QtCreator 2013-03-04T09:16:18
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = Useit
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += c:/tmp/mathfuncs
HEADERS += c:/tmp/mathfuncs
LIBS += c:/tmp/mathfuncs/MathFuncs.lib
Thanks in advance for any input.
D.

QT Creator exits with code 0 when running program

I am trying to run a simple OpenCV program in QT Creator 2.3, QT 4.7.4. I know the syntax is correct, but my program does not get run. When I run it, I simply get the qtcreator_process_stub.exe window with "Press <RETURN> to close this window...".
Why is this? My .pro file looks as such:
QT += core
QT -= gui
TARGET = myQtConsoleProject
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:\\opencv\\release\\include
LIBS += -LC:\\opencv\\release\\lib \
-lopencv_core231.dll \
-lopencv_highgui231.dll \
-lopencv_imgproc231.dll \
-lopencv_features2d231.dll \
-lopencv_calib3d231.dll
The application output is
Starting C:\Users\chris\QT\myQtConsoleProject-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release\release\myQtConsoleProject.exe...
C:\Users\chris\QT\myQtConsoleProject-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release
\release\myQtConsoleProject.exe exited with code 0
The contents of my source code is as follows:
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
printf("not outputting...\n");
cv::Mat image= cv::imread("C:/temp/img.jpg");
cv::namedWindow("My Image");
cv::imshow("My Image", image);
cv::waitKey(50000);
return 1;
}
I've added C:\opencv\release\bin to my path.
The fact that your console window does not show any lines except the "Press to close" line means that your application does not output anything to the console.
I see you have a console project configured, meaning it has no GUI. But due to the fact that your program compiles fine this might not be a problem.
Can you post the code of your main() function? The behavior you describe seems to be related to your code, not the project configuration.

Resources