In Qt Creator I have a main.cpp file that of course contains the int main(int argc, char **argv) and in my MainWindow.h file it has code to set up a window, which is functional ( I did not post the code as it is not relevant ). The problem arises when since I am trying to use the msvc compiler (which I have successfully set up), Because when I use the MinGW compiler it runs correctly and sets up the window. However in my main.cpp file ->
#include <QCoreApplication>
#include <iostream>
#include "Engine/Window/MainWindow.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argc);
std::cout << "Starting application" << std::endl;
MainWindow w;
w.execute();
return a.exec();
}
When I run the application I don't see "Starting Application" I just see "Press <return> to close the window" Any help or tips would be greatly appreciated.
Edit: I would also like to note that I just used Qt's "auto detect" functionality to find these compilers.
Also my .pro file
QT += core
QT -= gui widgets
CONFIG += c++14
CONFIG += windeployqt
TARGET = BaneEngine_
CONFIG += console
CONFIG += app_bundle
INCLUDEPATH += $$PWD/ExtLibs/include
DEPENDPATH += $$PWD/ExtLibs/include
LIBS += -L$$PWD/ExtLibs/libs
LIBS += -lSDL2main -lSDL2test -lSDL2 -lglew32 -lOpenGL32
TEMPLATE = app
SOURCES += main.cpp \
Engine/Window/MainWindow.cpp
HEADERS += \
Engine/Window/MainWindow.h
#include <QApplication>
#include <iostream>
#include "Engine/Window/MainWindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argc);
std::cout << "Starting application" << std::endl;
MainWindow w;
w.show();
return a.exec();
}
Related
I'm trying to get global position of the mouse however the functions mentioned in older topics are either deprecated or not working as intended. I have tried QCursor::pos() as seen below but it didn't work as intended.
#include <QtCore/QCoreApplication>
#include <QCursor>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while(1)
{
std::cout << QCursor::pos().x() << " " << QCursor::pos().y() << std::endl;
}
return a.exec();
}
Output: 2147483647 2147483647
It is very simple. QCursor is in the Gui module of Qt see here, so you have to change QCoreApplication to QGuiApplication and then it works (already tested it).
I have created a simple service application for testing with QtService. This can be easily executed in the QtCreator using the command line arguments:
-exec: This allows me to debug the service in QtCreator
-install: This allows the service to be installed (Alternatively, I can also install the service via cmd with the sc command).
After I have installed the service, I try to start it in the Windows service management. However, the error message appears: The service "MyService" on "Local computer" could not be started. Error 1053: The service did not respond to the start or control request in time.
This error message appears immediately without trying to start the service for 30 seconds. I tried debug and release mode.
My guess is that Qt dlls must also be next to the EXE. But I don't know which.
simpleService.pro
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
myservice.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
include(qtservice/src/qtservice.pri)
HEADERS += \
myservice.h
myservice.h
#ifndef MYSERVICE_H
#define MYSERVICE_H
#include <qtservice.h>
#include <QCoreApplication>
#include <QDebug>
#include <QObject>
class MyService: public QtService<QCoreApplication>
{
public:
MyService(int argc, char **argv);
~MyService();
protected:
void start();
void pause();
void resume();
void stop();
void createApplication(int &argc, char **argv);
private:
QStringList _args;
};
#endif // MYSERVICE_H
myservice.cpp
#include "myservice.h"
MyService::MyService(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, "MyService7")
{
try {
qDebug() << "CONSTRUCTOR";
setServiceDescription("This is my service. ");
setServiceFlags(QtServiceBase::CanBeSuspended); // able to resume
qDebug() << "CONSTRUCTOR 1";
} catch (...) {
qCritical() << "An unknown error occured in constructor";
}
}
MyService::~MyService()
{
qDebug() << "DECONSTRUCTOR";
}
void MyService::start()
{
qDebug() << "START";
try {
QCoreApplication *app = application(); // nessesary for windows
qDebug() << "Service started";
qDebug() << app->applicationDirPath();
} catch (...) {
qCritical() << "An unknown error occured in start";
}
}
void MyService::pause()
{
qDebug() << "PAUSE";
}
void MyService::resume()
{
qDebug() << "RESUME";
}
void MyService::stop()
{
qDebug() << "STOP";
}
void MyService::createApplication(int &argc, char **argv)
{
for (int i = 0; i < argc; i++)
{
_args.append(QString(argv[i]));
qDebug() << "Arg: " << argv[i];
}
QtService::createApplication(argc, argv);
}
main.cpp
#include "myservice.h"
#include <QCoreApplication>
int main(int argc, char *argv[])
{
MyService service(argc, argv);
return service.exec();
}
I found a solution:
In Qt 5.13.1(MinGW 7.3.0 64-bit) Compiler (In my case) change the directory to exe file. Then enter following command: windeployqt.exe .
Put the QtSolutions_service-head.dll next to exe file.
Now I can start the service.
on win10,if you want it to work as service ,you should install it in powershell in admin mode, or it would not work!
I have an application that when run through terminal, the user has the option between command-line mode or GUI mode.
There doesn't seem to be any output to the console at all when using std::cout. std::cout statements don't work in the main event loop.
I have added CONFIG += console to my .pro file.
For now, I have been using QTextStream() which works fine:
QTextStream(cout) << "Hello World" << std::endl;
My question is:
Why can I not use std::cout? Does this have something to do with Qt affecting input and output streams? I couldn't find any documentation in Qt's docs on this.
int main(int argc, char *argv[])
{
std::cout << argv[1] << std::endl; //This is being outputted.
//if(argc == 2 && !strcmp(argv[1],"-win")){
if(true){ //Just for this example's sake
QApplication a(argc, argv);
std::cout << "Hello" << std::endl; //This is not being ouputted.
MainWindow w;
w.show();
return a.exec();
}
else
{
qDebug() << "Console Mode.\n";
std::cout << "Console Mode.\n";
//Do stuff
}
}
This is not a Qt issue, but how std::cout works. You seem to blow up your std::cout in here:
std::cout << argv[1] << std::endl;
Your issue can be reproduced even with a simple program like this:
main.pro
TEMPLATE = app
TARGET = main
CONFIG -= qt
SOURCES += main.cpp
main.cpp
#include <iostream>
int main(int /*argc*/, char **argv)
{
std::cout << argv[1] << std::endl;
std::cout << "Hello stdout!" << std::endl;
if (std::cout.bad())
std::cerr << "I/O error while reading\n";
return 0;
}
Build and Run
Success: qmake && make && ./main foo
Failure: qmake && make && ./main
In your case argv[1] is nil and so this makes std::cout not to print anything more. I would suggest to either pass an argument all the time and/or check against argc with some help usage print. The best would be to use the builtin command line parser in QtCore these days.
You could ask why? Because it is undefined behavior. You can read the details from the documentation:
basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb);
After constructing and checking the sentry object, checks if sb is a null pointer. If it is, executes setstate(badbit) and exits.
If you happen to have an issue with the IDE itself, for instance QtCreator, then follow these steps in case of QtCreator:
Projects -> Select a kit -> Run tab -> Run section -> Arguments
Works OK for me:
QT += core
QT -= gui
TARGET = untitled
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
main.cpp:
#include <QCoreApplication>
#include <QTextStream>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout << "Hello World" << std::endl;
return a.exec();
}
EDIT:
#include <QCoreApplication>
#include <QTextStream>
#include <QtWidgets/QWidget>
#include <QDebug>
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "test" << std::endl; // <--- THE PROBLEM IS HERE...IF YOU TRY A SIMPLE STRING IT WORKS FINE SO THE PROBLEM IS argv[1] IS AN EMPTY STRING
//if(argc == 2 && !strcmp(argv[1],"-win")){
if(true){
//Just for this example's sake
QCoreApplication a(argc, argv);
std::cout << "Hello" << std::endl; //This is not being ouputted.
return a.exec();
}
else
{
qDebug() << "Console Mode.\n";
std::cout << "Console Mode.\n";
//Do stuff
} }
I have a problem under Qt, Actually I want to use opencv under it (ubuntu) and there is a crash.
If I compile under the terminal :
g++ pkg-config --cflags opencv example.cc -o output_file pkg-config --libs opencv
All is all right but under QT there is a crashed problem and I just read this message error :
Starting /home/quentin/build-test_opencv-Desktop_Qt_5_2_1_GCC_64bit-Release/test_opencv...
The program has unexpectedly finished.
/home/quentin/build-test_opencv-Desktop_Qt_5_2_1_GCC_64bit-Release/test_opencv crashed
This is my .pro :
QT += core
QT -= gui
TARGET = test_opencv
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
CONFIG += link_pkgconfig
PKGCONFIG += opencv
SOURCES += main.cpp
INCLUDEPATH += -I /usr/local/include/opencv
LIBS += `pkg-config opencv --libs`
and this is my main.cpp :
#include <QCoreApplication>
#include <iostream>
#include "cv.h"
#include "highgui.h"
using namespace std;
int main(int argc, char *argv[])
{
IplImage* img = cvLoadImage( "lena.png" );
cout << "Image WIDTH = " << img->width << endl;
cout << "Image HEIGHT = " << img->height << endl;
cvReleaseImage( &img );
return 0;
}
Most likely cvLoadImage fails and returns nullptr. You never bother checking for that.
What version of openCV do you use? What berak means is that IplImage is not used in the newer versions of openCV. Use Mat instead of IplImage. Try this code and tell me what happens:
#include <QCoreApplication>
#include <iostream>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
int main(int argc, char *argv[])
{
Mat* img = new cv::Mat(cv::imread("lena.png",CV_LOAD_IMAGE_COLOR));
if(img == NULL){
perror("Could not load image");
}
std::cout << "Image WIDTH = " << img->cols << std::endl;
std::cout << "Image HEIGHT = " << img->rows << std::endl;
img->release();
return 0;
}
This will work in opencv 2.4.X. Also make sure your image is in the same folder as your program. Please tell me of any error.
This question already has answers here:
How to deal with "%1" in the argument of QString::arg()?
(3 answers)
Closed 9 years ago.
I have tried to use the example given in the Qt4.8 documentation:
#include <QCoreApplication>
#include <QString>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString str;
str = "%1 %2";
str.arg("%1f", "Hello"); // returns "%1f Hello"
std::cout << str.toStdString().c_str() << std::endl;
str.arg("%1f").arg("Hello"); // returns "Hellof %2"
std::cout << str.toStdString().c_str() << std::endl;
return a.exec();
}
However this outputs :
%1 %2
%1 %2
both times. I have tried this on Windows 7 and Ubuntu, using QtCreator and from the command line. I have checked I have
QMake version 2.01a
Using Qt version 4.8.1 in /usr/lib/x86_64-linux-gnu
and in Windows:
QMake version 2.01a
Using Qt version 4.7.0 in C:\qt\4.7.0\lib
I have even checked my source files for non-ascii characters, e.g. the "%" sign is correct. Please tell me why this doesn't work!
Here is the PRO file I am using:
QT += core
QT -= gui
TARGET = testarg
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
The arg() functions do not modify the QString (if you look at the docs, these functions are const.) They return a new QString instead, which you aren't saving anywhere. If you want to modify the original string, you can do so with:
str = str.arg("%1f", "Hello");
If you want to preserve the original, just use a new QString instead:
QString tmp = str.arg("%1f", "Hello");
#include <QCoreApplication>
#include <QString>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString str;
str = "%1 %2";
QString a = str.arg("%1f", "Hello"); // returns "%1f Hello"
std::cout << a.toStdString().c_str() << std::endl;
QString b = str.arg("%1f").arg("Hello"); // returns "Hellof %2"
std::cout << b.toStdString().c_str() << std::endl;
return a.exec();
}
note all arg overloads are const and return QString :).