How to set relative path instead of absolute - qt

I am using Qt Quick Application project in QT 5.2.1 in Linux Mint 13 and I want use relative path to my audio files, which are located in sounds folder of my project folder.
Here is my
main.qml
import QtQuick 2.2
import QtQuick.Window 2.1
import QtMultimedia 5.0
Window {
visible: true
width: 360
height: 360
Audio{
id: sound
source: "sounds/1.mp3" //doesn't work
source: "file:../sounds/1.mp3" //also doesn't work
source: "file:/home/vlado/Qt projects/test521/sounds/1.mp3" //this works
}
MouseArea {
anchors.fill: parent
onClicked: {
sound.play()
}
}
Text {
text: qsTr("Play Sound")
anchors.centerIn: parent
}
}
Doesn't work means, that there is following error:
GStreamer; Unable to play - "" Error: "No URI set"
As you can see, everything is OK, if I use absolute path, but I would like to deploy this application and installation folder then will be different. That's why I want to use relative path.
Here is my
test521.pro
TEMPLATE = app
QT += qml quick
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)
Here is
my main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
return app.exec();
}
How to set relative path instead of absolute?
Edit: Solution for QML: how to specify image file path relative to application folder doesn't work for me.

Related

QML/WebGL - How to stop DefaultFileDialog from returning unusable paths on Windows?

Problem description
When building a QML application on Windows for the desktop that contains a FileDialog, the default Windows file dialog is shown. This one works as expected, returning a path to the selected file or folder that has a prefix (file:///) before the path. Only once WebGL comes into play, the problem appears.
Building for the WebGL platform and connecting via browser will default the file dialog to DefaultFileDialog, which behaves drastically different upon selection of (sub-)directories. Lets say we start out with this path in the dialog (clipped to focus on the ROI):
After selecting a subdirectory (single click):
Another click enters that directory and replaces the path with:
If another selection is taken in the subdirectory, we finally lose the drive letter:
The path is something that I expect to work on Linux which might be the reason why I could not find any additional information on this behavior. Additional steps into subdirectories do not break the path further:
My search so far
I have google'd it, searched Stack Overflow and tried to regex my way through this problem, but so far, no luck.
Question
Is there a way to stop DefaultFileDialog instances from optimizing my path selections into uselessness on Windows? Ideally, I'd like to keep the system file dialog when not running the application for WebGL, but this is no hard requirement for me.
EDIT: #mike510a revealed other requirements that I missed:
The drive letter may be any valid one, as within the dialog, the drive can be changed. Therefore hardcoding a drive letter to replace the one lost within the dialog does not work.
Due to the unknown number of steps taken before accepting a file or folder, it cannot be assumed that the drive letter was already cut from the returned QString.
Sample code
Modify a project created by QtCreator (4.13.2 in my case, empty QtQuick project).
# autogenerated
QT += quick
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
app.setOrganizationName("Something"); // modified to avoid QML warning
app.setOrganizationDomain("Something.else"); // modified to avoid QML warning
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();
}
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Dialogs 1.2
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.3
Window {
width: 640
height: 480
minimumHeight: 320
minimumWidth: 480
visible: true
title: qsTr("Hello World")
color: "grey"
RowLayout {
anchors.fill: parent
Label {
text: "File path:"
Layout.column: 0
}
TextEdit {
id: pathTextEdit
readOnly: true
clip: true
Layout.column: 1
Layout.fillWidth: true
Rectangle {
z: -1
anchors.fill: parent
radius: 3
color: "white"
}
}
Button {
text: "Select file"
Layout.column: 2
onClicked: {
pathSelection.visible = true
}
}
}
FileDialog {
id: pathSelection
visible: false
width: 300
height: 300
selectFolder: true
onAccepted: {
pathTextEdit.text = folder
}
}
}
I'm pretty sure that because the WebGL platform uses a different schema than the Windows filesystem directory structure, you will get weird folders. You can use the schema file:// to translate URLS into pointers to files on the local machine, You can use the folder property along with file:///C:/Users/whatever or do the following...
To grab the operating System's underlying filesystem's locations, you can use the folder property with the value shortcuts.home as stated here: https://doc.qt.io/qt-5/qml-qtquick-dialogs-filedialog.html
FileDialog {
id: pathSelection
visible: false
width: 300
height: 300
selectFolder: true
// point to an operating system location and you should get
// a usable URL
folder: shortcuts.home
onAccepted: {
pathTextEdit.text = folder
}
}
With the help of Qt Support, a solution could be found. The issue responsible for the behavior is found in DefaultFileDialog.qml, more specifically, the function dirDown. It is fixable by replacing
root.folder = "file://" + path
with
root.folder = root.pathToUrl(path)
and rebuilding Qt afterwards. The entire patch reads like this:
--- a/src/dialogs/DefaultFileDialog.qml
+++ b/src/dialogs/DefaultFileDialog.qml
## -115,7 +115,7 ## AbstractFileDialog {
function dirDown(path) {
view.selection.clear()
- root.folder = "file://" + path
+ root.folder = root.pathToUrl(path)
}
function dirUp() {
view.selection.clear()
This patch was submitted for inclusion with Qt 5.15.3.

Rectangle as a root element in QML

I use Qt 5.5.0 MSVC 2013, 32bit.
I want to create minimal QtQuick application. When I choose New Project - Qt Quick Application I got project with 2 QML files: main.qml and MainForm.ui.qml. Since I do not need them, I delete second one and paste following to main.qml:
Import QtQuick 2.4
Rectangle{
id: root
visible: true
color: "gray"
width: 400
height: 800
}
But when I run project I got nothing. I see application in Task Manager but there is no application window.
Question: Is it possible to create .qml file with Rectangle as a root element?
Solution was found at official Qt Forum.
The template for creating Qt Quick Application adds QQmlApplicationEngine to launch the QML. But QQmlApplicationEngine dont work directly with Rectangle or Item as root element but requires any window like Window or ApplicationWindow.
So to make it work for Rectangle use QQuickView instead of QQmlApplicationEngine.
I changed content of main.cpp to
#include <QGuiApplication>
#include <QQuickView>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView *view = new QQuickView;
view->setSource(QUrl("qrc:/main.qml"));
view->show();
return app.exec();
}
and it solved my problem.

Why does a TextArea with NoWrap always cause an "anchor loop detected" warning?

Why is a TextArea with
wrapMode: TextEdit.NoWrap
always causing
file:///C:/Qt/5.5/mingw492_32/qml/QtQuick/Controls/ScrollView.qml:340:13: QML Item: Possible anchor loop detected on fill.
when I run it?
I am running Qt 5.5 on a 64-bit Windows 7 machine, and compiling with MinGW.
Here is my QML code test.qml:
import QtQuick 2.4
import QtQuick.Controls 1.3
ApplicationWindow {
title: "test window"
width: 500
height: 500
visible: true
TextArea {
wrapMode: TextEdit.NoWrap
}
}
Here is my C++ code main.c:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/test.qml")));
return app.exec();
}
Even if I add anchors.fill: parent to the TextArea, I still get the warnings.
As a second part of this question, is this warning something I should be worried about, or is it something I can safely ignore?
I think it's a bug from Qt, you can ignore it.
When created, TextArea have a width != 0, even if it's empty. When you enter a text that have an implicitWidth smaller then the (default) width of the TextArea, you will get this warning.
A workaround is to assign the wrapMode property in the Component.onCompleted handler:
Component.onCompleted: wrapMode = TextEdit.NoWrap

QML Window opacity doesn't work when fullscreen

I've experienced a strange problem: when a QML Window is fullscreen, its opacity property doesn't work, so the window stay opaque. When the window isn't fullscreen (e.g. maximized), it works properly.
Do you have any ideas how to deal with this problem?
In fact, I want to animate fullscreen window fading in.
The code:
main.qml
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.1
Window {
visible: true
visibility: "FullScreen"
opacity: 0.5
Text {
id: text
text: "Hello World"
font.pointSize: 36
color: "#333"
}
}
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
I use Qt 5.3 on Windows 8.1.
This is the age-old bug of the Qt/Win combination - windows with an OpenGL context, can't be made transparent without employing trickery. The solution is to embed your QML application in a QQuickWidget and make that transparent and full-screen. There also exists another workaround (using the 'DWM' API, which is nonportable - you can read about it in the bug description).
https://bugreports.qt.io/browse/QTBUG-28214

QML: not able to see text on TextInput element of QML

I have created qml/test.qml file as:
import QtQuick 1.0
Rectangle
{
id: pahe
width: 200; height: 50
color: "#55aaee"
TextInput
{
id: editor
anchors
{
left: parent.left; right: parent.right; leftMargin: 10; rightMargin: 10
verticalCenter: parent.verticalCenter
}
cursorVisible: true;
font.bold: true
color: "#151515";
selectionColor: "Green"
focus: true
}
}
and One qml/main.cpp file as:
#include <QApplication>
#include <QtDeclarative>
#include <QDeclarativeView>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("test.qml"));
view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
view.show();
return app.exec();
}
I am compiling this main.cpp file using commands like:
#qmake -project
#qmake
#make
and I am running the exe as:
./qml
So problem is that I am not able to see any text on TextInput even after entering text using key board. If i print the TextInput.text of element it shows entered text on console log but can not see on screen.
What could be the reason?
If i run same test.qml file using qmlviewer it works fine.
Any hint or comment in this would be helpful.
Thanks,
KBalar
why don't you try simulator? U said, it as an exe and why r u running it in a Linux style "./". If you still have the problem, check the background of the Rectangle or TextEdit and the font color.
The problem was with virtual Linux machine running on Windows PC. So If i run same example on Real Linux machine The problem wont be there.

Resources