How to get Clicked or Pressed event on QML TableView header? - qt

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?

Related

QML ScrollBar combined with ListView

I'm new to QML and QT so don't blame me if this question is going to sound stupid for most of you but I've search all over the internet without any luck in founding an answer.
What I'm trying to do:
I'm having a ScrollView which has inside of it a ScrollBar and a ListView.
I want that at the moment when I'm scrolling the ListView elements to also move the bar from ScrollBar. In other words, I want to use the ScrollBar as an overall view of your current position, you are not supposed to touch that, its only purpose is for viewing.
My Code:
ScrollView{
implicitHeight: 100
implicitWidth: 50
anchors.fill: parent
ScrollBar.horizontal: ScrollBar{
id: hbar
active: true
policy: ScrollBar.AlwaysOn
anchors {
left: parent.left
top: parent.top
right: parent.right
}
background: Rectangle {
implicitWidth: 100
implicitHeight: 50
opacity: enabled ? 1 : 0.3
color: hbar.down ? "#red" : "black"
}
contentItem: Rectangle {
implicitWidth: 6
implicitHeight: 100
radius: width / 2
color: hbar.pressed ? "#81e889" : "#c2f4c6"
}
}
ListView {
id: listViewParent
height: listViewID.height/10*6
contentHeight: height*2
contentWidth: width*2
clip: false
interactive: false
keyNavigationWraps: true
anchors.right: parent.right
anchors.rightMargin: 0
anchors.left: parent.left
anchors.leftMargin: 0
enabled: true
scale: 1
transformOrigin: Item.Center
anchors.verticalCenter: parent.verticalCenter
boundsBehavior: Flickable.DragAndOvershootBounds
flickableDirection: Flickable.HorizontalFlick
highlightMoveDuration: 0
cacheBuffer: 300
snapMode: ListView.SnapToItem
layoutDirection: Qt.LeftToRight
orientation: ListView.Vertical
model: 1
delegate:
ListView {
width: parent.width;
height: parent.height;
spacing: listViewID.width/8/9
model: MovieListModel {}
orientation: ListView.Horizontal
id: listid
delegate:
Rectangle {
property int recDynamicHeight: listViewID.height/10*6
property int recOriginalHeight: listViewID.height/10*6
property int recDynamiclWidth: listViewID.width/7
property int recOriginalWidth: listViewID.width/7
id: rectPer
width: recDynamiclWidth
height: recDynamicHeight
Image {
id: image1
anchors.fill: parent;
source: model.imgUrl
}
Text {
property bool isVisible: false
color: "#ffffff"
anchors.fill: parent
visible: textid.isVisible
id: textid
text: model.title
font.bold: true
horizontalAlignment: Text.AlignLeft
font.pixelSize: listViewID.width/8/9
topPadding: listViewID.width/8/9
leftPadding: listViewID.width/8/9
}
Text {
anchors.topMargin: listViewID.width/8/9
color: "#ffffff"
anchors.fill: parent
visible: textid.isVisible
id: yearId
text: model.year
horizontalAlignment: Text.AlignLeft
font.pixelSize: listViewID.width/8/9
topPadding: listViewID.width/8/9*2
leftPadding: listViewID.width/8/9
}
MouseArea {
anchors.fill: parent
onPressed: {
rectPer.recDynamicHeight = rectPer.recOriginalHeight;
rectPer.recDynamicHeight += rectPer.recOriginalHeight/10;
rectPer.recDynamiclWidth += rectPer.recOriginalWidth/10;
console.log(textid.isVisible);
textid.isVisible = true;
textid.visible = textid.isVisible;
console.log(sideButtonID.x);
console.log(sideButtonID.y);
console.log(model.year + " clicked");
}
onClicked: {
console.log("INDEX: " + model.id)
load_page(PageType.movie_detailed_view, model.title, model.description, model.imgUrl, model.type, model.year)
}
onReleased: {
rectPer.recDynamicHeight = rectPer.recOriginalHeight;
rectPer.recDynamiclWidth = rectPer.recOriginalWidth;
textid.isVisible = false;
textid.visible = textid.isVisible;
}
}
}
}
}
}
Layout:
You could try use a Flickable instead of a ScrollView and spawn a ListView there(delegates: Rectangles). Then, spawn the Scrollbar inside the ListView

Long TabBar - adding a size and position indicator to reflect the presence of off-screen tabs

I have a tab bar with a stacklayout like the following:
Rectangle {
id: rect
height: 190
anchors.right: parent.right
anchors.left: parent.left
color: "transparent"
anchors.top: uniqueHandleText.bottom
anchors.topMargin: 100
TabBar {
id: frame
anchors.right: parent.right
anchors.left: parent.left
background: Rectangle {
color: "#737373"
}
x: -hbar.position * width
Repeater {
model: wizard.categories
TabButton {
id: tabData
property bool selected: false
text: modelData.name
width: 200
font.pixelSize: 18
contentItem: Text {
text: tabData.text
font: tabData.font
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
wrapMode: Text.WordWrap
color: "#FFFFFF"
}
background: Rectangle {
implicitWidth: frame.width
implicitHeight: 180
opacity: enabled ? 1 : 0.3
color: tabData.checked ? "#BD9CBE": "#737373"
}
}
}
}
ScrollBar {
id: hbar
hoverEnabled: true
active: hovered || pressed
orientation: Qt.Horizontal
size: rect.width / frame.width
anchors.left: parent.left
anchors.right: parent.right
anchors.top: frame.bottom
}
Text {
font.pixelSize: 18
text: "Next"
anchors.right: parent.right
visible: frame.x != frame.width ? true: false
}
StackLayout {
id: stack1
anchors.left: parent.left
anchors.right: parent.right
anchors.top: frame.bottom
currentIndex: frame.currentIndex
Repeater {
model: wizard.categories
Item {
id: homeTab
TabBar {
id: homeTabTab
anchors.right: parent.right
anchors.left: parent.left
anchors.top: parent.top
height: 180
background: Rectangle {
color: "#958096"
}
Repeater {
model: modelData.sub_categories
TabButton {
property bool selected: false
id: currentTab
text: modelData.name
width: 200
font.pixelSize: 18
background: Rectangle {
implicitWidth: frame.width
implicitHeight: 180
opacity: enabled ? 1 : 0.3
color: currentTab.checked ? "#958096": "#8D758E"
}
contentItem: Text {
text: currentTab.text
font: currentTab.font
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
wrapMode: Text.WordWrap
color: "#FFFFFF"
MouseArea {
anchors.fill: parent
onClicked: {
if(currentTab.checked){
currentTab.checked = false
} else {
currentTab.checked = true
}
}
onDoubleClicked: {
currentTab.selected = true
var found = false;
var someText = frame.itemAt(stack1.currentIndex).text;
print(someText)
for(var i = 0; i<wizard.selectedSkills.count; i++){
if(wizard.selectedSkills.get(i).name === someText){
wizard.selectedSkills.get(i).sub_categories.append({"name":currentTab.text});
wizard.skills.push({"name": someText})
found = true;
}
}
if(!found){
print(currentTab.text)
wizard.selectedSkills.append({"name":someText, "sub_categories":[{"name":currentTab.text}]})
}
print(window.selectedSkills)
}
}
}
}
}
}
}
}
}
}
I've tried many different things to add a scrollbar or to figure out how to use the flickable functionality that TabBar has. However, the documentation doesn't specify how it works, it just does. Therefore, they are not accessible (or even rewritteable, to use those properties). I want to add a small indicator like an arrow to specify that there is more elements for ease of navigation on desktop on top of the TabBar functionality.
It doesn't seem like the necessary properties are exposed in order to make this happen the easy way.
However, since this is QML, it means the whole object tree is gaping wide open to introspection, allowing us to establish that the item that does the flicking is the contentItem of a ListView inside the Container the ToolBar inherits. The view happens to be the second visible child, although this is technically "private implementation" that one should not rely on. So it is better to take some extra care to establish whether or not you have the correct object.
ApplicationWindow {
id: main
width: 640
height: 480
visible: true
TabBar {
id: toolbar
width: parent.width
height: 50
Repeater {
model: 10
TabButton {
text: "test " + index
width: 100
}
}
}
Rectangle {
height: 5
width: main.width * (view ? view.visibleArea.widthRatio : toolbar.width / toolbar.contentWidth)
color: "red"
anchors.top: toolbar.bottom
x: view ? (main.width - width) * (view.contentX / (view.contentWidth - view.width)) : 0
}
property ListView view: {
var l = toolbar.visibleChildren.length
while (--l) if ("cacheBuffer" in toolbar.visibleChildren[l]) return toolbar.visibleChildren[l]
return null
}
}
And there you have it. We iterate the tabview children until we find one that has a property cacheBuffer which is fairly unique to ListView, and once we have that, we can access the needed properties. As you see, for the indicator width we can do even without the list view, as the toolbar exposes a contentWidth property, but for the indicator position there is no workaround.
And it works:

QML - Not able to canvas.requestPaint() from a function

I am a beginer and i am not getting how to call canvas.requestPaint() in My QML ,Code is below:
//myTab.qml
TabView {
id: tv
width: parent.width
height: parent.height
antialiasing: true
style: TabViewStyle {
frameOverlap: -1
tab: Rectangle {
color: "Transparent"
implicitWidth: text1.width + 50
implicitHeight: 20
radius: 2
smooth: true
Canvas {
id: canvas1
anchors.fill: parent
width: parent.width
height: parent.height
onPaint: {
styleData.selected ? drawTab(canvas1,"#0C3142") :
drawTab(canvas1,"Transparent") //Some custom JS function to draw a object
}
Text {
id: text1
height: parent.height
verticalAlignment: Text.AlignVCenter
anchors.left : parent.left
anchors.leftMargin: 15
text: styleData.title
color: "white"
}
}
}
frame: Rectangle {
width: parent.width
height: parent.height
color: "Transparent"
border.color:"white"
}
tabBar: Rectangle {
color: "Transparent"
anchors.fill: parent
}
}
Tab {
id: tab1
title: "Tab1"
}
Tab{
id: tab2
title: "Tab2"
}
onCurrentIndexChanged: {
console.log("index changed "+currentIndex)
canvas1.repaint() //ERRROR - not defind canvas1
}
}
When i try to use in onCurrentIndexChanged, I am getting the following error:
ReferenceError: canvas1 is not defined.
Please Suggest.
You have the id canvas1 in another scope, as the tab-style is a Component and the ID therefore not necessarily unique for the TabView. It might be instantiated multiple times.
I have little experience with the TabView, so there might be another solution. I however would declare a signal: refresh in the TabView which I trigger, whenever I want to repaint.
Then I'd use a Connections-element within the Canvas to connect to this signal to execute the repaint
Example:
TabView {
id: tv
width: parent.width
height: parent.height
antialiasing: true
signal refresh // *** DEFINE SIGNAL HERE
style: TabViewStyle {
frameOverlap: -1
tab: Rectangle {
color: "Transparent"
implicitWidth: text1.width + 50
implicitHeight: 20
radius: 2
smooth: true
Canvas {
id: canvas1
anchors.fill: parent
width: parent.width
height: parent.height
onPaint: {
styleData.selected ? drawTab(canvas1,"#0C3142") :
drawTab(canvas1,"Transparent") //Some custom JS function to draw a object
}
function drawTab() { // *** I DONT KNOW WHAT SHOULD BE DONE HERE
console.log('do nothing')
}
// *** CONNECT TO SIGNAL HERE ***
Connections {
target: tv
onRefresh: canvas1.requestPaint() // *** repaint is not a function.
}
Text {
id: text1
height: parent.height
verticalAlignment: Text.AlignVCenter
anchors.left : parent.left
anchors.leftMargin: 15
text: styleData.title
color: "white"
}
}
}
frame: Rectangle {
width: parent.width
height: parent.height
color: "Transparent"
border.color:"white"
}
tabBar: Rectangle {
color: "Transparent"
anchors.fill: parent
}
}
Tab {
id: tab1
title: "Tab1"
}
Tab{
id: tab2
title: "Tab2"
}
onCurrentIndexChanged: {
console.log("index changed "+currentIndex)
refresh() // *** INVOKE SIGNAL HERE
}
}

Combobox popup is opened transparent

I have a Combobox (Control 2.0). I write my own combobox like
Rectangle
{
border.width: 1
border.color: "lightsteelblue"
height:dp(40)
width: parent.width
ComboBox {
id:tmCombo
model:combotm.datalist
textRole: "value"
anchors.fill: parent
currentIndex:-1;
contentItem: Text {
leftPadding: 0
rightPadding: tmCombo.indicator.width + tmCombo.spacing
text:tmCombo.currentIndex==-1 ? "":tmCombo.model[tmCombo.currentIndex].value
font: tmCombo.font
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
}
onCurrentIndexChanged: {
if(currentIndex!=-1) {
var sqlid=model[currentIndex].sqlid;
combotm.getsqlid(sqlid,1,Query.SelectSubParam,Query.Subq,"TMC",1);
TaskResult.taskresult.HatBilgisi_TM=sqlid;
tmsCombo.enabled=true;
}
else {
tmsCombo.enabled=false;
}
tmsCombo.currentIndex=-1;
}
}
}
My problem is that when Combobox first open, half of popup is transparent. Then I close and open combobox again. Everthing is OK. I am working in android platform.
SOLUTION:
I have added import QtQuick.Templates 2.0 as T and chance Popup with T.Popup . That's work
Rigth code is : Combobox with filter input :) I have binded model from C++ side with Q_Property . dp function is my global function in main.qml which is fixed pixel for any device.
ComboBox {
id:trCombo
model:combotr.datalist
textRole: "value"
anchors.fill: parent
currentIndex:-1;
contentItem: Text {
leftPadding: 0
rightPadding: trCombo.indicator.width + trCombo.spacing
text:trCombo.currentIndex==-1 ? "":trCombo.model[trCombo.currentIndex].value
font: trCombo.font
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
}
popup: T.Popup {
id:mpopup
y: trCombo.height - (trCombo.visualFocus ? 0 : 1)
width: trCombo.width
implicitHeight: listview.contentHeight
topMargin: 6
bottomMargin: 6
// focus: true
closePolicy: Popup.NoAutoClose
contentItem: Item {
Column
{
anchors.fill: parent
spacing: 5
TextField
{
placeholderText: "arama yapın"
width: trCombo.width
height: dp(35)
// color: "red"
focus:true
inputMethodHints: Qt.ImhNoPredictiveText;
onTextChanged:{
//console.log("degisiyor");
process.filtertr(text);
}
onAccepted:{
// console.log("Tasarım Bitti");
isfinished(true);
// text="";
}
}
ListView {
id: listview
clip: true
model: trCombo.popup.visible ? trCombo.delegateModel : null
currentIndex: trCombo.highlightedIndex
width: trCombo.width
height:dp(500)
Rectangle {
z: 10
parent: listview
width: listview.width
height: listview.height
color: "transparent"
border.color: "#bdbebf"
layer.smooth: true
}
ScrollIndicator.vertical: ScrollIndicator { }
}
}
}
background: Rectangle { }
onClosed: {
if(!flag)
{
mpopup.open();
}
else
flag=false;
}
}
delegate: ItemDelegate {
width: trCombo.width
text: trCombo.textRole ? (Array.isArray(trCombo.model) ? modelData[trCombo.textRole] : model[trCombo.textRole]) : modelData
font.weight: trCombo.currentIndex === index ? Font.DemiBold : Font.Normal
highlighted: trCombo.highlightedIndex == index
onClicked: {
isfinished(true);
}
}
onCurrentIndexChanged: {
if(currentIndex!=-1)
{
var sqlid=model[currentIndex].sqlid;
combotr.getsqlid(sqlid,1,Query.SelectSubParam,Query.Subq,"TRC",1);
TaskResult.taskresult.HatBilgisi_TR=sqlid ;
trsCombo.enabled=true;
}
else
trsCombo.enabled=false;
trsCombo.currentIndex=-1;
}
}

How to animate a message that appears on bottom?

I'm showing a message on the bottom:
Msg.qml
import QtQuick 2.4
Item {
property alias text: mf.text
anchors.fill: parent
antialiasing: false
opacity: 0.9
z: 100
MsgForm {
id: mf
width: parent.width
y: parent.height - height - 5
}
}
MsgForm.ui.qml
import QtQuick 2.4
Item {
property alias text: msg.text
width: 200
id: message
height: msg.height+10
Rectangle {
id: rectangle
color: "#fb9191"
anchors.fill: parent
border.color: "#fd6666"
border.width: 2
Text {
id: msg
anchors.top: parent.top
anchors.topMargin: 2
textFormat: Text.PlainText
anchors.right: parent.right
anchors.rightMargin: 4
anchors.left: parent.left
anchors.leftMargin: 4
wrapMode: Text.WordWrap
clip: false
font.bold: true
font.pointSize: 12
font.family: "Tahoma"
}
}
}
How can I animate the form to appear from the bottom smoothly?
After the animation, if the window resizes, the message must stay always on the bottom.
You can play with anchors.bottomMargin property to raise the message item from the bottom.
import QtQuick 2.4
Item {
property alias text: mf.text
anchors.fill: parent
antialiasing: false
opacity: 0.9
z: 100
MsgForm {
id: mf
property bool showing: false
width: parent.width
anchors{
bottom: parent.bottom
bottomMargin: mf.showing ? 0 : -mf.height
Behavior on bottomMargin{
NumberAnimation{ }
}
}
}
}
Thanks everyone. In the end I've solved the issue by following advices received in the qtcentre forum.
The desired effect can be achieved easily by defining a local numerical property that is use to bind to either an anchors.XXXXmargin or the y property expression.
Following this approach a possible solution is the following:
MsgForm {
property bool showing: false
property int position: showing ? height : 0
width: parent.width
y: parent.height - position
Behavior on position {
NumberAnimation {duration: 500}
}
}
You can make an animation on the opacity change:
Msg.qml
import QtQuick 2.4
Item {
property alias text: mf.text
anchors.fill: parent
antialiasing: false
opacity: 0.9
z: 100
MouseArea{
anchors.fill: parent
onClicked: mf.opacity = !mf.opacity
}
MsgForm {
id: mf
//y: parent.height - height - 5
opacity:0
Behavior on opacity {
NumberAnimation{
duration:600
}
}
width: parent.width
anchors.bottom: parent.bottom
}
}
or any other NumberAnimation. I recommand you to create States, within it do some propertyChanges, and on some actions, example button clicked change states.
example in your MsgForm.ui.qml add:
MouseArea{
anchors.fill: parent
onClicked: mf.state= "show"
}
and in the action, example:
in my mouseArea I change the state of mf
MouseArea{
anchors.fill: parent
onClicked: mf.state= "show"
}
If you want an Animation on the y try this:
MsgForm.ui.qml
import QtQuick 2.4
Item {
id: message
property alias text: msg.text
width: parent.width
height: msg.height+10
Rectangle {
id: rectangle
color: "#fb9191"
anchors.fill: parent
border.color: "#fd6666"
border.width: 2
Text {
id: msg
anchors.top: parent.top
anchors.topMargin: 2
textFormat: Text.PlainText
anchors.right: parent.right
anchors.rightMargin: 4
anchors.left: parent.left
anchors.leftMargin: 4
wrapMode: Text.WordWrap
clip: false
font.bold: true
font.pointSize: 12
font.family: "Tahoma"
}
}
Behavior on y {
NumberAnimation{
duration:300
}
}
states: [
State {
name: "show"
PropertyChanges {
target: message
y: parent.height - height
}
},
State {
name: "hide"
PropertyChanges {
target: message
y: parent.height + height + 5
}
}
]
}
Msg.qml
import QtQuick 2.4
Rectangle {
property alias text: mf.text
width:800
height: 480
antialiasing: false
opacity: 0.9
z: 100
MouseArea{
anchors.fill: parent
onClicked: mf.state= "show"
}
MsgForm {
id: mf
//y: parent.height - height - 5
y: parent.height +height + 5
width: parent.width
}
}

Resources