SystemTrayIcon in QML - qt

I try to use the SystemTrayIcon in QML using Qt 5.11 (Manjaro with KDE), but when i add
import Qt.labs.platform 1.1
to the QML Code (or 1.0) it doesn't show anything but instantly fails with
/path/to/project exited with code 255
Code example for empty quick project:
import QtQuick 2.11
import QtQuick.Window 2.11
import Qt.labs.platform 1.1
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
}

Problem was conflicting namespaces. Using the import like
import Qt.labs.platform 1.1 as LabsPlatform
LabsPlatform.SystemTrayIcon {
}
works just fine.
In Addition I had to include widgets and use QApplication instead of QGuiApplication.

Related

QML FileDialog (selectFolder)

I am doing an App in QML / QT / C++ (to train myself for an internship). I need to open a FileDialog in order to choose a folder location but I get an error when writing the line selectFolder: true.
I found this property here https://doc.qt.io/qt-5/qml-qtquick-dialogs-filedialog.html#selectFolder-prop
It's weird because I can't set selectExisting and selectMultiple either.
The documentation said we have to specify these before opening the Dialog. I did this, I don't understand.
Can anyone help me fix this error ?
Here is what I import in main.qml: import QtQuick.Dialogs 1.3
Thank you very much. Have a good day.
Here is the code:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Dialogs 1.3
Window {
id: mainWindow
visible: true
width: 700
height: 500
// FileDialog
FileDialog {
id: fileDialog
title: "Please choose a file"
folder: shortcuts.home
selectFolder: true
onAccepted: {
console.log("You chose: " + fileDialog.fileUrls)
//acceptDialog();
}
onRejected: {
console.log("rejected")
//rejectDialog();
}
Component.onCompleted: visible = true
}
}
It's not the entire code (+ 200 lines)
Image from Qt Creator
For me, the code worked fine.
Just try to delete and then put the line again.
Sometimes qml gives you these kinds of errors for no reason. Just try to run it. If there are some errors, post them
I resolved the problem.
My Qt Project version was 5.12, I created a new project with Qt 5.14 and it worked.
Thanks.

QML Dialog positioning in ApplicationWindow

I am finding it impossible to position a Dialog central to my ApplicationWindow in QT 5.12
import QtQuick 2.7
import QtQuick.Controls 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
id:mainApplicationWindow
visible: true
height: 500
width: 500
Item {
anchors.centerIn: parent
MainWindowMessageDialog{
id: messageDialog
}
}
Component.onCompleted: {
messageDialog.open()
}
}
With MainWindowMessageDialog.qml
import QtQuick 2.0
import QtQuick.Dialogs 1.2
Dialog {
title: "There seems to be a problem"
standardButtons: StandardButton.Ok
onAccepted: {
this.close()
}
}
Gives me the image below. I've tried adding a fixed z position but nothing seems to shift the Dialog downwards into the window. I've tried MainWindowMessageDialog on its own outside of an Item. Nothing seems to shift it? Am I missing something?
This turned out to be an issue of modality.
https://bugreports.qt.io/browse/QTBUG-82737?jql=text%20~%20%22MessageDialog%22
Adding
modality: Qt.ApplicationModal
Did the trick

QML Button exclusiveGroup property?

I'm unable to assign ExclusiveGroup to my checkable buttons.
ExclusiveGroup {
id: group
onCurrentChanged: {
if(current != null) {
console.info("button checked...no!")
current = null
//current.checked = false <--- also this
}
}
}
Column {
width: parent.width
spacing: 0
Button {
id: btnScan
flat: true
text: qsTr("But1")
width: parent.width
checkable: true
exclusiveGroup: group
}
Button {
id: btnWhiteList
flat: true
text: qsTr("But2")
width: parent.width
checkable: true
exclusiveGroup: group
}
}
The documentation states clearly that Button does have exclusiveGroup property http://doc.qt.io/qt-5/qml-qtquick-controls-button.html#exclusiveGroup-prop. However, when I run the example, I get this error:
qrc:/main.qml:48 Cannot assign to non-existent property "exclusiveGroup"
Hovering mouse over "exclusiveGroup" makes a tooltip show up saying: "Invalid property name exclusiveGroup".
I have Qt 5.9.1 installed. Here's my import statements:
import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2
What am I doing wrong?
Thanks
The reason for your problem is this:
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
You have a Button in both, but they have a different API.
So at first you import the Button from QtQuick.Controls 1.4. Then you import the Button from QtQuick.Controls 2.0. As QML doesn't know, which one you want to use, it will take the one, you imported the last. If you want to be more specific, on which Button you want to use, you can use named imports
import QtQuick.Controls 1.4 as OldCtrl
import QtQuick.Controls 2.0 as NewCtrl
Then you can use Buttons from both versions as you like:
OldCtrl.Button { // from the QtQuick.Controls 1.4 }
NewCtrl.Button { // from the QtQuick.Controls 2.0 }
The documentation you quote is for QtQuick.Controls 1.x, and from there is the imported ExclusiveGroup. So you are trying to mix things from two libraries, that wont work together.
See ButtonGroup for a similar solution for QtQuick.Controls 2.x
For more on the differences and usecases of the both sets of controls read:
http://blog.qt.io/blog/2016/06/10/qt-quick-controls-2-0-a-new-beginning/
https://doc.qt.io/qt-5/qtquickcontrols2-differences.html

Cannot find Qt QML SpinBox minimumValue using QtQuick.Controls 2.1

I got this piece of code:
import QtQuick 2.7
import QtQuick.Controls 1.5
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("spinbox trouble")
SpinBox{
anchors.centerIn: parent
value: 5
minimumValue: 1
maximumValue: 10
}
}
Everything is fine. Now I want to use SwipeView. To use it I need use QtQuick.Controls 2.1. But when I use version 2.1, I cannot find [minimum/maximum]Value in SpinBox.
Questions:
1. What is reason to drop this properties in QtQuick.Controls 2.1?
2. Is there any way to use both SwipeView and SpinBox with [minimum/maximum]Value property?
Looking at the Qt doc I'd say you can use the from and to properties instead of minimumValue and maximumValue!?

Resolve resource ambiguity in QML imports

I need to use both QtLabs and QtQuickControls. Both have the Button type but I need to use the one in QuickControls. The QML file is picking the button in labs. How do I force it to use the one in QuickControls?
import QtQuick 2.6
import QtQuick.Controls 1.5 //This is what I need the QML file to pick button from
import QtQuick.Controls.Styles 1.4
import QtGraphicalEffects 1.0
import QtQuick.Dialogs 1.2
import QtMultimedia 5.6
import Qt.labs.controls 1.0 //This is where it is picking Button from
A fast/easy way to solve the issue is to make a named import with the as keyword. After you give a name to the import all the components in the module can be accessed through that name.
Example with your imports:
import QtQuick 2.6
import QtQuick.Controls 1.5 as Ctrl1 //name for old controls
import QtQuick.Controls.Styles 1.4
import QtGraphicalEffects 1.0
import QtQuick.Dialogs 1.2
import QtMultimedia 5.6
import Qt.labs.controls 1.0 as Ctrl2 //name for new controls
Ctrl2.ApplicationWindow {
id: root
visible: true
width: 400
height: 300
Column {
anchors.fill: parent
Ctrl1.Button {
text: qsTr("one")
}
Ctrl2.Button {
text: qsTr("two")
}
}
}
This approach can easily become too verbose. In that case I would separate the content in different files, physically separating the offending imports.

Resources