I have 2 machines Debian 7.8 64/32 bit. I create a simple program. In main.cpp:
void action(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QFile logFile("logfile.log");
static QTextStream ts(&logFile);
if(logFile.open(QFile::ReadWrite | QFile::Append))
{
ts << context.file << ":" << context.line << ":"
<< context.function << ": " << msg << endl;
logFile.close();
}
}
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
qInstallMessageHandler(action);
qDebug() << "this is some log";
return app.exec();
}
In the "logfile.log" I must see:
main.cpp:30:int main(int, char**): this is some log
but on Debian 7.8 64 bit Qt 5.4.1 GCC 4.6.1 64 bit I just see:
:0:: this is some log
I also tested on Debian 7.8 32 bit Qt 5.3.1 GCC 4.6.1 32 bit. It runs well.
Is it a bug of Qt 5.4.1 (64 bit)? Or I missed something?
Can you help me?
Most likely, you are using release build. By default Qt doesn't fill those fields in release mode. You can override this and enable context logging by defining QT_MESSAGELOGCONTEXT as explained in this answer.
Related
I try to use Qt with CLion. My problem is that qDebug() doesn't print anything, but qInfo(), qWarning(), qCritical() and qFatal() works well. Here is my example code :
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
qDebug() << "This will not appear";
qInfo() << "This will appear";
qWarning() << "This will appear too";
qCritical() << "This will appear too";
return a.exec();
}
Specs :
Qt : 5.5.1
GCC : 5.3.1 (64bits)
CMake : 3.3.2
CLion : 1.2.2
Fixed by editing the ~/.config/QtProject/qtlogging.ini file as follows :
[Rules]
*.debug=true
Related Bugzilla entry
Try to enter ctrl+shift+alt+/ ->Registry and tick therun.processes.with.pty.
I am writing a small application on linux using qt creator.
When i start my application i want it to execute a shell command. I`m using QProcess for it like this:
int main(int argc, char *argv[])
{
MyApplication a(argc, argv);
QProcess mapProc(&a);
QString command;
QStringList args;
command = "java";
args << "-jar" << "/home/$USER/MapServer/map.jar" << "localhost" << "9797" << "12123";
mapProc.start(command, args);
bool flag = mapProc.waitForStarted();
QProcess::ProcessState state = mapProc.state();
qDebug() << mapProc.errorString();
qDebug() << mapProc.pid();
/*/////////////////
some code
/////////////////*/
return a.exec();
}
but when my application started, process "mapProc" becomes a zombie. Why? what am i doing wrong?
$USER will not really work like that with QProcess. You will need to invoke the command through /bin/sh -c "mycmd" or even better if you just do it the proper Qt way as indicated below.
Try using QStandardPaths, so write this:
QString homeLocation =
QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
args << "-jar" << QString(homeLocation.first() + "/MapServer/map.jar")
<< "localhost" << "9797" << "12123";
instead of this:
args << "-jar" << "/home/$USER/MapServer/map.jar"
<< "localhost" << "9797" << "12123";
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
} }
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 :).
I'm facing strange problem. Namely, Qt somehow turns off exception handling in my program. I can't catch any exception, and when I throw an exception application crashes.
I'm using Qt 4.7.0 (32 bit) from Qt SDK v2010.05 on Windows 7 (64 bit), g++ (GCC) 4.5.1 from MinGW, NetBeans 6.9.1.
But I also cheked this with g++ 3.4.5 (also from MinGW) and Qt Creator 2.0.1 - same strange behavior.
For example (simplest case):
#include <Qt/QApplication.h>
#include <iostream>
#include <stdexcept>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
try {
cout << "Before exception" << endl;
throw runtime_error("Exception occured");
cout << "After exception" << endl;
} catch (runtime_error& exc) {
cout << exc.what() << endl;
exit(1);
}
return 0;
}
When I execute above program I've got this output:
Before exception
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
I've tried to add flag "-fexceptions" to g++ but it hasn't changed anything.
When I don't use Qt, everything is OK:
#include <Qt/QApplication.h> // It is not caused only by including Qt header
// so it doesn't matter if I comment this out or not
#include <iostream>
#include <stdexcept>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
// QApplication app(argc, argv);
try {
cout << "Before exception" << endl;
throw runtime_error("Exception occured");
cout << "After exception" << endl;
} catch (runtime_error& exc) {
cout << exc.what() << endl;
exit(1);
}
return 0;
}
The output:
Before exception
Exception occured
Does anybody know why is this happen that way and how to fix this? Has it something to do with type of exception handling method (SJLJ or Dwarf-2) used when Qt was build?
I've reconfigured and recompiled Qt with flag -exceptions:
D:\Qt\2010.05\qt>mingw32-make confclean && configure -exceptions && mingw32-make
and now everything is ok!
Thanks all for help, especially to Nick D!
Anyway, it's very strange that I had Qt build without this flag. I had downloaded Qt SDK in binary form from official site.
It's no longer necessary to use the -exceptions flag with Qt. In Qt Creator 4 it's the default, and my Windows Qt app happily uses vast and extensive exception handling with no problems. Qt MSVC builds use the /EHsc compiler option, which turns normal exception handling on.