QML scope: Invalid alias reference - qt

I get the following error: qrc:/main.qml:17 Invalid alias reference. Unable to find id "button2". The error can be reproduced by the following qml file:
import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Window 2.0
import QtQuick.Layouts 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Item {
id: root
property alias myprop: button2
SplitView {
id: splitView
orientation: Qt.Vertical
SplitView {
id: splitView1
orientation: Qt.Horizontal
TabView {
Tab{
title: "Tab1"
}
Tab{
title: "Tab2"
ColumnLayout {
GridLayout {
Button {
id: button2
text: qsTr("test button")
}
}
}
}
}
}
}
}
}
It seems I have a misunderstanding about qml scope. I thought the parent can access the ids of all of its children?

Related

How can I change a text in a QML from another qml?

I would like to change myText.text from the main.qml but myText is in an another folder. How can I do that?
---main.qml---
import QtQuick 2.14
import QtQuick.Window 2.1
import QtQuick.Controls 2.12
import "qrc:/secondfolder/pagetwo.qml" as PageTwo
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button {
id: button
x: 63
y: 71
width: 142
height: 66
text: qsTr("Button")
MouseArea{
anchors.fill: parent
onClicked: {
PageTwo.myText.text = "hello world"
}
}
}
}
---pagetwo.qml---
Item {
Text {
id: myText
text: "default text"
}
}
When I run the code I got this error: "qrc:/secondfolder/pagetwo.qml": no such directory
You need to declare your PageTwo in main.qml and give it id, like this:
PageTwo {
id: pageTwo
}
Instend of PageTwo.myText.text = "hello world" you need to write pageTwo.myText.text = "hello world".
Then in file PageTwo.qml you have to write property alias myText: myText.
main.qml
import QtQuick 2.14
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
PageTwo {
id: pageTwo
}
Button {
id: button
width: 142
height: 66
text: qsTr("Button")
onClicked: {
pageTwo.myText.text = "hello world"
}
}
}
PageTwo
import QtQuick 2.14
Item {
property alias myText: myText
Text {
id: myText
text: "default text"
}
}
I'd recomend you to read this and check some qml example applications.

How to set objectName for PopupItem from QML?

QML Popup and derived controls are creating a PopupItem object which is a visual representation of it, but Popup itself is parented to the contentData of the application window. objectName specified for Popup is not applied to PopupItem. For example, the following application:
import QtQuick 2.12
import QtQuick.Controls 2.12
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Popup Test")
Button {
text: "Open"
onClicked: dummyPopup.open()
}
Popup {
id: dummyPopup
objectName: "dummyPopup"
x: 100
y: 100
width: 200
height: 300
modal: true
focus: true
}
}
creates PopupItem with empty objectName
Is there a way to set objectName for PopupItem from QML?
Set the objectName of its contentItem upon completion:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Popup Test")
Button {
text: "Open"
onClicked: dummyPopup.open()
}
Popup {
id: dummyPopup
objectName: "dummyPopup"
x: 100
y: 100
width: 200
height: 300
modal: true
focus: true
Component.onCompleted: {
contentItem.objectName = "foo"
print(contentItem)
}
}
}
By the way, if this is for auto tests, I have a hack in C++ that avoids the need to give an objectName to the contentItem:
QObject *TestHelper::findPopupFromTypeName(const QString &typeName) const
{
QObject *popup = nullptr;
foreach (QQuickItem *child, overlay->childItems()) {
if (QString::fromLatin1(child->metaObject()->className()) == "QQuickPopupItem") {
if (QString::fromLatin1(child->parent()->metaObject()->className()).contains(typeName)) {
popup = child->parent();
break;
}
}
}
return popup;
}
You can then use that function like this in your test:
const QObject *newProjectPopup = findPopupFromTypeName("NewProjectPopup");
QVERIFY(newProjectPopup);
QTRY_VERIFY(newProjectPopup->property("opened").toBool());

ComboBox disable an item at a particular index

I have a combobox in qml in a as a TableViewColummn and I define it as follows:
import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
ListModel {
id: comboModel
ListElement {
text: ""
Index: -1
Dims: -1
}
}
TableViewColumn {
id: imageTypeList
role: "ImageType"
title: "Image Type"
width: 100
delegate: Rectangle {
ComboBox {
anchors.verticalCenter: parent.verticalCenter
anchors.margins: 2
model: comboModel
onActivated : {
console.log(comboModel.get(index).Index)
}
}
}
}
My question is that if it is possible to disable a combobox menu item given a index to the item in the ComboBox. So, I would not like to change the underlying model but actually simply disable the item and not allow the user to select it.
Is it possible to disable a ComboBox menu item ... and not allow the user to select it?
Sure, it is possible.
To do it using Quick Controls 2 you need to create ComboBox delegate this way:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
Window {
visible: true
width: 640
height: 200
title: qsTr("Let's disable some items in ComboBox")
ComboBox {
id: control
currentIndex: 0
anchors.centerIn: parent
model: [
{ text: "Enabled item.", enabled: true },
{ text: "Supposed to be disabled. Can't click on it.", enabled: false},
{ text: "Last, but enabled item.", enabled: true}
]
width: 500
textRole: "text"
delegate: ItemDelegate {
width: control.width
text: modelData.text
font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
highlighted: ListView.isCurrentItem
enabled: modelData.enabled
}
}
}
If you are using Quick Controls 1, you should provide your own implementation of ComboBox component.

Delegate FolderListModel in TableView

I'm learning Qt Quick to make a file manager, but I have no experience in QML or GUI in general. The first step is to list the content of a folder, using FolderListModel. I got the example code working using ListView, but naturally I want to display multiple fields in addition to the name, e.g. size, time, and so on. Thus, I'm thinking of using TableView.
However, it's not clear to me how to delegate each entry as a row in TableView. Currently I'm simply using itemDelegate to display fileName, and the resultant is that in each row, all the columns repeat the name of the entry. So I think rowDelegate is the correct way, but how do I make a proper delegate Component for that purpose? Conceptually I'd like to specify an array of fields, e.g. [model.fileName, model.fileSize] corresponding to the table columns. Is this achievable?
For clarify I'm posting the code below:
import QtQuick 2.4
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.1
import Qt.labs.folderlistmodel 2.1
ApplicationWindow {
visible: true
width: 900
height: 600
title: qsTr("Hello World")
Item {
anchors.fill: parent
width: 900
height: 600
SplitView {
id: splitView1
anchors.fill: parent
TabView {
id: tabView1
width: splitView1.width / 2
Tab {
title: qsTr("Home")
TableView {
id: tableView1
width: splitView1.width / 2
TableViewColumn {
role: "name"
title: qsTr("Name")
width: tableView1.width * 0.75
}
TableViewColumn {
role: "size"
title: qsTr("Size")
width: tableView1.width * 0.25
}
FolderListModel {
id: folderModel2
folder: "file:/home/username"
nameFilters: ["*"]
showHidden: true
}
Component {
id: fileDelegate2
Text {
text: model.fileName
}
}
model: folderModel2
itemDelegate: fileDelegate2
}
}
}
}
}
}
The documentation mentions that the following roles are available:
List item
fileName
filePath
fileURL (since Qt 5.2)
fileBaseName
fileSuffix
fileSize
fileModified
fileAccessed
fileIsDir
So you don't need to have custom delegates to display that information, just set the role property of TableViewColumn appropriately:
import QtQuick 2.4
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.1
import Qt.labs.folderlistmodel 2.1
ApplicationWindow {
visible: true
width: 900
height: 600
title: qsTr("Hello World")
Item {
anchors.fill: parent
width: 900
height: 600
SplitView {
id: splitView1
anchors.fill: parent
TabView {
id: tabView1
width: splitView1.width / 2
Tab {
title: qsTr("Home")
TableView {
id: tableView1
width: splitView1.width / 2
TableViewColumn {
role: "fileName"
title: qsTr("Name")
width: tableView1.width * 0.75
}
TableViewColumn {
role: "fileSize"
title: qsTr("Size")
width: tableView1.width * 0.25
}
FolderListModel {
id: folderModel2
folder: "file:/home/username"
nameFilters: ["*"]
showHidden: true
}
model: folderModel2
}
}
}
}
}
}

How to edit App menu from Qt Creator?

I start "QML App with controls" project in Qt Creator. I see that I can add to canvas different kind of controls, but I do not see how I can in graphical mode edit menu like: File, View, Edit... In constructor on canvas it's simple do not exists, but it's exists of running app, like http://img.ctrlv.in/img/15/10/03/560f856edb26c.png
You can create the menu in the main.qml file, here is an example application:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
title: qsTr("Hello World")
width: 640
height: 480
visible: true
menuBar: MenuBar {
Menu {
title: qsTr("&File")
MenuItem {
text: qsTr("&Open")
onTriggered: messageDialog.show(qsTr("Open action triggered"));
}
MenuItem {
text: qsTr("Save")
onTriggered: messageDialog.show(qsTr("Save action triggered"));
}
}
Menu {
title: qsTr("&Help")
MenuItem {
text: qsTr("About")
onTriggered: messageDialog.show(qsTr("About: test QML app with menu"));
}
}
}
MainForm {
anchors.fill: parent
button1.onClicked: messageDialog.show(qsTr("Button 1 pressed"))
button2.onClicked: messageDialog.show(qsTr("Button 2 pressed"))
}
MessageDialog {
id: messageDialog
title: qsTr("Message Test")
function show(caption) {
messageDialog.text = caption;
messageDialog.open();
}
}
}

Resources