Let's consider this code snippet:
ApplicationWindow
{
/**/
states: State {}
/**/
}
When running the application, I get
Cannot assign to non-existent property "states"
When using
ApplicationWindow
{
/**/
Item { states: State {} }
/**/
}
There is no error. Why can't I use states inside an ApplicationWindow?
I've just realized that ApplicationWindow inherits Window inherits QQuickWindow. Only types which inherit from Item have the states property.
The workaround is to use StateGroup.
Feel free to add a better solution ;)
I was able to code States within an QML ApplicationWindow using Qt v5.11, with two caveats. One was that the Qt Creator v4.6.1 editor flagged SignalTransition with a warning indicating a State cannot have a child item. Despite that warning, the code from the example at http://doc.qt.io/qt-5/qmlstatemachine.html#a-simple-state-machine builds and runs correctly. The second caveat was that the Qt Creator editor ignored the major.minor version of import QtQml.StateMachine. Any numbers I typed in instead of 1.11 were accepted:
import QtQuick 2.11
import QtQuick.Controls 1.4
import QtQml.StateMachine 1.11
ApplicationWindow {
...
Related
I'm using systemPalette (Qt5.15) in several qml-based applications in Linux, I need one of these applications to change systemPalette of qml system-wide (globally) so that other applications can detect this change at runtime.
what I need is similar to what happens in KDE, for example, I have the following qml code:
import QtQuick 2.15
import QtQuick.Window 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
SystemPalette {
id: palette
colorGroup: SystemPalette.Active
}
Rectangle{
anchors.centerIn: parent
width: 200
height: width
color: palette.base
}
}
when I run the above code in my KDE plasma, if I change the system's theme from breeze to breeze-dark, the rectangle's color will change at runtime, I can even select a custom color in
Appearance->globalTheme->colors.
In my case, regardless of Linux's distro, my application needs to change systemPalette globally and set its custom values for colors, how can I do that? where does qt read those color values from? (for systemPallete)
Thanks.
P.S: I can create my own style/plugin for qt and set different colors in my custom style, but still I have no idea how to change OS style globally so that all applications can detect this at runtime.
I want to set the Style for my elements in qml. For that, I want to use a style like Material Style. Using the example which can be found under:
https://doc.qt.io/qt-5/qtquickcontrols2-material.html
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.12
ApplicationWindow {
visible: true
Material.theme: Material.Dark
Material.accent: Material.Purple
Column {
anchors.centerIn: parent
RadioButton { text: qsTr("Small") }
RadioButton { text: qsTr("Medium"); checked: true }
RadioButton { text: qsTr("Large") }
}
}
Gives me the result seen in the image I attached.
No matter which Style I use, nothing changes.
I am currently using the newest free Qt version under a Windows 10 Os.
Can anyone help me?
And is it possible to globally overwrite a Style and make an own Style, simply in QML.
As the docs points out:
To run an application with the Material style, see Using Styles in Qt Quick Controls.
There are several ways to set the style in Qt Quick Controls 2:
Using QQuickStyle in C++:
add QT += quickcontrols2 in your .pro and use #include <QQuickStyle> and QQuickStyle::setStyle("Material"); in main.cpp
Command line argument:
You can run from the console/CMD by adding the argument: ./your_executable -style material.
If you use Qt Creator you can go to Projects-> Build & Run-> Run and in Command line arguments add: -style material.
Environment variable:
You can run from the console/CMD: QT_QUICK_CONTROLS_STYLE=material ./your_executable
If you are using Qt Creator you can add it in the section Projects-> Build & Run-> Run-> Run Environment.
or add qputenv("QT_QUICK_CONTROLS_STYLE", "material"); in main.cpp.
Configuration file:
The qtquickcontrols2.conf file must be created:
[Controls]
Style=Material
and must be in a qresource:
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>qtquickcontrols2.conf</file>
</qresource>
</RCC>
You have to set the style from C++ as well. See this Qt documentation.
So in you main add QQuickStyle::setStyle("Material");
when exiting application developed under Qt, I am getting following error:
file:///C:/Qt/5.10.0/mingw53_32/qml/QtQuick/Controls/Private/Control.qml:90:
ReferenceError: parent is not defined
from component defined as simply as this:
import QtQuick 2.8
import QtGraphicalEffects 1.0
import QtQuick.Controls.Styles 1.4
import QtQuick.Extras 1.4
Item {
id:root
CircularGauge {
anchors.centerIn: root
}
}
If line
anchors.centerIn: root
is removed, then there's no error. I am pretty sure that error is produced whenever CircularGauge somehow references parent, although setting
parent:root
in gauge does not help. Any idea what's causing that?
Set anchors.centerIn: parent instead if anchors.centerIn: root.
I was using Desktop Qt 5.10.0 MinGW 32bit. When changed to v5.8, application exits without error and this is enough for me.
I start on Qt Quick and I want to develop an application that will be deployed on an embedded system. I'm using Qt Quick Controls 2. I'm trying to create two toolbars in my header: a toolbar consisting of icons (battery level, network connection, ...) and a toolbar allowing to navigate in a StackView (as in the example Gallery provided by Qt). I would like to have two different background colors for each toolbarenter image description here. For now I have a header that have a toolbar and do the levels get organized with a ColumnLayout and two RowLayout.
How could I go about it?
You can use the Material.primary attached property to specify the Material style ToolBar color. For example:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
ApplicationWindow {
header: Column {
ToolBar {
Material.primary: Material.Red
}
ToolBar {
Material.primary: Material.Blue
}
}
}
SpinBoxStyle from QtQuick.Controls.Styles allows you to change the appearance of a SpinBox, and a part of that is the ability to redesign the up/down arrow buttons. However neither SpinBox nor the style gives you the ability to query the up/down arrow button state, so you can't check if it is pressed or hovered over.
This seems like too much of an oversight, so what part of the API docs have I missed?
I've tried adding a MouseArea to the control delegate itself, but some reason it never receives any events - the controls still work though which suggests that they are 'stealing' the events first.
SpinBox {
style: SpinBoxStyle {
incrementControl: Rectangle {
implicitHeight: 10
implicitWidth: 10
color: "blue"
MouseArea {
anchors.fill: parent
hoverEnabled: true
onEntered: console.log( "Hello" ) // Never printed
}
}
}
}
Apparently you're supposed to use the styleData properties to detect hovered and pressed states, but they aren't documented. Please create a bug report for that.
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
SpinBox {
style: SpinBoxStyle {
incrementControl: Rectangle {
implicitHeight: 10
implicitWidth: 10
color: styleData.upHovered && !styleData.upPressed
? Qt.lighter("blue") : (styleData.upPressed ? Qt.darker("blue") : "blue")
}
}
}
I'm not sure why the style was implemented this way, but if you look further into the source code, you can see that there are always MouseAreas for the up and down controls. This is very confusing to me; if you're not supposed to provide an interactive control because there will always be MouseAreas shadowing them, why call it incrementControl and decrementControl? Names like increment and decrement might suffice, given that they're not able to receive almost any interaction (clicking works at least, for some reason). If you find this a bit confusing, you may also want to file a separate bug report for the API.
git log --follow -p shows that this code hasn't changed much since the introduction of styles, so I'd say the current implementation (and API) is just outdated, and hopefully there are opportunities for improving this in the future.