QtCreator: qDebug Messages Not Shown - qt

I am currently using QT Creator 3.2.1 with Qt 5.3.2 (as required by my current project) on Windows 7 (64 bits, Ultimate). I am currently working on a GUI project
I am unable to see any qDebug messages in the Application Output window despite already done the following:
Having the appropriate QDebug code
Building the project in Debug mode
Using "CONFIG += openssl-linked" "CONFIG += console" as additional arguments for building the project
Not defining QT_NO_DEBUG_OUTPUT at all
Confirming that I have a debugger (GDB from MinGW 4.8.2 32 bit installed during installing QtCreator)
May I know what else should I try? Thanks!

This did it for me on Arch Linux
Qt creator > Tools > Options > Kits, select your kit, find Environment, click change and add:
QT_ASSUME_STDERR_HAS_CONSOLE=1
Now the Message can be printed in the Application Output
with something like that:
qDebug() << "User clicked on the button!";
And than you should see something in the program like so:

I don't get any debug messages supposed to be printed out by
qDebug() in Qt Creator? What could be the reason for that?
It is common to redefine the standard Qt debug output in Qt apps via the custom log message handler but we can still take care of making debug messages to reach the debug console:
// example for custom message handler
void customLogHandler(QtMsgType type, const QMessageLogContext& context,
const QString& msg)
{
// send the data to log file via the other thread
emit writeToFile(type, context, msg);
// now output to debugger console
#ifdef Q_OS_WIN
OutputDebugString(text.toStdWString().c_str());
#else
std::cerr << text.toStdString() << std::endl;
#endif
}
void main()
{
// custom handler start
qInstallMessageHandler(&customLogHandler);
// other app initializations
}

Related

catch that new QApplication failed and try something else

I have two programs. One call SelectScreen and another called RadioPanel.
SelectScreen sends a message to RadioPanel telling it what screen its supposed to display the gui on.
RadioPanel uses setenv("DISPLAY", myHostslist[hostId].c_str(),true); to set the DISPLAY environment variable. Then a mQtApplication = new QApplication(mArgc, mArgv); to start the gui.
This works as long as the host info is correct. However if it is not correct QApplication causes the program to end. What I want to do is catch the fact that QApplication failed and try to run the gui on ":0"
I tried using a try catch block but it will not catch. My guess is that QApplication just ends the process and does not throw an exception in this case.
Does anyone know if there is a way to determine if QApplication is going to fail or respond to that failure.
The message I get from QApplication when it fails is:
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.```
I found a solution. If you use XOpenDisplay you can check the return to confirm that the X Server display is working before you attempt to create the QApplication.
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
void myUiApplication::qtGuiThread()
{
Display *dis;
dis=XOpenDisplay((char *)0);
if(dis!=nullptr)
{
XCloseDisplay(dis);
mQtApplication = new QApplication(mArgc, mArgv);
} else
{
CCS_ERR("Failed to display on host:" << cds::toString(mCdsId) << " "
<< mCdsHosts[mCdsId].c_str() << ".\nPlease edit the "
<< getConfigFileName() << " file. Attempting to run GUI on local X Server.");
setenv("DISPLAY",":0",true);
mQtApplication = new QApplication(mArgc, mArgv);
}
}

qDebug()<<qstring no longer compiling

I have built a QWidgets application in Qt 5.3.1 and in some places have used
qDebug() << msg;
where msg is a QString.
I had had this compiling and running for a few years, but tonight I decided to recompile it and I got the message:
D:\devt\myapp\extcoder.cpp:29: error: no matching function for call to 'QMessageLogger::debug()'
qDebug()<<msg;
^
In fact I got a similar problem in another app I wrote the other day, and by experimenting I thought I had fixed it by replacing such calls by qDebug(msg).
But it looks as if some kind of software rot is setting in!
Any ideas? Of course the file starts with
#include <QDebug>
To test this problem I built a barebones QWidgets application (of a QMainWindow kind), and the only code I wrote, was (apart from the #include)
qDebug()<<"Hello world";
in the MainWindow constructor. I get exactly the same compilation error.
Reinstallation of Qt made qDebug()<<s work again.
It was indeed a kind of "software rot": I reinstalled qt 5.3.1 after making a copy of the original so I could compare the new with the old.
Using Winmerge I was able to uncover the error:
I dont know exactly how it happened but the file qlogging.h got corrupted.
Here's what happened: at lines 118 and 120 the declarations of two versions of debug got rewritten to NBIS_debug.
Now I have worked trying to port free software from the NBIS. At some stage I must have renamed a debug function from debug to NBIS_debug, and this modification must have been propagated all the way to qlogging.h.
QDebug &QDebug::operator<<(const QString &s) is supported in Qt.
Output example:
QString s;
s = "a";
qDebug().noquote() << s; // prints: a
qDebug() << s; // prints: "a"
Your Qt environment must be broken. Please, check your environment and re-install Qt if needed.
I had to reinstall Ubuntu and Qt. The reinstall only Qt did not help.

Transmitting an exe file via network in Qt

I am trying to create a Remote compiling system in Qt. What I am trying to do is to get the C/C++ source from the client and calling the locally available GCC via QProcess on the server, create the exe file and then transmit the exe back to the client. Simple right?
The exe file is being generated perfectly but Qt is just not able to read the file into an array to transmit it.
My Code is:
QByteArray arr;
qDebug() << "To Client: exe";
QFile f("compiled.exe");
qDebug() << f.exists(); //output = false
QString path = QDir::toNativeSeparators(QFileInfo(f).absoluteFilePath());
qDebug() << path; //perfect path is coming
qDebug() << f.open(QIODevice::ReadOnly); //output = flase
arr = f.readAll(); // size of arr is 0
f1.close();
client->write(arr); //zero bytes written
Although the exe file is very much present, I am getting such outputs. I tried with other files in the same directory, it works absolutely perfectly. Only exe files wont work.
I am using Qt 4.8.1 32 bit on Windows using MSVC v16.00.40219.10 as the compiler.
Thanks in advance!!
No need to speculate, there is a description of error codes when using QFile::open().
Btw
qDebug() << f.exists(); //output = false
It seems compiled.exe is not in the process working directory, neither nor in any direcotry in path. Provide the complete path...

How can I see qDebug messages while debugging in QtCreator

I'm making the transition from Eclipse CDT (with Qt integration plugin) to QtCreator 2.0 but there is still one thing that bother me with QtCreator :
When I debug in QtCreator, I don't see my qDebug messages inside the Application output tab until I stop the application I'm debugging... Then they are all displayed at once which is not very useful.
With Eclipse, I don't have this problem : the qDebug messages are displayed correctly when encountered while stepping through the source code.
I'm using both Eclipse CDT and Qt Creator under Windows. I didn't try under Linux (which is not an option right now).
While not a complete answer, you can install DebugView (If you're on an XP machine) to view the qDebug output while you try to figure this out.
Another solution might be considered a hack, but works quite nicely, is to simply hijack debug messages yourself:
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <iostream>
void msgHandler( QtMsgType type, const char* msg )
{
const char symbols[] = { 'I', 'E', '!', 'X' };
QString output = QString("[%1] %2").arg( symbols[type] ).arg( msg );
std::cerr << output.toStdString() << std::endl;
if( type == QtFatalMsg ) abort();
}
int main(int argc, char *argv[])
{
qInstallMsgHandler( msgHandler );
QCoreApplication a(argc, argv);
qDebug() << "Hello world.";
qWarning() << "Uh, oh...";
qCritical() << "Oh, noes!";
qFatal( "AAAAAAAAAH!" );
return a.exec();
}
Which would output:
[I] Hello world.
[E] Uh, oh...
[!] Oh, noes!
[X] AAAAAAAAAH!
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
This is actually a modification of some code that I use myself to send qDebug, qWarning, etc., to a log file.
You don't have to close the application to see qDebug() messages.
There is a tab named 3 - Application output at the very bottom of the Qt Creator. Clicking that window will show you the Application output window at the bottom of Qt Creator.
That particular window will display the qDebug() messages as soon as they get called while the application is still running.
Hope it helps.
Edit:
I am not sure whether this is an answer but it might be a good valid cause.
From qDebug() documentation,
The Qt implementation of these
functions prints the text to the
stderr output under Unix/X11 and Mac
OS X. With Windows, if it is a console
application, the text is sent to
console; otherwise, it is sent to the
debugger.
Now Qt Creator doesn't have it's own debugger attached to it.
From Qt Creator documentation, we have to manually install the debugger. Since you are using Windows, you need to install Debugging tools for Windows manually.. More documentation can be found here...
Though am not used to Eclipse CDT, I assume there might be a Debugger attached to it and hence it displays the Debugging output correctly..
Since there isn't a Debugger attached to the Qt Creator, it might be behaving strangely..
Just give it a try..
To add to the above answers..
Always make sure to build in debug mode and not in release mode..
As qDebug() , only works with the debug build.
This was the silly mistake i made, before my search for the led me here, and i wanted to add this fine but important point, to the list of other answers.
Have you tried to add the following line to your .pro?
OUTPUT += Console
Then you can output on std::cout.
Let me tell you something:
Once I have developed a c++ console application on linux. During running the application it was reading a file and starting to process some logic by printing out some status messages. When I run that application the output was OK. So for convenient debugging I have decided to run the application like this:
./a.out |& tee log
This command redirects standard output (may be also standard error I don't remember) into a file named "log". So when I run with this option I saw that it writes in the log file exactly the same as in std out only there are some displacements like this:
in std out - desired output
A
operation 1 success
B
operation 2 success
C
operation 3 success
D
operation 4 success
in the log file - output with displacement (this is not correct)
A
B
C
D
operation 1 success
operation 2 success
operation 3 success
operation 4 success
I guess your problem is this kind of one... I have taken a look at QDebug and have not seen any function that frees the buffer (something like operation called flush). So I recommend you to not waist your time on this kind of small issue (I also consider that Qt Creator 2.0 is release as a beta version and it may appear a bug) and use one of the following:
qFatal()
qCritical()
qWarning()
QMessageBox
I personally use QMessageBox::about in order to debug as you can stop the screan and read the value in a very usefull places that with debugger you can't (I mean you can but you can't see the current state of you GUI application as debugger have taken the control).
Hope helps.
This is a workaround, but you could probably install your own message handler. Take a look at qInstallMsgHandler in the documentation if you have not done so already.
I have a similar issue on Ubuntu 9.10 with Qt Creator 2.0 and Qt 4.7.0, though I don't see stdout output until the application is closed. It is as if the buffer isn't flushed.
Printing to stderr shows the output in the Application output window immediately though.
fprintf(stderr, "Test1 \n"); // Prints immediately
fprintf(stderr, "Test2 \n\r"); // Prints immediately
fprintf(stdout, "Test3 \n"); // Delayed until app termination
fprintf(stdout, "Test4 \n\r"); // Delayed until app termination
qDebug() << "Test6 \n\r"; // Does not print at all
None of these answers were right for me (Arch linux user). Rather than try to explain my troubles, here is a .pro file that worked. I'm not using the QT thread loop, purely just a simple main() that does stuff and exists. I'm using cout for my output:
QT += core
QT -= gui
CONFIG += c++14
TARGET = evo
#CONFIG += console
CONFIG -= app_bundle
#CONFIG += qt
#OUTPUT += console
TEMPLATE = app
SOURCES += main.cpp \
individual.cpp \
node.cpp \
tree.cpp
HEADERS += \
individual.h \
node.h \
tree.h
If Anyone is still looking for an answer, this is what I did:
In Debug tab go to build
under build select debug not release or profile
While running your application click the Application Output tab
Then you will see all your debugs message if you used a "qDebug()" macro.
my 2 cents contrib, in your main, just before a.exec :
qputenv("QT_ASSUME_STDERR_HAS_CONSOLE", "1");

Console input with Qt Creator

I'm developping a very simple app on my Mac using QtCreator.
It's a console application and I want the user to enter its name, and then I display his name. Here is the code :
#include <iostream>
int main(int ArgC, char* ArgV[])
{
char Name[1000];
std::cout << "Type your name : ";
std::cin >> Name;
std::cout << "Hello " << Name << "\n";
return 0;
}
When running this app with QtCreator, the string "Type your name :" is displayed in the 'Application Output' tab. But if I type some text and press the enter key, nothing is happening.
What's wrong ?
I found a solution. With Qt Creator 1.3.0 (on Mac OS X), here is what I had to do :
Project->Run settings, check "Run in Terminal" (thanks Ropez)
Qt Creator->Preferences : Environnement : General : Terminal : I had to put the whole path to XTerm. For my config, I had to put /usr/x11/bin/xterm -e.
Now, everything is working fine !
Go to Project -> Run settings, and make sure "Run in Terminal" is checked.
BTW:
std::cin >> Name;
is probably not what you want. It will read just a single token (typically only the first name). You should have a look at getline, or the string version.
Jeromes solution is the proper one. Though I can give you a different alternative. In case you don't want to use X11 (for some reason anyhow) in the same position (QtCreator->Preferences->Environment:General:Terminal) you can give your path to the Terminal application like this:
/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
Enjoy!
Solution for Windows.
In the .pro file add:
QT -= core gui
TEMPLATE = app
CONFIG += console
Go to Project -> Run settings, and make sure "Run in Terminal" is checked.
I had the "Cannot start the terminal emulator 'xterm'" problem on Mac and fixed it by going to settings, Environment and clicking the "Reset" button next to the Terminal text field.
For some reason by default it just said "xterm -e" but after the reset it became an absolute path of "/usr/X11/bin/xterm -e".
My console app then ran fine.
For Mac-based Qt 2.4.0, click on the Project vertical tab, which is located below the "Debug" along the same vertical line as Welcome, Edit, Design. In Target-> Run, make sure "Run in terminal" is checked!

Resources