Could not initialize GLX -- CLI on virtual machine - qt

I created a Qt app with a GUI that can also run on the command line (by never calling QMainWindow::show()). When I try to run it on a Debian virtual machine I get the error:
$ xvfb-run ./myApp
Could not initialize GLX
Aborted
I built it on Ubuntu 16.04 with dynamic linking to the Qt libs and copied over the needed libraries. It was previously working, but started giving this error after I updated the app. How can I find out if this error is due to missing dependency or some issue with xvfb?

$ xvfb-run ./myApp
Xvfb doesn't support GLX / OpenGL. That's all. Either use a full blown Xorg server with GPU drivers or a headless EGL context.

The problem is most likely because you instantiate QApplication or QGuiApplication: not showing the window is not enough. When you run from the command line, you also need to use QCoreApplication only.
#include <QtWidgets>
#include <memory>
#ifdef Q_OS_WIN
#include <io.h>
int isatty(int fd) { return _isatty(fd); }
#else
#include <unistd.h>
#endif
using MyWindow = QWidget;
bool onCommandLine() {
return isatty(0);
}
int main(int argc, char **argv) {
std::unique_ptr<QCoreApplication> app(
onCommandLine() ? new QCoreApplication(argc, argv)
: new QApplication(argc, argv));
/* common logic goes here, e.g. argument parsing, etc. */
if (!onCommandLine()) {
MyWindow w;
w.show();
return app->exec();
} else
return 0;
}

Related

user-defined literals and whitespace and c++11 issues

I'm compiling very simple code and as output I receive error:
`../untitled6/main.cpp:17:1: error: unable to find string literal operator 'operator"" __FILE__'
connect(&d_t, SIGNAL(timeout()), this, SLOT(doPlay()));`
The code is following:
#include <QObject>
#include <QTimer>
class Test : public QObject
{
Q_OBJECT
public:
explicit Test(QObject *parent = 0) : QObject(parent) {}
void Play()
{
connect(&d_t, SIGNAL(timeout()), this, SLOT(doPlay()));
}
public slots:
void doPlay() {}
private:
QTimer d_t;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Test test;
test.Play();
return a.exec();
}
It is happened only if I include c++11 support in my project. Without this support the compilation is Okey. I have read about user-defined literals and whitespace for gcc ver. 4.7 when c++11 support is included. But my code doesn't include any FILE code.... I found that problem is related to SIGNAL and SLOT constructions. But I have no idea what is wrong here...
P.S. For compilation I use sh4-linux-g++ (GCC) 4.8.
I have found that this issue don't observed for release build configuration. It seems it is issue for debug build configuration...
This has been fixed in Qt 4.8.1:
https://bugreports.qt.io/browse/QTBUG-22847
You should upgrade.

QProcess with 'cmd' command does not result in command-line window

I am porting code from MinGW to MSVC2013/MSVC2015 and found a problem.
QProcess process;
QString program = "cmd.exe";
QStringList arguments = QStringList() << "/K" << "python.exe";
process.startDetached(program, arguments);
When I use MinGW, this code results in command-line window. But when I use MSVC2013 or MSVC2015, the same code results in cmd-process running in background without any windows. Are there any ways to make command-line window appear?
The problem was connected with msvc2015, not with Qt5.8.0. There is the way to escape it. The idea is to use CREATE_NEW_CONSOLE flag.
#include <QProcess>
#include <QString>
#include <QStringList>
#include "Windows.h"
class QDetachableProcess
: public QProcess {
public:
QDetachableProcess(QObject *parent = 0)
: QProcess(parent) {
}
void detach() {
waitForStarted();
setProcessState(QProcess::NotRunning);
}
};
int main(int argc, char *argv[]) {
QDetachableProcess process;
QString program = "cmd.exe";
QStringList arguments = QStringList() << "/K" << "python.exe";
process.setCreateProcessArgumentsModifier(
[](QProcess::CreateProcessArguments *args) {
args->flags |= CREATE_NEW_CONSOLE;
args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
});
process.start(program, arguments);
process.detach();
return 0;
}
You don't need to use QProcess for this. It's much simpler to just use std::system:
#include <cstdlib>
// then when you want to open a
// detached command prompt:
std::system("cmd");
You can also do things like:
std::system("cd some/path && cmd");
It's standard C++ (from C) so std::system(...) itself will work on any platform, only thing you need to set per platform is the shell command.

QSharedMemory::handle doesnt exist error

I'm trying to run the following QT code :
#include <QtCore/QCoreApplication>
#include <QSharedMemory>
#include <QDebug>
QSharedMemory g_objSharedMemory(QString("Shared Memory"));
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if(g_objSharedMemory.isAttached()==false)
{
qDebug()<<"Shared memory is not attached !!!!trying to attach it\n ";
qDebug()<<g_objSharedMemory.errorString();
if(g_objSharedMemory.attach()==false)
{
qDebug()<<"Failed to attach shared memory to the process!!!!";
qDebug()<<g_objSharedMemory.errorString();
return 0;
}
}
return a.exec();
}
I've failed to attach the shared memory segment to the process. I'm building this code on windows XP.
I'm getting QSharedMemory::handle doesnt exist error.
How can i fix this error?
You need to create() the shared memory segment in one of the processes which are using it. Most likely, you have one "master" or "server" process which is started first - let this process create the shared memory with a specific size:
qDebug()<<"Creating shared memory ...";
if(g_objSharedMemory.create(42) == false) {
qDebug() << "Failed to create shared memory!!!!";
qDebug() << g_objSharedMemory.errorString();
}
Then, in your "slave" or "client" processes, you should be able to attach to the shared memory with the same key.
Note that create() also attaches the process, so you must not call attach() in the master process.

I just cannot get QTcpServer working (newConnection never called)

I know similar question to this have been asked, but I haven't found an answer that fixes my problem.
I'm adapting some existing Qt code to add server functionality to a program my company uses. To that end I added a QTcpServer object to the existing dialog, call listen() and connect a slot to the newConnection emitter, like:
.h
class QConsole : public QDialog
{
Q_OBJECT
public:
void init();
public slots:
void new_Connection();
private:
QTcpServer m_Server;
}
.cpp
void QConsole::init()
{
m_Server.listen(QHostAddress::Any, 12346);
QDialog::connect(&m_Server, SIGNAL(newConnection()), this, SLOT(new_Connection()));
}
Main is:
int main( int argc, char *argv[] )
{
QApplication app(argc, argv);
QConsole * _output_window = new QConsole(desktopRect);
_output_window->init();
_output_window->show();
return app.exec();
}
new_Connection() never gets called so I can't see the relevance, but here it is:
void QConsole::new_Connection()
{
}
This works fine in that my program starts listening on the port specified and if I telnet to it a connection of sorts it made, but new_Connection() is never ever ever called!
I've seen posts on this problem dating back to 2005 so it's obviously not a new thing, but what I haven't found is a satisfactory answer to the problem (or any answer actually). This has got everyone at work stumped, even the person that has written a Qt server program. I'm guessing that there is something fundamentally wrong with the existing framework, but I have no idea what it might be.
I have been tearing my hair out for a day and a half over this, and the closes I got to success was using waitForNewConnection() which would actually return me a socket, but when I connected to the readReady() emitter, that was never fired either. So what would prevent these signals never getting called?
Please spare my sanity and help me as much as you can.
Here is a complete working example, tested using MSVC++ 2010.
This listens for a connection on port 12346, replies with "HELLO WORLD" and logs the connection to a list on the dialog.
main.cpp
#include <QtGui>
#include "console.hpp"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Console con;
con.show();
return app.exec();
}
console.hpp
#include <QtCore>
#include <QtGui>
#include <QtNetwork>
class Console : public QDialog
{
Q_OBJECT
public:
Console();
public slots:
void connection();
private:
QTcpServer mServer;
QListWidget* mConnList;
};
console.cpp
#include "console.hpp"
Console::Console() :
QDialog(),
mServer(),
mConnList(new QListWidget())
{
if (!mServer.listen(QHostAddress::Any, 12346))
qDebug() << "Error during 'listen'" << mServer.errorString();
connect(&mServer, SIGNAL(newConnection()), this, SLOT(connection()));
QVBoxLayout* mainLayout = new QVBoxLayout();
mainLayout->addWidget(mConnList);
setLayout(mainLayout);
}
void Console::connection()
{
qDebug() << "CONNECTION";
QTcpSocket* skt = mServer.nextPendingConnection();
if (!skt)
return;
mConnList->addItem(QString("%1:%2").arg(skt->peerAddress().toString()).arg(skt->peerPort()));
skt->write("HELLO WORLD!\r\n");
skt->close();
}
test.pro
TEMPLATE=app
CONFIG+=console debug
QT=core gui network
HEADERS=console.hpp
SOURCES=main.cpp console.cpp
Another working example, again on Linux, although I have coded a program using QTcpServer to run on both Linux and Windows before without a problem. If this doesn't work, surely it must be either a Qt installation or OS configuration problem. Either that or a bug in the Qt version.
~/tcp_test$ qmake --version
QMake version 2.01a
Using Qt version 4.8.6 in /usr/lib/x86_64-linux-gnu
~/tcp_test$ for file in qconsole.{h,cpp} main.cpp tcp_test.pro ; do echo -e "$file:\n"; cat $file; echo; echo; done
qconsole.h:
#include <QDialog>
#include <QTcpServer>
class QConsole : public QDialog
{
Q_OBJECT
public:
QConsole();
public slots:
void connection();
private:
QTcpServer server;
};
qconsole.cpp:
#include "qconsole.h"
QConsole::QConsole()
{
server.listen(QHostAddress::Any, 12346);
QDialog::connect(&server, SIGNAL(newConnection()), this, SLOT(connection()));
}
void QConsole::connection()
{
qDebug("got connection");
}
main.cpp:
#include <QApplication>
#include "qconsole.h"
int main( int argc, char *argv[] )
{
QApplication app(argc, argv);
QConsole * window = new QConsole();
window->show();
return app.exec();
}
tcp_test.pro:
QT = core gui network
CONFIG += debug
TARGET = tcp_test
SOURCES = main.cpp qconsole.cpp
HEADERS = qconsole.h
~/tcp_test$ ./tcp_test &
[3] 9784
~/tcp_test$ nc localhost 12346
got connection
^C

When I run my Qt messaging application it stops with exit code -1073741515

main:
#include "QtGui/QApplication"
#include "spc_login.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SPC_LOGIN w;
//#if defined(Q_WS_S60)
// w.showFullScreen();
//#else
// w.show();
//#endif
w.showMaximized();
return a.exec();
}
..other code:
protected:
void changeEvent(QEvent *e);
private:
// QAction *softKeyAction;
// QAction *leftSoftKeyAction;
Ui::THREESPC_VERIFYINGNUMBER *ui;
QHttp *getSinHttp;
QHttp *getOutboundSMSHttp;
QHttp *putStatusHttp;
QXmlStreamReader xmlGetSinReader;
QXmlStreamReader xmlCallOutboundReader;
QXmlStreamReader xmlPutStatusReader;
QTimer timer;
QMessageId sendId;
QMessageManager manager;
QMessageService service;
When I comment out QMessageId and QMessageManager varibales then it works fine but when I uncomment these fields, it doesn't work and shows me the exit code -1073741515. Please help me.
Starting C:\NokiaQtSDK\QtCreator\bin\SPCWIDGET-build-simulator\debug\SPCWIDGET.exe...
C:\NokiaQtSDK\QtCreator\bin\SPCWIDGET-build-simulator\debug\SPCWIDGET.exe exited with code -1073741515
From the Installation guide of QtMobility about the Messaging module:
While not supported for this release the desktop Windows backend requires that a MAPI subsystem is installed. Note that messaging functionality will not work if you are using the MinGW compiler. Additionally note that CE MAPI is available on Windows Mobile and does not need to be installed separately.
Which means that you cannot run your application on the desktop. You will have to use an emulator or you have to deploy your app to a device.

Resources