Qt5 QuickView cannot create window: no screens are available - c++

I receive this error (title, below) whenever I try to run the following code:
#include <QCoreApplication>
#include <QQuickView>
int main(int argc, char *argv[]){
QCoreApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl::fromLocalFile("app.qml"));
QObject *object = (QObject*)view.rootObject();
view.show();
delete object;
return app.exec();
}
Cannot create window: no screens available
The program has unexpectedly finished.
All I can find online for that error are bug reports arising from specific conditions significantly more involved than the above.
app.qml is a file that runs fine alone, i.e. without the above C++ and in a separate project configured as a 'Qt Quick UI'. Giving it's qrc:// path, or deliberately specifying a file which does not exist has no effect.
Note the QObject* cast - this was not present in the docs, but without it:
/main.cpp:11: error: cannot initialize a variable of type 'QObject *' with an rvalue of type 'QQuickItem *'
How should this be done?

The QCoreApplication can be used with console application, not with GUI ones, i.e. you have to use a QGuiApplication object. It seems to me that you created a console application instead of a graphical one.
You can create a proper application via the Qt Quick Application, add your "app.qml" as a resource to that project and call such a file instead of the default "main.qml", provided by the project template.
If you want to quick fix your current project, just check that the .pro file is set to import GUI libraries:
QT += gui qml quick
Set your qml file as a resource:
Create a new resource file via file -> new File or Project... -> Qt -> Qt Resource File
Right click the newly created .qrc file and click add existing file to add your "app.qml" file
Finally, rewrite your main like this:
#include <QQuickView>
#include <QGuiApplication>
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv); // GUI APPLICATION!!!
QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:///app.qml")));
view.show();
return a.exec();
}
However, going for the Qt Quick Application project would be the wiser choice.

Related

"Must construct a QApplication before a QWidget" error, but only on Windows builds?

I am working on a CMAKE C++ project which uses the QT Libraries. (For me, 5.15.3, for others 5.12.x)
In this project, there is a class Vtk3DViewer : public QWidget. In its constructor, it tries to create one of its member variables, which is of type QVTKOpenGLNativeWidget. This is from the VTK libraries. (Located in include\vtk-9.2\QVTKOpenGLNativeWidget.h)
For me, this new QVTKOpenGLNativeWidget() call within the constructor of my QWidget fails with the following error:
"Must construct a QApplication before a QWidget"
But that's just it, we do create a QApplication in main() well before this point. And this only happens on Windows. Linux builds appear to not have any issue.
Switching from Debug to RelWithDebugInfo moves the error - making it happen much earlier and on creating a QToolBarExt instead.
Why is this happening, and how do I fix it?
Here is an example of main():
int main(int argc, char* argv[])
{
// Set info for settings & registry
QApplication::setOrganizationName(COMPANY_NAME);
QApplication::setOrganizationDomain(COMPANY_DOMAIN);
QApplication::setApplicationName(APP_NAME);
// Set up for software-based backend for VTK
QApplication::setAttribute(Qt::AA_UseOpenGLES);
QApplication a(argc, argv);
a.setWindowIcon(QIcon(":/main-window/favicon.ico"));
// Instantiate singletons
TaskExecutionManager::getInstance(); // Instantiate the task manager
DataDispatcher::getInstance();
// Create main window with default size
MainWindow w;
w.show();
// Start application event loop
return a.exec();
}
Then the main window's constructor calls:
void MainWindow::initializeMainWindow(Ui::MainWindow* ui)
{
this->setDockOptions(AnimatedDocks | AllowNestedDocks | AllowTabbedDocks | GroupedDragging);
// Main toolbar
m_topToolBar = new QToolBarExt(this); // This causes a "Must construct a QApplication before a QWidget" error
}
The issue was that VTK was built in RELEASE, while our project was built in DEBUG.
(I did not see this as an issue, since we would never need to step into/debug VTK's code)
It appears this cryptic/incorrect error message will appear under these circumstances. Ensuring VTK and the project it includes are compiled the same way fixes it.

Use Qts QXmlSchemaValidator without QApplication

I need some help in Qt since I don't really undestand the way Qt libraries can be used in visual studio projects. I try to use the QXmlSchemaValidator class from QtXmlPatterns to validate an xml file against schema, but I can't instantiate a QApplication object since I don't have access to the main.cpp file. I don't want to create a Qt project, just try to use this schemaValidator class in one of a class' method.
This is how I try to load the schema:
QUrl url("http://.../schema.xsd");
QXmlSchema schema;
if (schema.load(url))
qDebug() << "schema is valid";
else
qDebug() << "schema is invalid";
I get this warning: "Please instantiate the QApplication object first".
I found a solution here: QEventLoop: Cannot be used without QApplication that says I need the main function to look like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Is there a way to load the schema and validate my xml files without a QApplication object?
Thanks in advance!
Yes, use QCoreApplication instead.
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//your code here
return a.exec();
}
Seriously, if some Qt features you like require an event loop, you just can't get away with it without one. About not having a "Qt project" (and maybe you mean you're not using qmake) but yet using Qt classes: good luck.

In Qt main function, how does QApplication learn about Mainwindow?

Looking at a simplest Qt Widget sample application that you can find from almost every Qt tutorial:
#include "notepad.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Notepad w;
w.show();
return a.exec();
}
There is one thing puzzles me. There are two major variables a and w here. a.exec() starts Qt's main loop, which suppose to interact with the main GUI component w. However, both of them live on stack and I don't see any code pass w somehow to a. So how does a be aware of the existence of w?
Does the constructor of w initializes a static data structure that a can access to check the top-level widgets?
Qt preprocess your code and build the real c++ code before compiling, its at this moment QApplication wrap all Q object in the main.cpp file and build the rest of the code from it.

Qt 5.1 QApplication Without Display - QXcbConnection: Could not connect to display

I'm using Qt5.1 and I'm trying to create a QApplication without a display. I need to draw text with QPainter, so I need to use QApplication (or QGuiApplication), otherwise I get a segfault.
The application worked fine in Qt4.8, but fails in Qt5.1 on a headless version of Ubuntu with the error:
"QXcbConnection: Could not connect to display".
In Qt 4.8, I was able to use the following constructor with GUIenabled = false to create a QApplication that did not require a display:
QApplication::QApplication ( int & argc, char ** argv, bool GUIenabled )
In Qt5.1, the constructor for QApplication no longer has the GUIenabled flag.
I scanned the source code briefly, and there does seem to be a flag in the QApplication constructor, but it is undocumented as to what options can be used in that flag. Using "false" does not work.
How can I create a QApplication without a display? Is there an alternative method to telling QApplication GUIenabled = false? Alternatively, can I create a QCoreApplication that will not segfault when drawing text with QPainter on a QImage?
Yes, that's a Qt 3 (?) thing that is gone in Qt 5. Try running your application with the -platform offscreen command line option instead.
Note that you don't need QApplication or linking to QtWidgets to just draw upon a QImage, using QGuiApplication (and linking to QtGui) is sufficient.
If you want to create an app without GUI, you need to use QCoreApplication instead of QApplication.
Just hit this same issue. Really annoying that it at least isn't a compile error. My solution was just to use pointers and heap objects like,
QCoreApplication* app = 0;
Display* display = XOpenDisplay(NULL);
if (display)
{
XCloseDisplay(display);
app = new QApplication(argc, argv);
qobject_cast<QApplication*>(app)->setQuitOnLastWindowClosed(false);
}
else
{
app = new QCoreApplication(argc, argv);
}
return app->exec();

Qt - What is meant by those lines of code

In the C++ GUI Programming with Qt 4 book, part of creating a dialog application, there was the following main.cpp file:
#include <QApplication>
#include <QDialog>
#include "ui_gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui::GoToCellDialog ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
dialog->show();
return app.exec();
}
Can you just describe those lines of code?
Ui::GoToCellDialog ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
Thanks.
"ui_gotocelldialog.h" is a file generated automatically based on the file "gotocelldialog.ui" which contains the GUI for the dialog. Ui::GoToCellDialog::setupUi() must be called for the UI to be initialized.
Lets take a look:
Ui::GoToCellDialog ui;
This line creates an instance of GoToCellDialog. As already have been said, this class automatically generated from the gotocelldialog.ui file. The use-case is:
Open qt-designer and make the interface you want.
Save the file (in our case gotocelldialog.ui)
In your .cpp file write #include "ui_gotocelldialog.h"
Now you can use the interface you designed
PROFIT????
Next:
QDialog *dialog = new QDialog;
This line creates new instance of the QDialog class that represents simple modal window (commonly called as dialog). But your window would be empty after this line. You need to place the controls, do you? How you can do this? Lets see:
ui.setupUi(dialog);
This line uses the interface you designed in the qt-designer. It places this interface to the newly created dialog. So you can see all the controls in the window. Pretty easy as for me.
It sets up the ui described by an qt ui xml file hosted inside of a dialog window/qdialog.