I have a simple treeview model with defined roles.
Here is a code:
TreeView {
model: theModel
itemDelegate: Rectangle {
color: ( styleData.row % 2 == 0 ) ? "white" : "lightblue"
height: 20
Text {
text: styleData.value === undefined ? "" : styleData.value
}
}
TableViewColumn {
width: 100
role: "name_role"
title: "Map"
}
TableViewColumn {
width: 50
role: "description_role"
title: "Description"
}
Image {
width: 15
source: description_role + ".png"
}
}
In the second column i've got correct description displayed, but when i use this role as an image source i've got mistake "role is undefined".
The question is: How to correctly define role as an image source?
I have found the answer, by reading documentation of the TableViewColumn
The QML treeView should be like this:
TreeView {
model: theModel
itemDelegate: Rectangle {
color: ( styleData.row % 2 == 0 ) ? "white" : "lightblue"
height: 20
Text {
text: styleData.value === undefined ? "" : styleData.value
}
}
TableViewColumn {
width: 100
role: "name_role"
title: "Map"
}
TableViewColumn {
width: 50
role: "description_role"
title: "Description"
}
TableViewColumn {
width: 50
role: "description_role"
title: "Icon"
delegate: Image {
source: styleData.value + ".png"
}
}
}
Related
I am Using custom inputRow and MenuButton is placed Outside component when i try to print value of Val1 and Val2 it gives "ReferenceError: val1 is not defined",
how can i access it Outside component.
InputRow {
name:"Command"
enabled: true
filename: "qrc:/AutonomyPlatform/Images/Parameters.svg"
component: Column {
spacing: 10
Row{
spacing: 50
Text {
text: "Val1"
color: Colors.menuBodyTextNormal
font.pointSize: 10
}
TextInput {
id:val1
width: 5
text: selectedModel ? "0" : ""
font.pointSize: 10
color: Colors.menuBodyTextInput
}
Text {
text: "m"
color: Colors.menuBodyTextNormal
font.pointSize: 10
}
}
Row {
spacing: 50
Text{
text: "Val2"
color: Colors.menuBodyTextNormal
font.pointSize: 10
}
TextInput {
id:val2
width: 5
text: selectedModel ? "0" : ""
font.pointSize: 10
color: Colors.menuBodyTextInput
}
Text {
text: "m"
color: Colors.menuBodyTextNormal
font.pointSize: 10
}
}
} //End of Column
MenuButton {
label: "Send"
buttonHeight: 25
buttonWidth: 35
onMenuButtonClicked:
{
console.log("VAL1",val1.text) //ReferenceError: val1 is not defined,
console.log("VAL2",val2.text)
console.log("SEND")
}
}
}
When i put Menubutton inside column component it prints as expected but when its outside component i get ReferenceError as mentioned above
As I mentioned above that could be done in a declarative style:
For example:
Window {
id: window
visible: true
width: 400
height: 600
title: qsTr("Test")
property string str1: ""
property string str2: ""
onStr1Changed: printOut();
onStr2Changed: printOut();
function printOut()
{
console.log(str1, str2);
}
ColumnLayout {
anchors.centerIn: parent
Loader {
id: loader1
sourceComponent: TextInput {
id: txt1
text: "xxx"
Binding {
target: window
property: "str1"
value: txt1.text
}
}
}
Loader {
id: loader2
sourceComponent: TextInput {
id: txt2
text: "xxx"
Binding {
target: window
property: "str2"
value: txt2.text
}
}
}
}
Component.onCompleted: {
loader1.active = true;
loader2.active = true;
}
}
Got a way to access this :
InputRow{
id: userForm
property var userInput1
property var userInput2
component :
Column{
...
TextInput {
id:val1
text: "777"
onTextChanged: userForm.userInput1 = text
Component.onCompleted : userForm.userInput1 = text
}
}
MenuButton{
onClicked : console.log(userForm.userInput1)
}
I try to make a cell editable using a tableview in qt. I have found a few examples and came up with the following:
TableView {
id: tableView
objectName: "tableView"
horizontalScrollBarPolicy: -1
selectionMode: SelectionMode.SingleSelection
Layout.minimumWidth: 300
Layout.fillHeight: true
Layout.fillWidth: true
model: trackableInfoModel
itemDelegate: Rectangle {
Text {
anchors.verticalCenter: parent.verticalCenter
text: styleData.value
}
MouseArea {
id: cellMouseArea
anchors.fill: parent
onClicked: {
if(styleData.column === 2){
//do something
}
}
}
}
From what I found it looks like I need an itemDelegate to paint each cell. Then I add a MouseArea to the cell and check which cell was selected. In my case I only need to react on the cells in column 2.
The thing is when I use the code shown above I get the error that:
JavaScipt blocks are not supported in a QT Quick UI form. (M223)
Because of that I tried to register a property alias for cellMouseArea like this:
property alias cellMouseArea : cellMouseArea
However that leads to this error:
qrc:/EditPageForm.ui.qml:24 Invalid alias reference. Unable to find id "cellMouseArea"
Overlay cell with TextInput on click.
import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
TableView {
id: tableView
objectName: "tableView"
horizontalScrollBarPolicy: -1
selectionMode: SelectionMode.SingleSelection
anchors.fill: parent
TableViewColumn {
id: titleColumn
title: "Title"
role: "title"
movable: false
resizable: false
width: tableView.viewport.width - authorColumn.width
}
TableViewColumn {
id: authorColumn
title: "Author"
role: "author"
movable: false
resizable: false
width: tableView.viewport.width / 3
}
model: ListModel {
id: libraryModel
ListElement {
title: "A Masterpiece"
author: "Gabriel"
}
ListElement {
title: "Brilliance"
author: "Jens"
}
ListElement {
title: "Outstanding"
author: "Frederik"
}
}
itemDelegate: Rectangle {
Text {
anchors { verticalCenter: parent.verticalCenter; left: parent.left }
color: "black"
text: styleData.value
}
MouseArea {
id: cellMouseArea
anchors.fill: parent
onClicked: {
// Column index are zero based
if(styleData.column === 1){
loader.visible = true
loader.item.forceActiveFocus()
}
}
}
Loader {
id: loader
anchors { verticalCenter: parent.verticalCenter; left: parent.left}
height: parent.height
width: parent.width
visible: false
sourceComponent: visible ? input : undefined
Component {
id: input
TextField {
anchors { fill: parent }
text: ""
onAccepted:{
// DO STUFF
loader.visible = false
}
onActiveFocusChanged: {
if (!activeFocus) {
loader.visible = false
}
}
}
}
}
}
}
}
I am trying to display Image and Text in a row of QML table view component, I chose itemDelegate to accomplish it, but the result shows as the bold text on each row, and in image column both image and text are displayed. It seems two times the model items are displaying on the table.
Code:
Rectangle{
width:parent.width
height:parent.height
color: "#333333"
id: root
ListModel {
id: live_alertmodel
}
TableView {
// anchors.top: download_bt.bottom
anchors.horizontalCenter: parent.horizontalCenter
width: root.width
height: 100
TableViewColumn {
role: "time"
title: "Time"
width: root.width/4
}
TableViewColumn {
role: "location"
title: "Location"
width: root.width/4
}
TableViewColumn {
role: "alert"
title: "Alert"
width: root.width/4
}
TableViewColumn {
role: "image"
title: "Image"
width: root.width/4
}
model: live_alertmodel
itemDelegate: Item {
Text {
anchors.verticalCenter: parent.verticalCenter
color: styleData.textColor
//elide: styleData.elideMode
text: styleData.value
}
Text {
anchors.verticalCenter: parent.verticalCenter
color: styleData.textColor
//elide: styleData.elideMode
text: styleData.value
}
Text {
anchors.verticalCenter: parent.verticalCenter
color: styleData.textColor
//elide: styleData.elideMode
text: styleData.value
font.bold: false
}
Image {
id: myIcon;
width:root.width/4;
//height:50;
anchors.horizontalCenter: parent.horizontalCenter;
source: styleData.value;
fillMode: Image.PreserveAspectFit
height:20
cache : true;
asynchronous: true;
}
}
Component.onCompleted: {
for(var i=0;i<10;i++)
live_alertmodel.append({ time:"12/15/2015",
location:"location",
alert:"access",
image:"http://images.freeimages.com/images/premium/previews/4852/48521810-globe-icon-flat-icon-with-long-shadow.jpg" })
}
}
}
Also see the screen shot how the out put looks with the above code.
Anything wrong with above code?
I have solved it by
Removing itemDelegate from TableView
Defining delegate item for each TableViewColumn like,
TableViewColumn {
role: "time"
title: "Time"
width: root.width/4
delegate:textDelegate
}
TableViewColumn {
role: "location"
title: "Location"
width: root.width/4
delegate:textDelegate
}
TableViewColumn {
role: "alert"
title: "Alert"
width: root.width/4
delegate:textDelegate
}
TableViewColumn {
role: "image"
title: "Image"
width: root.width/4
delegate:imageDelegate
}
Finally created separate delegate item for text and image column
Component {
id: textDelegate
Item {
id: f_item
height: cell_txt.height
Text {
id: cell_txt
width: parent.width
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
//font.bold: true
text: styleData.value
elide: Text.AlignHCenter
color: "white"
renderType: Text.NativeRendering
}
}
}
Component {
id: imageDelegate
Item {
Image {
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
fillMode: Image.PreserveAspectFit
height:20
cache : true;
asynchronous: true;
source: styleData.value// !== undefined ? styleData.value : ""
}
}
}
I'm using a rowDelegate to implement variable row heights as follows. However, it doesn't use the default background colors for selected/alternate rows. How can I preserve all defaults that I'm not trying to override?
TableView {
id: messagesTable
TableViewColumn {
role: "severity"
title: ""
width: 20
delegate: Image {
anchors.centerIn: parent
fillMode: Image.Pad
source: iconSources[styleData.value.toLowerCase()]
}
}
TableViewColumn {
role: "message"
title: "Message"
width: 300
}
TableViewColumn {
role: "source"
title: "File"
width: 150
}
TableViewColumn {
role: "startLine"
title: "Line"
width: 40
}
TableViewColumn {
role: "startColumn"
title: "Column"
width: 50
}
rowDelegate: Rectangle {
width: childrenRect.width
height: (messagesModel.get(styleData.row).lineCount || 1) * 20
}
model: messagesModel
}
You can't.
The general rule of styling with Qt Quick Controls is: once you override a delegate, you start from scratch. If the styles provided a DefaultTableViewRowDelegate type, for example, you could construct an instance of that and then it would work, but since the default delegates are written inline, you have no way of accessing them.
I've got a TableView with two TableViewColumns and would like to prevent them from being resized.
How can I do this?
TableView {
TableViewColumn {
title: "Name"
width: parent.width - 20
}
TableViewColumn {
width: 18
delegate: CheckBox {
}
}
TableViewColumn {
title: "Name"
width: parent.width - 20
resizable: false
}