Segmentation Fault when using some Qt5 classes in a QtQuick 2 application - c++

When trying to use some Qt-5 classes I'm experiencing crashes. I first discovered this trying to use QFileSystemModel. Trying to call setRootPath immediately leads to a crash. The callstack isn't of much help (all of it is assembly code) except that QFileIconProvider::icon() is the last function called before the seg fault occurs.
So next I tried using QFileIconProvider manually and -to no surprise- it also crashed the program.
I'm using QtCreator 4 and the type of project is "Qt Quick Application". When I instead create a project of type "Qt Widgets Application", I can use both QFileIconProvider and QFileSystemModel without problems.
Here's where I'm out of ideas. I don't know enough about the Qt environment to know what difference between the two types of projects could lead to the seg fault.
Both projects use the same Kit (same gcc, same Qt 5.6.1) and the default settings as set by QtCreator.
This is my project.pro file:
TEMPLATE = app
QT += qml quick widgets //default .pro file except for widgets
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
This is main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDir>
#include <QFileSystemModel>
#include <QQmlContext>
#include <QFileIconProvider>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
//If trying to use QFileSystemMode...
QFileSystemModel model;
model.setRootPath("/somefolder/"); //..the crash happens here
//Attempting to use QFileIconProvider also crashes
//QFileIconProvider fip;
//fip.icon( QFileInfo("/somefolder/somefile") ); //<- here
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
I'd appreciate any help or pointers as to how to debug that mess.

As confusing as it may sound, QFileSystemModel is part of QtWidgets and therefore requires you to create and instance of QApplication instead of QGuiApplication.

Related

how to add qtvirtualkeyboard to a qt widget project

I have a Qt Widget project that I created using QtCreator and Qt version 5.15.2 to which I'm trying to add the QtVirtualKeyboard as matchbox-keyboard I've already tried using stays under the application when it's in fullscreen mode.
However I'm having trouble getting it to work as it's not appearing at all at the moment. This is how I've tried adding it
Main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
MainWindow w;
//w.showFullScreen();
w.show();
return a.exec();
}
I've tried adding QT += qtvirtualkeyboard or QT += virtualkeyboard in the .pro file but it just gives me an error saying "unknown module"
How can I add the virtual keyboard to the project?
You will need to make sure that the QtVirtualKeyboard library is selected during the installation process.
Also, I would recommend you that you start using cmake for Qt applications as Qt has officially dropped qmake since the release of Qt 6.
It is not to say that you cannot use it, but you will get better support and results by using an actively developed build system rather than an abandoned.

Why Qt is giving me error about "inability to create directory" of a project?

The thing is I am new to Qt, above at all, I am even more new to using frameworks. It is first time that I am using Qt framework for developing GUI for my end-semester programming project as Electrical Engineering Student.
But after installing Qt v5.6, when I made a project and compiled it then I got this frustrating error.
It's quite simple:
Do not use <QtModule/QClass> includes: they hide project misconfiguration and delay errors until linking.
After making sure that the .pro file contains relevant modules, you must re-run qmake and build the project. Or simply delete the entire build folder: this will force qmake to run again.
QCoreApplication cannot be used with widgets. Use QApplication instead.
Your program can look as follows:
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv) {
QApplication app(argc, argv);
QPushButton button("Hello, World!");
QObject::connect(&button, &QPushButton::clicked, &app, &QCoreApplication::quit);
button.show();
return app.exec();
}
The .pro file is very simple:
QT = widgets
TEMPLATE = app
TARGET = MyExample
SOURCES = main.cpp

Cmake link issue: undefined reference to QPushButton

I just started using Qt. I have a problem with compiling the first example.
main.cpp:
#include <QCoreApplication>
#include <QPushButton>
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QPushButton button ("Hello world !");
return app.exec();
}
CMake.txt:
cmake_minimum_required(VERSION 2.6)
project(new)
find_package(Qt4 REQUIRED)
enable_testing()
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
set(source_SRCS main.cpp)
qt4_automoc(${source_SRCS})
add_executable(new ${source_SRCS})
target_link_libraries(new${QT_QTCORE_LIBRARY})
add_subdirectory(tests)
install(TARGETS new RUNTIME DESTINATION .)
The error I get upon building is:
undefined reference to `QPushButton::QPushButton(QString const&,QWidget*)'
It is a linking problem, but how can I solve it?
Here is what I think you are missing:
find_package(Qt4 REQUIRED QtGui)
looking at your cmake you probably want to change the target_link_libraries for the following:
target_link_libraries(new ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})
You have three problems:
You're not linking with the Gui module (Widgets module in Qt 5). This is covered in the other answer.
You you must use QApplication in widgets-based applications. Since QPushButton comes from the Gui module (Widgets in Qt5), you can't merely use QCoreApplication nor QGuiApplication: your program will crash as soon as you attempt to instantiate a QWidget.
You're not showing the button, so when your program starts you'll see nothing once you fix the above.
Your main.cpp should look like:
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
#include <QtGui>
#else
#include <QtWidgets>
#endif
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QPushButton button ("Hello world !");
button.show();
return app.exec();
}
You're also going to need to link against the QtGui and QtWidgets library. Within qmake it handles which of the many libraries make up Qt, you'll have to do this by hand in cmake.
If you look at the documentation for QPushButton (http://doc.qt.io/qt-5/qpushbutton.html), the "qmake" line shows what library you need.
Consider using qmake instead of cmake with Qt.
Anyway, QCoreApplication (see docs) is the console version of main application class and will not work in GUI application. QPushButton is a widget class and can exist alone and will make a window (although you must show() it explicitely for that) but only with QApplication.
When using qmake in your *.pro file you need to include widgets like so:
CONFIG += widgets
and make sure you don't have
CONFIG -= gui
If you insist on using cmake then see here.
this answer solves my same problem :)
ok,
i had can solve it by myself.
For all they have they problem too:
The error is sourced from the use of "Q_OBJECT".
To solve the error, right-cklick on the Project and choose "Run qmake" and after >this: "Rebuild".
Then the error should be disappeared ;-)
-casisto
https://forum.qt.io/topic/52439/get-undefined-reference-error-but-don-t-know-why/2

QML C++ application crash outside Qt Creator

I am using Qt 5.1.1 with Visual Studio 2012 compiler. I have a Quick 2 application with a c++ backend. When i'm running application in Qt Creator everything looks good.
But when i'm trying to launch application outside from Qt Creator it crashes.
I have all Qt*.dll 's in my folder, also have D3DCompiler_46.dll.
I tried to access some filed in my QML file from C++ part, and I see that after crash in Visual Studio debugger that the crash look like null pointer exception.
QObject *openButton = instanceRoot->findChild<QObject*>("openFileButton");
openButton->setProperty("enabled",QVariant(enabled));
I have all my resources declared in qrc file, and it also included in my .pro file as
RESOURCES += res.qrc
By this article that menas, that all my qml files should be emdeded in my exe files. But I think it shouldn't.
How can I find and fix this problem?
UPD:
Here is my main function:
int main(int argc, char *argv[]) {
QGuiApplication a(argc, argv);
Q_INIT_RESOURCE(res);
QQuickView view;
Decrypter dec;
view.rootContext()->setContextProperty("Decrypter", &dec);
QObject::connect(view.engine(), SIGNAL(quit()), &a, SLOT(quit()));
view.setSource(QUrl("qrc:/PlayerGUI.qml"));
view.show();
QQuickItem *rootObject = view.rootObject();
Player *player = new Player(rootObject);
return a.exec();
}
UPD2: Two persons from comments were confused by one string of the code:
view.engine()->addImportPath("../QtAV/qml/");
So, I deleted it, and still have a crash(

MSVC2012 Qt supposed to include a directory?

I built a custom Qt5 for msvc2012 using BlueGo.
I was reading the examples and they show this:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(320, 240);
window.show();
window.setWindowTitle(
QApplication::translate("toplevel", "Top-level widget"));
return app.exec();
}
Problem is, QtGui for me is actually a directory and not a file so it cannot be included. I'm using the include files under /qtbase/include/. Am I doing something wrong?
The QtGui header actually exists and simply includes all headers from the QtGui module. You can find it inside the QtGui directory. The compiler is able to find it because the QtGui directory is specified in the include paths. In other words, it's the same as:
#include <QtGui/QtGui>
It's a terrible practice to include the QtGui header though. You should only include what you actually use, otherwise compilation times will increase for no good reason. However, for quick tests and such, it's quite handy.
I know it is a bit late time now but you can do it like this:
add the gui module and widgets in the pro file :
QT += widgets core gui
And by replaging the include files,
replace
#include <QtGui/QWidget>
#include <QtGui/QApplication>
with
#include <QWidget>
#include <QApplication>
The compiler should recognize it.