The program has unexpectedly finished - app crashed - qt

I have a problem with Qt Charts in QML modeling. I have a simple application that implements a very simple ChartView. As I run the application, it crashed and I get the error in Application Output The program has unexpectedly finished.
I added in my src.pro
QT += qml quickcontrols2 charts
and my applications is this
import QtCharts 2.2
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ChartView {
anchors.fill: parent
theme: ChartView.ChartThemeBrownSand
antialiasing: true
PieSeries {
id: pieSeries
PieSlice { label: "eaten"; value: 94.9 }
PieSlice { label: "not yet eaten"; value: 5.1 }
}
}
}
How do I have this problem?

Check if you are using QGuiApplication instead of QApplication in your main.cpp.
The following example works properly, but it crashes if we use QGuiApplication in main():
Note: Since Qt Creator 3.0 the project created with Qt Quick Application wizard based on Qt Quick 2 template uses QGuiApplication by default. As Qt Charts utilizes Qt Graphics View Framework for drawing, QApplication must be used. The project created with the wizard is usable with Qt Charts after the QGuiApplication is replaced with QApplication.
More info here.
main.cpp
#include <QApplication>
#include <QQuickView>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQuickView *view = new QQuickView;
view->setSource(QUrl(QLatin1String("qrc:/main.qml")));
view->show();
return app.exec();
}
main.qml
import QtQuick.Controls 2.0
import QtQml 2.2
import QtCharts 2.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ChartView {
anchors.fill: parent
theme: ChartView.ChartThemeBrownSand
antialiasing: true
PieSeries {
id: pieSeries
PieSlice { label: "eaten"; value: 94.9 }
PieSlice { label: "not yet eaten"; value: 5.1 }
}
}
}

Related

QT6.1 - Qml Why does the ProgressBar not have an animation corresponding to the indeterminate property?

As the title says, I set the indeterminate property of the ProgressBar to True, but he doesn't have any animation.
Just like this:
But:
I use the default project and the code is very simple.
I would like to know if the indeterminate itself does not have any animation or what is wrong with it?
Thanks for your help.
By the way, this is the first time I'm looking for answers here, so I hope it will be a pleasant experience :)
version:
Qt6.1.1 MinGW 64-bit (default Debug Version)
The code is as follows:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
ProgressBar{
id: proBar
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
height: 20
from: 1
to: 1
indeterminate: true
}
}
Yes, that's all the code in my qml. The rest of the file did not change a word
I think it relates to your Qt version , I test your code in my Qt .
I use Qt5.14 and GCC compiler, the result is this :
For adding Style :
in main.cpp put this
QQuickStyle::setStyle("Universal");
like this :
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickStyle>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickStyle::setStyle("Universal");
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl)
{
if (!obj && (url == objUrl))
{
QCoreApplication::exit(-1);
}
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
and in .pro file add this :
QT += quick quickcontrols2
Edited code
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Universal 2.12
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Universal.theme: Universal.Dark
Universal.accent: Universal.Red
ProgressBar{
id: proBar
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
height: 70
from: 1
to: 1
indeterminate: true
}
}

Qml: Accessibility not working for all elements

I am adding accessibility to QML application. While using Windows Narrator or NVDA program, they are not able to read elements of ComboBox. They read ComboBox fine but not content of it.
Cannot post my project to posting example file here.
QML:
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.14
Item {
visible: true
width: 640
height: 480
ComboBox{
model: ["option1","option2","option3"]
Accessible.name: "ComboBox"
Accessible.description: "ComboBox"
}
}
Main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickView view;
const QUrl url(QStringLiteral("qrc:/main.qml"));
view.setSource(url);
view.show();
return app.exec();
}
Qt Version: 5.15.2, any help is appreciated
So, after browsing QT Bugs found out that QML is not fully supported for accessibility and QT is aware of it since 2016. This ticket is really helpful
https://bugreports.qt.io/browse/QTBUG-68465 and from comments
Qt should actually be setting focus to the menu's first child menu item, since this is thew behavior VoiceOver expects. We currently don't do this.
So by forcing focus this can be fixed, i am able to fix it.
Check focus:true that is most important part.
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
Item {
visible: true
width: 640
height: 480
ComboBox{
id: selectMe
model: ["selectMe1","selectMe2","selectMe3"]
focus: true
Accessible.role: Accessible.ComboBox
contentItem:
Text {
id: ld2
text: selectMe.currentText
}
hoverEnabled: true
popup: Popup {
y: selectMe.height - layoutRowHeight
width: selectMe.width
implicitHeight: contentItem.implicitHeight
padding: 1
focus: true
contentItem: ListView {
spacing: 2
focus: true
Accessible.role: Accessible.List
clip: true
implicitHeight: contentHeight
model: selectMe.popup.visible ? selectMe.delegateModel : null
delegate: ItemDelegate {
id: selectMeDelegate
width: selectMe.width
focus: true
contentItem:
Text {
text: modelData
color: "black"
font.pointSize: 12
elide: Text.ElideRight
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignLeft
focus: true
Accessible.name: "selectMe combobox"
Accessible.description: "selectMe"
}
background: Rectangle{
width: selectMe.width
height: selectMe.height
}
}
}
background: Rectangle {
color:"grey"
opacity: 0.5
radius: 10
}
}
}
}

How to set proper path to offline OSM directory in QML ( not using qrc )?

I would like to have folder with tiles realTiles in folder with .exe.
Of course I can add folder to qrc and everything is ok:
import QtQuick 2.10
import QtQuick.Window 2.11
import QtLocation 5.12
import QtPositioning 5.12
...
Map {
id: map
anchors.fill: parent
activeMapType: map.supportedMapTypes[0]
zoomLevel: 14
plugin: Plugin {
name: 'osm';
PluginParameter {
name: 'osm.mapping.offline.directory'
value: ':/realTiles/'
}
}
}
So I copy folder realTiles to folder with .exe and folder one above .exe. So for example I have folder realTiles in this 2 positions:
C:/Users/tom/Desktop/app/build-OsmOffline-Desktop_Qt_5_15_1_MinGW_64_bit-Release/release/realTiles
C:/Users/tom/Desktop/app/build-OsmOffline-Desktop_Qt_5_15_1_MinGW_64_bit-Release/realTiles
And I tried with Plugin Parameter' value:
value: 'file:///' + applicationDirPath + '/../realTiles/'
value: 'file:///realTiles/'
value: 'file:/realTiles/'
and many other. Of course I can did mistake. What is a real solution to this?
EDIT:
main.qml
import QtQuick 2.10
import QtQuick.Window 2.11
import QtLocation 5.12
import QtPositioning 5.12
Window {
id: win
objectName: "window"
visible: true
width: 512
height: 512
Map {
id: map
anchors.fill: parent
activeMapType: map.supportedMapTypes[0]
plugin: Plugin {
name: 'osm';
PluginParameter {
name: 'osm.mapping.offline.directory'
//value: ':/realTiles/'
value: 'file:///' + applicationDirPath + '/../realTiles/'
}
}
}
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QStandardPaths>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("applicationDirPath", QGuiApplication::applicationDirPath());
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
Try to give the full path
PluginParameter {
name: "osm.mapping.cache.directory"
value: "C:/Users/UserName/Desktop/OSMCache"
}
I have tried to change the cache directory it worked. So provide the full path in the value field.

Make new window from separate file appear or disappear with QQmlApplicationEngine

I'm using QQmlApplicationEngine то load my main window.
QQmlApplicationEngine engine;
engine.load("GUI.qml");
Where GUI.qml is my main GUI file. How I can create and destruct a new window from code? As far as I can see, if I write engine.load("SecondWindow.qml");, how I can close it? Or I should create and destruct such objects from QML itself?
Option 1: You can do it from QML, see this sample:
import QtQuick 2.12
import QtQuick.Controls 2.5
ApplicationWindow {
id: window
visible: true
width: 640
height: 480
title: "Window 1"
CheckBox {
id: cb
text: "Show Window #2"
}
Loader {
active: cb.checked
sourceComponent: Component {
ApplicationWindow { // Or "SecondWindow"
visible: true
width: 640
height: 480
title: "Window 2"
}
}
}
}
Option 2: Also, you can control it from C++ side, for example like this:
QML
import QtQuick 2.12
import QtQuick.Controls 2.5
ApplicationWindow {
id: window
visible: true
width: 640
height: 480
title: "Window 1"
Loader {
active: showWindowFlag
// Instead of "sourceComponent" you can use
// source: "SecondWindow.qml"
sourceComponent: Component {
ApplicationWindow {
visible: true
width: 640
height: 480
title: "Window 2"
}
}
}
}
C++
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
bool showWindowFlag = false;
engine.rootContext()->setContextProperty("showWindowFlag", showWindowFlag); // !!!!
auto timer = new QTimer(&engine); // Parent will delete timer
QObject::connect(timer, &QTimer::timeout, [&](){
showWindowFlag = !showWindowFlag;
engine.rootContext()->setContextProperty("showWindowFlag", showWindowFlag);
});
timer->setInterval(1000);
timer->setSingleShot(false);
timer->start();
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}

Popup item loses opaquness

I have a weird problem with Popup QML component from QtQuick2 , when I do open() it, it shows with transparent background , and it should be opaque.
This is how I call the Popup component from the main.qml file :
NewUser {
id: new_user_form
}
This is the component's source:
// File: NewUser.qml
Popup {
id: new_user_popup
modal: true
focus: true
x: 10
y: 10
width: 300
height: 200
Rectangle {
anchors.fill: parent
color: "transparent"
border.color: "red"
}
}
And this is the output:
Now, I can fix this error by moving the source of the NewUser.qml into main.qml and everything looks fine now:
This is the now 'fixed' main.qml:
Popup {
id: new_user_form
modal: true
focus: true
x: 10
y: 10
width: 300
height: 200
}
See, the popup is completely OPAQUE:
So, why if I move the source code of the component into a separate file from main.qml it loses the opaquness ? My main.qml has a lot of other stuff, but I believe it is not related to the popup, also the ID is unique. I hope this is not some issue with QT 5.8 RC (which I am using for development) , it is not yet official but soon it will be.
EDIT:
I believe I found a bug. The bug is reproducible with Qt 5.8 and Qt 5.7
To reproduce, create a project with the following files:
Main file:
//main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
NewUser {
id: new_user_form
}
Button {
text: "open popup"
onClicked: {
new_user_form.open()
}
}
}
The component file (NewUser.qml)
//File: NewUser.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.3
import Qt.labs.settings 1.0
import QtQuick.Templates 2.0
Popup {
id: new_user_popup
modal: true
focus: true
x: 10; y:10;
height: 200; width: 300;
Button {
text: "Test button"
}
}
//File: qtquickcontrols2.conf
; This file can be edited to change the style of the application
; See Styling Qt Quick Controls 2 in the documentation for details:
; http://doc.qt.io/qt-5/qtquickcontrols2-styles.html
[Controls]
Style=Material
[Universal]
Theme=Light
;Accent=Steel
[Material]
Theme=Light
;Accent=BlueGrey
;Primary=BlueGray
//File: main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
return app.exec();
}
That does the bug do? If you click on the button "open popup" you will see a dimmed screen but no popup at all.
This is not a bug. See, you are not using a namespace for the templates import.
import QtQuick.Templates 2.0 as T
For the sake of clarity, there is a one-to-one mapping between the types provided by the QtQuick.Templates and QtQuick.Controls imports. For every type available in the QtQuick.Controls import, a non-visual template type by the same name exists in the QtQuick.Templates import. It is recommended to use a namespace for the templates import to avoid overlap with the types provided by the QtQuick.Controls import.

Resources