Resolve resource ambiguity in QML imports - qt

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.

Related

SystemTrayIcon in QML

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.

QML - Cannot assign to non-existent property "style"

I'm using Qt 5.10.1 with Qt Creator 4.5.1 and the style property is never available in elements.
For example, as shown here ButtonStyle QML Type , I would like to do:
Button {
text: "A button"
style: ButtonStyle {...}
}
But, I get the error:
Cannot assign to non-existent property "style"
I tried with a rectangle, progressbar and I get the same error.
Edit #1:
I do have all these imports. If the import was missing, I would get the error on ButtonStyle , but the error is on style.
import QtQuick 2.2
import QtQuick.Controls 2.3
import QtQuick.Dialogs 1.0
import QtGraphicalEffects 1.0
import QtQuick.Shapes 1.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Styles 1.4
There are 2 types of Buttons in QML:
Button Qt Quick Controls 2: https://doc.qt.io/qt-5.10/qml-qtquick-controls2-button.html
Button Qt Quick Controls: http://doc.qt.io/qt-5/qml-qtquick-controls-button.html
In your case, you are importing the Qt QuickControls 2 Button: import QtQuick.Controls 2.3, and that Button does not have the style attribute.
If you need to use the style you must import:
import QtQuick.Controls 1.4
instead of:
import QtQuick.Controls 2.3
If you are using items from Qt Quick Controls and Qt Quick Controls 2 you could separate them using a namespace:
import QtQuick.Controls 2.3 as QQC2
import QtQuick.Controls 1.4 as QQC1
QQC1.Button {
text: "A button"
style: ButtonStyle {...}
}
QQC2.another_item_of_Qt_Quick_Controls2{
}
You can customize Qt Quick Controls 2 button by modifying its two visual items of background and content item:
https://doc.qt.io/qt-5/qtquickcontrols2-customize.html#customizing-button
import QtQuick 2.12
import QtQuick.Controls 2.12
Button {
id: control
text: qsTr("Button")
contentItem: Text {
text: control.text
font: control.font
opacity: enabled ? 1.0 : 0.3
color: control.down ? "#17a81a" : "#21be2b"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
}
background: Rectangle {
implicitWidth: 100
implicitHeight: 40
opacity: enabled ? 1 : 0.3
border.color: control.down ? "#17a81a" : "#21be2b"
border.width: 1
radius: 2
}
}
Make sure importing QtQuick.Controls.Styles
import QtQuick.Controls.Styles 1.4
Button {
text: "A button"
style: ButtonStyle {...}
}

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

QT Quick Control 2 Combobox cannot select Text

This code blocks work fine with import QtQuick.Controls 1.4
import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
Rectangle {
property bool isval: true
objectName: "havadurumuCombo"
anchors.fill: parent
ComboBox {
id:havadurumuCombo
model:combohava.datalist
textRole: "value"
width: parent.width
height: parent.height
}
I have changed QtQuick.Controls 1.4 to import QtQuick.Controls 2.0 . I have experienced strance behavior. I choose combobox then drop down with information in model.That is OK. But I cannot choose any text on combobox.If ı select with curson on dropdown menu, then There is no text appears on it after closing dropdown.

How to change "always on top" Window behavior dynamically in QML

How can I change the behavior by e.g. toggling a Menu item? I tried changing/setting flags:
import QtQuick 2.1
import QtWebEngine 1.1
import QtQuick.Controls 1.0
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.0
import QtQuick.Window 2.1
import QtQuick.Controls.Private 1.0
import QtQuick.Dialogs 1.2
import Qt.labs.settings 1.0
//(...)
property alias isOnTop: onTop.checked
flags: (isOnTop) ? (Qt.Window | Qt.WindowStaysOnTopHint) : (Qt.Window ) // not working
Flags are set properly but won't change when onTop.checked changes.

Resources