qDebug()<<qstring no longer compiling - qt

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.

Related

Qt - undefined reference to QJsonValue::toString

I'm trying to build a sample project using Qt 5.7 over Linux. Previously, the same project have been built with Qt 5.4.2 without issues. When I try with Qt 5.7, I get the followings errors:
undefined reference to `QJsonValue::toString() const'
I have tried installing Qt5.7 with Qt Maintenance Tool and also download from Qt official site and install it using .run file. When I run a locate to qjsonvalue I get the followings results:
$ locate qjsonvalue
/opt/Qt/5.4/Src/qtbase/include/QtCore/qjsonvalue.h
/opt/Qt/5.4/Src/qtbase/src/corelib/json/qjsonvalue.cpp
/opt/Qt/5.4/Src/qtbase/src/corelib/json/qjsonvalue.h
/opt/Qt/5.4/android_armv7/include/QtCore/qjsonvalue.h
/opt/Qt/5.4/android_x86/include/QtCore/qjsonvalue.h
/opt/Qt/5.4/gcc_64/include/QtCore/qjsonvalue.h
/usr/include/qt5/QtCore/qjsonvalue.h
/usr/share/doc/qt5/qtcore/qjsonvalue-members.html
/usr/share/doc/qt5/qtcore/qjsonvalue.html
According with the previous results, seems that I need to install something becuase qjsonvalue.h/cpp is not in my Qt5.7 core. Any ideas about how to solve it? Need to install anything else? As note, when I start Qt Maintenance Tool I get an alert message that says the following:
Your installation seems to be corrupted. Please consider re-installing from scratch.
Found this comming here via google. I had the same issue.
The API changed from a default argument
QString QJsonValue::toString(const QString & defaultValue = QString()) const
to two overloads.
QString QJsonValue::toString() const
QString QJsonValue::toString(const QString &defaultValue) const
You should build with the headers of the oldest version you want to support. But if you just need this one error fixed you can just change the call from
QJsonValue someval;
QString str = someval.toString();
to
QString str = someval.toString(QString());
But you will probably get some other linker error in the wake of fixing this one.

"Failed to make context current." on a computer without Qt

So I have been trying to develop a Qt application and run it on another Windows 8.1 machine which doesn't have Qt installed.
On my first computer, I used Visual Studio 2013 to develop my application and compiled it in release mode with the Multi-threaded DLL flag.
This produces a .exe file. In the same folder, I have:
accessible/ platform/
assimp.dll kernel32.dll
icudt52.dll icuin.dll
icuuc52.dll opengl32.dll
libEGL.dll libGLESv2.dll
Qt5Core.dll Qt5Gui.dll
Qt5OpenGL.dll Qt5Widgets.dll
msvcp110.dll msvcr110.dll
msvcp120.dll msvcr120.dll
I copied the Qt DLLs from C:/Qt32bits/5.3/msvc2013_opengl/. Except for libEGL.dll and libGLESv2.dll that I got from QtCreator as I couldn't find them in the previous folder. However, this shouldn't be a problem as I am compiling with msvc (not mingw), right?
In my code, at the very beginning I have:
QApplication app(argc, argv);
QGLFormat format;
format.setProfile(QGLFormat::CoreProfile);
format.setVersion(4, 0);
QGLFormat::setDefaultFormat(format);
/*And then later*/
cout << "Creating Window" << endl;
GLWidget *widget = new GLWidget(i); //i is the id of the widget
cout << "Widget created" << endl;
widget->makeCurrent();
cout << "Context current" << endl;
GLWidget derives from QGLWidget and in the constructor, I just set its ID attribute to i.
On my machine, everything works fine. But when I run my executable file on the second machine, I get the output:
"Creating Window"
"Failed to make context current."
"Widget created"
"Context current"
And then it crashes (MyApp.exe has stopped working)... What is weird is that "Failed to make context current" (which I believe was produced by makeCurrent()) comes before "Widget created"...
Any idea of what's wrong and how I could debug this?
Thank you very much for your time and help!
In the end it was a mix of problems:
The opengl32.dll that I was using seemed to be for 64 bits according to dependency walker (What's weird is that I took it from C:/System32/). I downloaded a new one for 32 bits and it worked fine. Thank you Sebastian Lange.
The second problem probably comes from the second machine's graphic card/OpenGL. I've tried on a third computer and now it works fine. Thank you vahancho.
Hope this can help future coders!

Run a program from Qt

I am working with Qt in Linux. I need to run the "evince" pdf reader from Qt.
I have been searching and as I understand I can use Qprocess to make it work. Something like this:
QStringList args;
args.append("/home/user/presentacion0.pdf");
QProcess p(this);
p.start("/usr/bin/evince", args, QIODevice::ReadWrite);
p.waitForFinished();
QString p_stdout = p.readAllStandardOutput();
QString p_stderr = p.readAllStandardError();
But I always get the following error:
No protocol specified
Cannot parse arguments: Cannot open display:
So I tried the system(char *cmd) from stdlib.h and does not work
system("/usr/bin/evince /home/user/presentacion0.pdf");
I think system() does not work because "evince" is a graphical application. However I can run "ls", "mkdir", etc. commands with system() without problems.
Please I need help with this topic.
I Really appreciate your help in advance.
Thank you.
You're running your Qt app from an application (most likely shell) that doesn't have DISPLAY environment variable set properly (it's usually :0). QProcess uses environment of the calling process.
If you're not really interested in grabbing evince's stdout and stderr, and you don't need to wait for its finish, then I would go with QProcess::startDetached().

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");

How to get command line arguments for a running process

In my program, I have been receiving an error when I use a
command-line compile command for mxmlc. The error is related to an
embedded font name not being correctly identified by flex in the
system fonts list.
However, on a whim, I decided to copy the code to Flex Builder and
compile it there. To my surprise, it worked, and it found the proper
font using the same system name I had given (PMingLiU).
I suspected my problem may be a locale one, and that my system cannot
correctly identify the font name because of locale considerations.
I've tried setting the locale of the compile code to en_US, to no
avail. So I would like to ask if anyone here knows how exactly Flex Builder invokes the MXML compiler and what differences there are compared to running mxmlc directly? We know it's not using the mxmlc.exe directly, since we tried replacing mxmlc with our own executable to capture the command line parameters.
If it matters, the OS used is Windows XP.
Although I don't have the exact answer to your question (what command line arguments Flex Builder passes to mxmlc.exe), I do have a meta-answer for you. You can find the command line by using one of two methods.
The first is platform-agnostic but will require you to compile a small C++ program. I've used this approach before when solving similar problems. What you can do is create a wrapper application which simply outputs the command line to a file. Build this application and drop it in as a temporary replacement for your mxmlc.exe, and when Flex Builder executes it you'll be able to access the resulting file "cmdline.txt" to get the full command line that it was called with:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
ofstream cmdLine;
cmdLine.open("cmdline.txt");
for (int i = 0; i < argc; i++) {
cmdLine << argv[i];
if (i < argc)
cmdLine << " ";
}
cmdLine.close();
return 0;
}
If you don't feel right about playing this dirty trick on Flex Builder, there is an alternative assuming you're running on Windows. You can use WMI to iterate over all of the running processes and grab their command line information. Ruby being my language of choice, this would require you to install the Ruby interpreter for Windows which you can do easily with the One-Click Ruby Installer for Windows.
After installing, just run this script as soon as Flex Builder kicks off your build:
require 'win32ole'
wmi = WIN32OLE.connect("winmgmts://")
processes = wmi.ExecQuery("select * from win32_process")
for process in processes do
cmdLine = process.CommandLine
puts "Command line: #{cmdLine}" if cmdLine =~ /mxmlc/
end
I've added in a regular expression to print out the command line only for processes which were started with "mxmlc" in the command line (which should work for your needs). For a more general solution of iterating over each process, just remove the if clause at the end of the line containing:
puts "Command line: #{cmdLine}" if cmdLine =~ /mxmlc/
This will save you the headache of doing any low-level magic with StartRemoteThread and navigating through the PEB structures.
That's about the best I could do considering the nature of your question and without more information regarding your development OS. If this solves your problem I might suggest that you edit your post so that people facing similar issues can find this solution. A title like "How to get command line arguments for a running process" might be more apt.

Resources