catch that new QApplication failed and try something else - qt

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

Related

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.

Qprocess and avrdude

i'm trying to create a simple QT program that allows me to launch avrdude without using command line operations.
I saw that with Qprocess it's easy to launch any kind of program and I tried succesfully with simple programs like Explorer Word and Others.
The problem is that when i try to open cmd.exe nothing happens, even if i try to pass a batch file containing all the information to launch correctly avrdude.
Here's the code
QProcess *process = new QProcess(this);
process->startDetached("cmd.exe",QStringList()<<"C:/avrdude/avr.bat");
I wrote a minimal sample application which shows how to start cmd with a command using QProcess::startDetached() (on button click):
// standard C++ header:
#include <iostream>
// Qt header:
#include <QApplication>
#include <QMainWindow>
#include <QPushButton>
#include <QProcess>
int main(int argc, char **argv)
{
// main application
#undef qApp // undef macro qApp out of the way
QApplication qApp(argc, argv);
QMainWindow qWin;
QPushButton qBtn(QString::fromLatin1("Start cmd"));
QObject::connect(&qBtn, &QPushButton::clicked,
[](bool) {
bool ret = QProcess::startDetached(
#if 1 // let Windows search for cmd.exe in %PATH%
QString::fromLatin1("cmd.exe"),
#else // define exact path of cmd.exe
QString::fromLatin1("C:\\Windows\\system32\\cmd.exe"),
#endif // 1
QStringList()
<< QString::fromLatin1("/K")
<< QString::fromLatin1("echo Hello"));
std::cout << "QProcess::startDetached():"
<< (ret ? "OK." : "Failed!") << std::endl;
});
qWin.setCentralWidget(&qBtn);
qWin.show();
return qApp.exec();
}
The Qt project file is left as exercise. (Sorry, I used CMake for this.)
Please, note the #if 1. If 1 is replaced by 0 the alternative code with full path is used. (During chat session we examined special problems with starting the cmd.exe.) On my system, both alternatives did as well.
(My system: Windows 10, VS2013, Qt 5.7)
I too have been working on a Qt program where there are a couple calls to AVRDUDE. This is what worked for me. Here's the code I made for a read of the AVR device through AVRDUDE, followed by a couple of comments.
void MainWindow::call_AVRDUDE_read() //AVR READ
{
QProcess CommandPrompt;
QStringList Arguments;
QString COMPortUsed = (ui->COM_Port_Used->text()); // get the COM port from the user off UI
Arguments << "/C avrdude -c arduino -P "+ COMPortUsed +" -b 115200 -p ATmega328P -e -U eeprom:r:fromEEPROM.bin:r";
CommandPrompt.start("cmd",Arguments);
CommandPrompt.waitForFinished();
}
Here's something else which may well influence things in your application. In my case, I am reading the AVR's EEPROM. There is another routine that writes the EEPROM, but it is essentially the same as above, but a different script is sent.
In BOTH these cases, the AVRDUDE operation takes a few seconds to perform its task. When you use the QProcess::startDetached(), it has the disadvantage that control will return IMMEDIATELY after the AVRDUDE script is called through the QProcess. This can cause problems, if for instance you wanted to (as in my case) read the contents of the EEPROM and try to do so before the read actually completes.
An alternative to startDetached() you might consider trying is shown below. This will retain control until the process is finished, which may be pretty important to you. Use these two lines to replace the startDetached() call you are currently using.
CommandPrompt.start("cmd",Arguments);
CommandPrompt.waitForFinished();
This will wait for the AVRDUDE process to finish before control is returned.
The take away here though is that QProces::startDetached() may return prematurely in your application. Just beware of that.

QtCreator: qDebug Messages Not Shown

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
}

Reasons SFML TCPListener would fail to bind to port?

I'm just getting my feet wet with networking however I do understand the terminology. I was in the process of starting a Server-Multiple client project using SFML 2.3.2 in Visual C++ 2015 and got this far:
#include "stdafx.h"
#include <iostream>
#include "SFML/Network.hpp"
const unsigned short PORT = 5000;
void Server(void)
{
sf::SocketSelector TCPSelect;
// Add listener
sf::TcpListener TCPListen;
if (!TCPListen.listen(PORT))
{
std::cout << "Failed to bind Listener on port " << PORT << std::endl;
}
}
int main()
{
Server();
return 0;
}
Output: Failed to bind Listener on port 5000
I have used port 5000 for other small network projects, but this is the first time I am using a TcpListener. I tried running the code as is and with everything in the main(). When printing TCPListen.listen directly it returns the error status code. I have tried different ports of course, some that are port-forwarded on my router, some that aren't (if it mattered).
Is this an issue regarding my code? (If so, what are the means of fixing it?) Or is this an issue regarding something else?
I think I seem to have answered my own question. My error was in misinterpreting documentation on the subject of TcpListener when setting up my error message.
The function listen() returns 0 if everything works correctly. I cross checked this by printing the listener's port after calling listen(), which returned 5000.

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

Resources