0
votes

I'm new in QML and can create an aplication, who store data to sqlite database. I have problem, that I cannot find any usable whoto..

My sqltablemodel have one table (at this time) - tasks and 5 basic properties,

a. date (date)
b. time (time)
c. subject (string)
d. body (string)
e. solved (bool)

In QML I create two basic screens

a) listview
b) detail

I can, when user click to an existin item in listview, application go to detail screen. Detail will be filled by current data (by selected listview row)

I have two questions / problems.

a. Is model view in QML implemented as i accepted? If I change view, qml automaticly change model/database?

b. I don't known how to fill detail by listview actual row. Can you help me? Any link to usable example?

I tested something like

  • myModel[ListView.currentIndex].Subject
  • ListView.CurrentItem.Subject
  • myModel.index.Subject

etc... But detail is still blanc.

In qml I have something like this (at this moment chaos that will be later cleaned...sry):

LISTVIEW

import QtQuick 2.9
import QtQuick.Controls 1.4 as C
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
Item {
    id: page1

ListView
{
    id: listView1

    anchors.fill: parent
    anchors.margins: 10
    model: myModel
    delegate: myDelegate
    width: parent.width
    currentIndex: currentIndex

    Component {
        id: myDelegate
        Item {
            id: myItem
            property variant myData1: model
            //visible: acknowleadge ? false : downtime ? false : true
            visible: true
            //height: tservice.height + thost.height + tinfo.height + 10
            //height: myItem.visible == true ? (tservice.height + thost.height + tinfo.height + 10) : 0
            height: 100
            width: listView1.width


            Column
            {
                width: myDelegate.width
                Row
                {
                    spacing: 10
                    Text
                    {
                        id: info1
                        text: color
                        color: "#fdd300" //"#ee0a24" "lightgray"
                        width: listView1.width - tdown.width - tack.width
                        font.family: "Microsoft Tai Le"
                        font.pointSize: 16;
                        //font.bold: true;
                    }
                    Text
                    {
                        id: info2
                        text: type
                        color: "black"
                        width: 220
                        font.family: "Microsoft Tai Le"
                        font.pointSize: 16;
                    }
                    Text
                    {
                        id: info3
                        text: subject
                        color: "black"
                        width: 220
                        font.family: "Microsoft Tai Le"
                        font.pointSize: 16;
                    }
                }
                Row{
                    Text
                    {
                        id: info4
                        text: body
                        color:"lightgray"
                        width: listView1.width
                        font.family: "Microsoft Tai Le"
                        font.pointSize: 10;
                        wrapMode: Text.WordWrap
                    }
                }
            }
            MouseArea
            {
                anchors.fill: parent
                onClicked:
                {
                    listView1.currentIndex = index
                    tabBar.incrementCurrentIndex(1)
                }
                onDoubleClicked:
                {
                    listView1.currentIndex = index
                    tabBar.incrementCurrentIndex(1)
                }
                onPressAndHold:
                {
                    listView1.currentIndex = index
                    tabBar.incrementCurrentIndex(1)
                }
            }
        }
    }
    highlightMoveDuration: 1
        highlightMoveVelocity: 1
        highlight: Rectangle
        {
             color:"#26282a"
        }
    }
}

DETAIL

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3

Item {
    id: page2
    property alias textField1: textField1

Column{
    anchors.fill: parent
    anchors.margins: 6
    spacing: 10
    /*
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.topMargin: 20
    anchors.top: parent.top
    columns: 2
    spacing: 6
    */

    Row{
        Text {
            id: label1
            width: 100
            text: qsTr("Subject:")
        }
        TextField {
            id: textField1
            placeholderText: qsTr("Subject...")
            text: myModel[ListView.currentIndex]
        }
    }
    Row{
        width: parent.width
        height: parent.height - 250
        Text {
            id: label2
            width: 100
            text: qsTr("Body:")
        }
        Rectangle {
            id: rect1
            width: parent.width - 100
            height: parent.height
            border.color: 'gray'
            border.width: 1

            ScrollView {
                width: parent.width
                height: parent.height
                TextArea {
                    width: parent.width
                    height: parent.height
                    id: textField2
                    placeholderText: qsTr("Body...")
                }
            }
        }
    }
    Row{
        Text {
            id: label3
            width: 100
            text: qsTr("Date:")
        }
        TextField {
            id: textField3
            placeholderText: qsTr("Date...")
        }
    }
    Row{
        Text {
            id: label4
            width: 100
            text: qsTr("Time:")
        }
        TextField {
            id: textField4
            placeholderText: qsTr("Time...")
        }
    }
    Row{
        Button {
            id: button1
            width: 100
            text: qsTr("Storno")
        }
        Button {
            id: button2
                text: qsTr("Ok")
            }
        }
    }
}

MODEL:

#include "listmodel.h"
#include "database.h"

ListModel::ListModel(QObject *parent) :
    QSqlQueryModel(parent)
{
    this->updateModel();
}

// The method for obtaining data from the model
QVariant ListModel::data(const QModelIndex & index, int role) const {

    // Define the column number, on the role of number
    int columnId = role - Qt::UserRole - 1;
    // Create the index using a column ID
    QModelIndex modelIndex = this->index(index.row(), columnId);

    return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}

QHash<int, QByteArray> ListModel::roleNames() const {

    QHash<int, QByteArray> roles;
    roles[IdRole] = "id";
    roles[ColorRole] = "color";
    roles[TypeRole] = "type";
    roles[SubjectRole] = "subject";
    roles[BodyRole] = "body";
    roles[DateRole] = "date";
    roles[TimeRole] = "time";
    return roles;
}

// The method updates the tables in the data model representation
void ListModel::updateModel()
{
    // The update is performed SQL-queries to the database
    this->setQuery("SELECT id, " TABLE_COLOR ", " TABLE_TYPE ", " TABLE_SUBJECT ", " TABLE_BODY ", " TABLE_DATE ", " TABLE_TIME " FROM " TABLE);
}

// Getting the id of the row in the data view model
int ListModel::getId(int row)
{
    return this->data(this->index(row, 0), IdRole).toInt();
}
2
show your model.eyllanesc
I added it to my question.exo

2 Answers

0
votes

Ok, I have several problems in my code.

a) I must create a new variable mData and add him my model (in listveiw.qml). It must be in delegate!

  Component {
            id: myDelegate
            Item {
                id: myItem
                property variant mData: model
                visible: true
                ...
                ...

b) Then I must create global variables (in main.qlm):

property var gColor
property var gType
property var gSubject
property var gBody
property var gDate
property var gTime

c) And finaly, I must fill listView1.currentIndex and global variables to valeus after doubleclick (in listveiw.qml)

            MouseArea
            {
                anchors.fill: parent
                onClicked:
                {
                    listView1.currentIndex = index
                    gColor = listView1.currentItem.mData.color
                    gType = listView1.currentItem.mData.type
                    gSubject = listView1.currentItem.mData.subject
                    gBody = listView1.currentItem.mData.body
                    gDate = listView1.currentItem.mData.date
                    gTime = listView1.currentItem.mData.time
                }

Then (in detail.qml) i can call

text: gSubject
text: gDdate
text: gTime
etc...
0
votes

Surely if MouseArea is in a delegate, there's no need to use currentItem.mData, you can just use the attributes directly:

gColor = color
gType = type
gSubject = subject
gBody = body
gDate = date
gTime = time

And you can probably read this. It's quite easy to make a QML model of an SQL table.