Solved QXcbConnection: Could not connect to display Abort trap (core dumped)

I'm trying to output a very basic (one-window based) QT application on my screen:

Code:
// this is mainwindow.cpp
#include"mainwindow.h"
#include"ui_mainwindow.h"

MainWindow::MainWindow(QWidget*parent):
QMainWindow(parent),
ui(newUi::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
deleteui;
}
Code:
// this is the main.cpp
#include"mainwindow.h"
#include<QApplication>

intmain(intargc,char*argv[])
{
QApplicationa(argc,argv);
MainWindoww;
w.show();

returna.exec();
}
.. and a mainwindow.ui that looks like this:
Code:
<?xmlversion="1.0"encoding="UTF-8"?>
<uiversion="4.0">
<class>MainWindow</class>
<widgetclass="QMainWindow"name="MainWindow">
<propertyname="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<propertyname="windowTitle">
<string>MainWindow</string>
</property>
<widgetclass="QWidget"name="centralWidget"/>
<widgetclass="QMenuBar"name="menuBar">
<propertyname="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>25</height>
</rect>
</property>
</widget>
<widgetclass="QToolBar"name="mainToolBar">
<attributename="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attributename="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widgetclass="QStatusBar"name="statusBar"/>
</widget>
<layoutdefaultspacing="6"margin="11"/>
<resources/>
<connections/>
</ui>

Nothing fancy. This code works fine on my Ubuntu machine. Now, I understand that FrameBuffer is not an option on FreeBSD, so I have xorg installed.

This is how my QT project folder looks like:
Code:
Makefile   
main.o       
moc_mainwindow.cpp
TestWindow   
mainwindow.cpp   
moc_mainwindow.o
TestWindow.core   
mainwindow.h   
ui_mainwindow.h
TestWindow.pro   
mainwindow.o
main.cpp   
mainwindow.ui

When I launch TestWindow ./TestWindow I get the following error:
Code:
QXcbConnection: Could not connect to display
Abort trap (core dumped)

I looked up this error, but I can't seem to make much sense of it. Would I need to first use xinit before launching any GUI QT-based app.? I have tried to launch this app. via ssh and also locally (on the FreeBSD machine) getting the same error.
 
I'll kind of try to answer my own question:
Would I need to first use xinit before launching any GUI QT-based app.?

The xinit(1) manual states:
If no specific client program is given on the command line, xinit will
look for a file in the user's home directory called .xinitrc to run as
a shell script to start up client programs. If no such file exists,
xinit will use the following as a default:

xterm -geometry +1+1 -n login -display :0
So, in my ~/ folder I added a .xinitrc file containing one line of code exec /usr/home/<usr_name>/<qt_compiled_app>
This makes it so that when I start a new xorg server instance with xinit it launches my QT app. So, yes. xinit is necessary.
 
I am having the exact same issue: running a Qt application from Konsole throws
Code:
[root@leo00 ~]# qt5ct
QXcbConnection: Could not connect to display
Abort trap (core dumped)

But the same runs fine from KDE launcher. The other programs run fine, examples VLC, Firefox, QtCreator. Only some throw this error.
 
Back
Top