I have a component which displays a custom combo box:
Column {
anchors.top: parent.top
anchors.topMargin: 200
anchors.left: parent.left
anchors.leftMargin: 30
Rectangle {
id: rectangle1
signal comboClicked
Column {
id: column_button
MenuButtonStyle {
id: buttonCombo
style: MyComboBoxButtonStyle {
label: ButtonTextStyle {
text: "Choose"
}
}
}
}
Column {
id: column1
anchors.left: column_button.right
anchors.leftMargin: -25
anchors.top: parent.top
anchors.topMargin: 3
Image {
id: image1
width: 16
source: "arrow_down.png"
}
}
Keys.onReturnPressed: {
rectangle1.state = rectangle1.state==="dropDown"?"":"dropDown"
console.log("Open Drop Down..")
}
states: State {
name: "dropDown";
PropertyChanges { target: buttonCombo; style: MyButtonStyle }
}
}
}
The goal is to change the style property in "MenuButtonStyle" module.
I tried it with the line
PropertyChanges { target: buttonCombo; style: MyButtonStyle }
but it gives me the error:
file:///D:/projekte/qt_quick/FirstTest/MainPane.qml:82: ReferenceError: MyButtonStyle is not defined
If I replace the "MyComboBoxButtonStyle" directly with "MyButtonStyle" QT did not complain over a not defined reference.
What is the problem? Is it not possible to change the style of a component like it is possible with CSS in HTML?
I am not sure how your MyButtonStyle is written, but you can instantiate MyButtonStyle in your root component and use it throughout the application:
MyButtonStyle { id: myButtonStyle }
or you can define it as a singleton object.
Reference: http://qt-project.org/wiki/QmlStyling
Related
In a Qml file ,the code like:
StackView {
id: stackView
anchors.right: parent.right
width: parent.width/2-20
initialItem:patientdetail
Component{
id:patientdetail
Column {
id:detailcolumn
spacing: 2
anchors.right: parent.right
width: parent.width/2-20
Label {
id: label
color: "#ffffff"
text: qsTr("User ID")
}
TextField {
id: textField_id
readOnly: true
placeholderText: qsTr("")
}
}
}
Component{
id:component2
//...other component will add to stackview
}
}
And I want to change the text of TextField by a JS function(in same file) like:
function updatedetail(clear,rowindex){
if(clear){
textField_id.text="";
}
else{
textField_id.text=jsonModel1.model.get(rowindex).id;
}
}
But there is a error:
ReferenceError: textField_id is not defined
Where the error occurred?
As you try to change an Object that is not yet instantiated, it will fail. But even when it is instantiated, its id will be in a different scope, that can't be reached like that.
This is necessary, as the same Component might be instantiated multiple times (e.g. as a delegate in a ListView) so it would not be unique in the context anymore.
Uppon instantiation of your StackView, your Component will be instantiated an pushed on the StackView. Now you have an instance and might alter exposed properties by using:
currentItem.textFieldText = newValue
in your function. For this to work you need to expose the property:
Component{
id:patientdetail
Column {
id:detailcolumn
spacing: 2
anchors.right: parent.right
width: parent.width/2-20
property alias textFieldText: textField_id.text // in this context, you have access to the id. Expose it, to change it from the outside.
Label {
id: label
color: "#ffffff"
text: qsTr("User ID")
}
TextField {
id: textField_id
readOnly: true
placeholderText: qsTr("")
}
}
}
However, as the instance might be destroyed and recreated later, this change would not be permanent, so it would be better to bind the TextField.text to a property of an object, that will survive as long as necessary. This could be a contextProperty exposed from C++ or a QtObject passed as a model, or just a property, e.g. in the StackView.
StackView {
id: stackView
anchors.right: parent.right
width: parent.width/2-20
initialItem:patientdetail
// Change this property. The textField_id.text will be bound to it.
property string textFieldText
Component{
id:patientdetail
Column {
id:detailcolumn
spacing: 2
anchors.right: parent.right
width: parent.width/2-20
Label {
id: label
color: "#ffffff"
text: qsTr("User ID")
}
TextField {
id: textField_id
readOnly: true
placeholderText: qsTr("")
text: stackView.textFieldText
}
}
}
}
I am trying to communicate between 2 QML pages.
In my page Main.qml I receive a signal from my C++ code. On receiving this signal I want text on InputPage.qml to change. This page is shown within Main.qml using a Loader. The only way I could find so far is to set up another signal between the 2 pages. However, I think there is a much easier way to do this. I already tried this way but I could not get it to work. So before I proceed I would like to know if this is the right method or not.
Any ideas on how to do this, and if the method described above is the correct one?
My code:
Main.qml
Item {
id: screen_InputPage
width: 1920
height: 930
anchors.left: parent.left
anchors.leftMargin: 0
anchors.top: parent.top
anchors.topMargin: 100
visible: false
opacity: 1
Loader {//Loads the pages
id: pageLoader_ID2
source: "inputPage.qml"
}
}
And i would like to access the text(and maybe functions) placed on inputPage.qml
Text {
id: text_volume_perc_ID1
height: 48
text: qsTr("50")
anchors.right: parent.right
anchors.rightMargin: 0
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
anchors.top: parent.top
anchors.topMargin: 126
anchors.left: parent.left
anchors.leftMargin: 0
font.pixelSize: 42
}
To access the created object, you can use
idLoader.item.idInputpage.idText.text
I propose that you load objects dynamically to improve performance. To do so you can create your own CustomLoader.qml:
CustomLoader.qml
import QtQuick 2.0
Item {
id: idRoot
width: childrenRect.width
height: childrenRect.height
property Item createdObject
property string source
function fnSourceChange() {
if (""!== source){
var component
// create component
component = Qt.createComponent(source)
if (Component.Ready === component.status) {
createdObject= component.createObject(idRoot)
if(!createdObject)
console.log("Loader::Could not create the object ")
}
else {
console.log("Loader::Could not create panel", component.errorString(), "component has errors")
}
}
else {
createdObject.destroy();
createdObject = null
// unComment this line if you want to force the garbage collector
//gc()
}
}
onSourceChanged: {
fnSourceChange()
}
// even without that it should detect the source change and create it
// you can unComment this line if you want, but like that it will parse the function
// two times one on the sourceChanged signal and on on in this handler
// print the source or somthing in the function and you'll see
// Component.onCompleted: fnSourceChange()
}
main.qml
import QtQuick 2.0
Item {
id: screen_InputPage
width: 1920
height: 930
anchors.left: parent.left
anchors.leftMargin: 0
anchors.top: parent.top
anchors.topMargin: 100
visible: false
opacity: 1
CustomLoader{
id: pageLoader_ID2
source: "inputPage.qml"
}
}
InputPage.qml
import QtQuick 2.0
Item {
width: 800
height: 480
property alias text: idText.text
property alias label: idText
property alias rect: idRect
Text{
id: idText
}
Rectangle{
id: idRect
width: 100
height: 200
}
}
In your main add :
//or another scope like click button
Component.onCompleted: {
pageLoader_ID2.createdObject.text = "hello"
pageLoader_ID2.createdObject.rect.color = "red"
}
In my application i show a nested list, that shows groups and folders as its children. I have built the functions necesary to generate a new list in the backend in c++ based on which item is clicked by the user.
I allready have the necesary functionality to pass the list to qml through QProperty.
so my question is, how do i previous listviews and show new ones dynamically. Considering it should also be possible to click the button "back", which should load the previous page again showing the groups and the folders.
this is the code i have now, showing the groups and its children(folders)
import QtQuick 2.4
import QtQuick.Window 2.2
//import ListMode 1.0
Rectangle {
height: 250
width: 140
color: "pink"
//property var aNum: 0
Component {
id: folderDelegate
Item {
width: 140
height: col2.childrenRect.height
Column {
id: col2
anchors.left: parent.left
anchors.right: parent.right
Rectangle {
height: 20
width: parent.width
border.color: "black"
MouseArea {
anchors.fill: parent
onClicked: treemodel.getObject(model.ID + ":" + model.Name)
}
Text {
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
id: name1
text: model.Name
}
}
}
}
}
ListView {
id: outer
model: myModel
delegate: groupsDelegate
anchors.fill: parent
}
Component {
id: groupsDelegate
Item {
width: 140
height: col.childrenRect.height
Column {
id: col
anchors.left: parent.left
anchors.right: parent.right
Text {
anchors.horizontalCenter: parent.horizontalCenter
id: t1
font.bold: true
font.underline: true
font.pointSize: 9
text: model.Name
}
ListView {
id: folderlist
model: treemodel.lists[treemodel.modIndex]
delegate: folderDelegate
contentHeight: contentItem.childrenRect.height
height: childrenRect.height
anchors.left: parent.left
anchors.right: parent.right
clip: true
}
}
}
}
}
i have been reading documentations and searching forums, but the information is pretty overwhelming. So a pointer in the right direction would be appreciated.
the main model is setup for each item to have its own unique ID. So when an item is clicked, i run a function that grabs and stores the item based on the ID + name that was clicked
MouseArea {
anchors.fill: parent
onClicked :{
treemodel.getObject(model.ID + ":" + model.Name)
stackView.push(Qt.resolvedUrl("content/ButtonPage.qml"))
}
}
next, based on the item that was clicked i have functions that fill different QList items which are loaded into the ButtonPage.qml.
the function in c++ that is invoked is:
Q_INVOKABLE void getObject(QString index) {
clickedItemID = index;
getClickedItem();
getFilesByFolder();
}
now, i am not sure if this is a good solution. But for me it works. Maybe it will work for someone else too.
I have an Qml component with a SequentialAnimation containing a static sequence of PropertyAction components:
SequentialAnimation {
id: anim
running: true
PropertyAction { target: icon; property: "iconid"; value: propStore.anim1 }
PropertyAction { target: icon; property: "iconid"; value: propStore.anim2 }
PropertyAction { target: icon; property: "iconid"; value: propStore.anim3 }
}
This accomplishes that a specific icon is animated. However now I'd like to make it a little bit dynamic by building the sequence dynamically. The reason is that the propStore isn't under my control, and users adding new images to the animation sequence require me to make changes to the Qml :(
How should I go about doing this?
My first thought was to dynamically add components to anim.animations, but that doesn't work (it seems to be a read-only property of SequentialAnimation.)
My next thought was to add a ListModel to the outer component, and in its Component.onCompleted slot I append objects of the shape { image: "animN" } (I get the "animN" strings using introspection on propStore.) Then I use the ListModel to populate a Repeater. However, the SequentialAnimation doesn't seem to accept a Repeater object.
You can't append directly to anim.animations, but you can reaffect it to a new value. Build a JS array from anim.animation, append a dynamically created animation to it, then reaffect it to anim.animations.
Here's a simple example.
Component
{
id: component
SequentialAnimation
{
id: seq
property string color
PropertyAction {target: rect; property: "color"; value: seq.color}
PauseAnimation { duration: 500 }
}
}
function addAnim(color)
{
var listAnim = []
for(var i=0; i<anim.animations.length; i++)
listAnim.push(anim.animations[i])
var temp = component.createObject(root, {"color":color})
listAnim.push(temp)
anim.animations = listAnim
}
Rectangle
{
id: rect
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: row.top
anchors.margins: 40
border.width: 1
SequentialAnimation
{
id: anim
}
}
Row
{
id: row
anchors.bottom: parent.bottom
anchors.bottomMargin: 50
anchors.horizontalCenter: parent.horizontalCenter
spacing: 10
Button {
text: qsTr("Play")
onClicked: anim.start()
}
Repeater
{
model: ["red", "green", "blue", "cyan", "magenta", "yellow"]
Button {
text: qsTr("Add %1").arg(modelData[0])
onClicked: addAnim(modelData)
}
}
}
I'm trying to make custom component for editable tables in QML, like this:
// BaseTableView.qml
import QtQuick 2.3
import QtQuick.Controls 1.2
Item {
signal addActionPerformed()
signal editActionPerformed(int id)
signal deleteActionPerformed(int id)
property var model
ToolBar {
id: toolBar
anchors.left: parent.left
anchors.right: parent.right
Row {
ToolButton {
id: addButton
iconSource: "qrc:/icons/actions/add.png"
onClicked: addActionPerformed()
}
ToolButton {
id: editButton
enabled: false
iconSource: "qrc:/icons/actions/edit.png"
}
ToolButton {
id: deleteButton
enabled: false
iconSource: "qrc:/icons/actions/delete.png"
}
}
}
TableView {
id: tableView
model: parent.model
anchors.left: parent.left
anchors.right: parent.right
anchors.top: toolBar.bottom
anchors.bottom: parent.bottom
onCurrentRowChanged: {
editButton.enabled = currentRow !== null
deleteButton.enabled = currentRow !== null
}
}
}
and use this component in another file like this:
// Another.qml file
import QtQuick 2.3
import QtQuick.Controls 1.2
import "../common" // Here is BaseTableView.qml
BaseTableView {
TableViewColumn {
role: "id"
title: qsTr("Id")
}
TableViewColumn {
role: "object_expression"
title: qsTr("Expression")
}
}
So, problem is how i can pass table view columns from usage to underlying TableView?
I've tried to make property list in BaseTableView and assign a list of objects to this property in Aother.qml? but unsuccessfully.
Use default properties:
An object definition can have a single default property. A default property is the property to which a value is assigned if an object is declared within another object's definition without declaring it as a value for a particular property.
More relevant for your scenario:
You will notice that child objects can be added to any Item-based type without explicitly adding them to the children property. This is because the default property of Item is its data property, and any items added to this list for an Item are automatically added to its list of children.
Default properties can be useful for reassigning the children of an item. See the TabWidget Example, which uses a default property to automatically reassign children of the TabWidget as children of an inner ListView.
If you take a look at the TabWidget example that the last paragraph refers to, you should have all you need:
import QtQuick 2.0
Item {
id: tabWidget
// Setting the default property to stack.children means any child items
// of the TabWidget are actually added to the 'stack' item's children.
// See the "Property Binding"
// documentation for details on default properties.
default property alias content: stack.children
property int current: 0
onCurrentChanged: setOpacities()
Component.onCompleted: setOpacities()
function setOpacities() {
for (var i = 0; i < stack.children.length; ++i) {
stack.children[i].opacity = (i == current ? 1 : 0)
}
}
Row {
id: header
Repeater {
model: stack.children.length
delegate: Rectangle {
width: tabWidget.width / stack.children.length; height: 36
Rectangle {
width: parent.width; height: 1
anchors { bottom: parent.bottom; bottomMargin: 1 }
color: "#acb2c2"
}
BorderImage {
anchors { fill: parent; leftMargin: 2; topMargin: 5; rightMargin: 1 }
border { left: 7; right: 7 }
source: "tab.png"
visible: tabWidget.current == index
}
Text {
horizontalAlignment: Qt.AlignHCenter; verticalAlignment: Qt.AlignVCenter
anchors.fill: parent
text: stack.children[index].title
elide: Text.ElideRight
font.bold: tabWidget.current == index
}
MouseArea {
anchors.fill: parent
onClicked: tabWidget.current = index
}
}
}
}
Item {
id: stack
width: tabWidget.width
anchors.top: header.bottom; anchors.bottom: tabWidget.bottom
}
}
In cases like this, where you want to replicate something that is done by an item offered by Qt, it can also be helpful to take a look at the source code of what you're trying to replicate. However, the documentation is a bit easier to read. :)