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 : ""
}
}
}
Related
I I´m trying get the click or press event on QML TableView Header.
I have tried on HeaderDelegate create a MouseArea with width, height and anchors.fill from the parent, and get the onClicked or onPressed events, but don´t work.
Example:
MouseArea {
width: parent.width
height: parent.height
anchors.fill: parent
onClicked: {
console.debug("CLICKED: "+styleData.pressed)
console.debug("COLUMN: "+ styleData.column)
}
onPressed: {
console.debug("PRESSED: "+styleData.pressed)
console.debug("COLUMN: "+ styleData.column)
}
}
that doesn't log anything
I also tried use "Connections" on HeaderDelegate, with "target: styleData" and "onPressedChanged"event, and worked, but the "onPressedChanged" get 4 changes in the same time.
Example:
Connections {
target: styleData
onPressedChanged: {
console.debug("PRESSED: "+ styleData.pressed)
console.debug("COLUMN: "+ styleData.column)
}
}
Return:
qml: PRESSED: true
qml: COLUMN: 0
qml: PRESSED: true
qml: COLUMN: 0
qml: PRESSED: false
qml: COLUMN: 0
qml: PRESSED: false
qml: COLUMN: 0
The whole code from TableView:
TableView {
id: tViewEnt
width: parent.width
height: parent.height
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
verticalScrollBarPolicy: Qt.ScrollBarAlwaysOff
horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff
TableViewColumn {
id: column0
title: "column0"
width: 30
delegate: CheckBox {
style: CheckBoxStyle {
indicator: Rectangle {
//INDICATOR PROPERTIES
}
}
}
}
TableViewColumn {
title: "column1"
role: "column1"
}
TableViewColumn {
title: "column2"
role: "column2"
}
headerDelegate: Rectangle {
height: tViewEnt.height / 15
color: styleData.column===0 ? "#4D4D4D" : "#0077B3"
Rectangle {
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.bottomMargin: 1
anchors.topMargin: 1
width: 1
color: "#333"
}
Text {
text: styleData.value
color: "#CFF"
width: parent.width
height: parent.height
font.pointSize: 18
minimumPointSize: 3
fontSizeMode: Text.Fit
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
Connections {
target: styleData
onPressedChanged: {
console.debug("PRESSED: "+ styleData.pressed)
console.debug("COLUMN: "+ styleData.column)
}
}
}
}
So, How do i get the Header Delegate click or pressed event in the right way?
I want to make a ToolTip for a delegate Component in a TableView. It works fine, if I use an ItemDelegate, however, I cannot use an ItemDelegate as it destroys the selection mode of TableView. If I try to define the ToolTip in my delegate component I get the error
qrc:/FileSystem.qml:34 Invalid component body specification
How can I use a ToolTip for a Component?
Here is my code:
TreeView {
id: view
anchors.fill: parent
sortIndicatorVisible: true
model: fileSystemModel
rootIndex: rootPathIndex
selection: sel
selectionMode: 2
Component {
id: mycomp
Row{
id: myrow
CheckBox{
id: cbox
anchors.baseline: ctext.baseline
}
Text{
id: ctext
text: styleData.value
color: styleData.textColor
width: namecolumn.width-cbox.width-myrow.x
elide: Text.ElideRight
}
}
NC.ToolTip {
parent: mycomp
visible: hovered
delay: 1000
text: qsTr(styleData.value)
}
}
TableViewColumn {
id: namecolumn
title: "Name"
role: "fileName"
resizable: true
width: parent.width-sizeWidth-dateWidth-scrollBarWidth
delegate: mycomp
}
A component must consist of exactly one "child". So you could try to wrap the contents of the component into an Item, something like this:
Component {
Item {
width: parent.width
height: myrow.height
Row{
id: myrow
CheckBox{
id: cbox
anchors.baseline: ctext.baseline
}
Text{
id: ctext
text: styleData.value
color: styleData.textColor
width: namecolumn.width-cbox.width-myrow.x
elide: Text.ElideRight
}
}
MouseArea {
id: mouseArea
anchors.fill: true
hoverEnabled: true
}
NC.ToolTip {
visible: mouseArea.containsMouse
delay: 1000
text: qsTr(styleData.value)
}
}
}
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 working on qml, found found an error during moving mouse wheel scrolling i.e.
"TypeError: Cannot read property 'name' of undefined".
It only appear when either mouse wheel scrolling or close the application while during execution and beginning of the application didn't show this error.
One more thing, I am getting accurate data/result whatever I want but due to this error the performance of application get affected.
Please let me know how can I get rid of this issue?
also find below example to see error.
My code is..
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
TableView {
anchors.fill: parent
visible: true
backgroundVisible: false
alternatingRowColors: false
sortIndicatorVisible: true
clip: true
highlightOnFocus: false
width: parent.width
height: parent.height
TableViewColumn{ role: "code" ; title: "cde" ; width: 200;
delegate: Component {
id: codeDelegate
Item{
Text {
color: "lightgray"
elide: styleData.elideMode
text: styleData.value //modelSettingData.get(styleData.row).name
font.family: "Arial"
font.pixelSize: 18
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
Text {
id: metaData
color: "red"
width: parent.width
// height: 20
anchors.bottom: parent.bottom
anchors.bottomMargin: -parent.height/1.5
font.weight: Font.ExtraLight
//elide: Text.ElideMiddle
text: "Time: " + modelSettingData.get(styleData.row).name //Error comes when this part added but I need to use it with the same form
}
}
}
}
}
TableViewColumn{ role: "name" ; title: "nme" ; width: 200
delegate: Component {
id: nameDelegate
Text {
color: "yellow"
elide: styleData.elideMode
text: styleData.value
font.family: "Arial"
font.pixelSize: 18
}
}
}
model: ListModel {
id: modelSettingData
ListElement {
name: "aaaaaaaaaa"
code: "3042666666666"
}
ListElement {
name: "bbbbbb"
code: "32235"
}
ListElement {
name: "ccccccc"
code: "32638"
}
ListElement {
name: "ddddddddddd"
code: "00000000000"
}
ListElement {
name: "eeeeeeeeeeee"
code: "111111111111"
}
ListElement {
name: "ffffffffffff"
code: "222222222222"
}
ListElement {
name: "ggggggggggggg"
code: "3333333333333"
}
ListElement {
name: "hhhhhhhhhhhhh"
code: "4444444444444"
}
ListElement {
name: "iiiiiiiiiiiii"
code: "5555555555555"
}
}
rowDelegate: Rectangle {
property bool selected : styleData.selected
width: parent.width-2
height: 100
color: styleData.selected? "black" : "black"
Rectangle {
width: parent.width
height: 1
anchors.bottom: parent.bottom
visible: parent.selected
color: "yellow"
}
}
}
}
So the subject is basicaly what I'm asking.
TableViewStyle
{
Component {
id: header
Rectangle {
anchors.topMargin: 5
height: 22
anchors.verticalCenter: parent.verticalCenter
color: "#6d6e70"
Text {
anchors.fill: parent
anchors.verticalCenter: parent.verticalCenter
horizontalAlignment: Text.Center
verticalAlignment: Text.AlignVCenter
text: styleData.value
color: "white"
renderType: Text.NativeRendering
}
Rectangle {
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.bottomMargin: 1
anchors.topMargin: 1
width: 1
color: "black"
}
}
}
Component {
id: defaultRow
Rectangle {
height: 30
property color selectedColor: styleData.row % 2 ? "#fbfbfc" : "white"
color: styleData.selected ? "#999" : selectedColor
Rectangle {
width: 1
color: "black"
}
}
}
Component {
id: largeRow
Rectangle {
height: 120
property color selectedColor: styleData.row % 2 ? "#fbfbfc" : "white"
color: styleData.selected ? "#999" : selectedColor
Rectangle {
width: 1
color: "black"
}
}
}
Component {
id: imageDelegate
Image {
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
fillMode: Image.PreserveAspectFit
cache: true;
asynchronous: true;
source: styleData.value
}
}
}
For headers there is a border with width 1 and black color, but for rows it isn't working even if I use the saming parametrs. How do i generate borders for rowdelegate?
You can hack something like that:
Rectangle {
color: "black"
Rectangle {
anchors.fill: parent
anchors.rightMargin: 1
color: "white"
}
}
(don't assume that I've understood the question)
update
(Thanks to Mitch for data example)
You want something like that for the column borders?
import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
Window {
id: win
width: 360
height: 360
visible: true
ListModel {
id: libraryModel
ListElement {
title: "A Masterpiece"
author: "Gabriel"
}
ListElement {
title: "Brilliance"
author: "Jens"
}
ListElement {
title: "Outstanding"
author: "Frederik"
}
}
TableView {
anchors.fill: parent
TableViewColumn {
role: "title"
title: "Title"
width: 100
}
TableViewColumn {
role: "author"
title: "Author"
width: 200
}
model: libraryModel
style: TableViewStyle {
itemDelegate: Rectangle {
width: 200
height: 200
color: "black"
Rectangle {
anchors.fill: parent
anchors.rightMargin: 1
color: "white"
Text {
id: textItem
anchors.fill: parent
verticalAlignment: Text.AlignVCenter
horizontalAlignment: styleData.textAlignment
anchors.leftMargin: 12
text: styleData.value
elide: Text.ElideRight
color: textColor
renderType: Text.NativeRendering
}
}
}
}
}
}