我是QML的新手,可以创建一个应用程序,将数据存储到sqlite数据库。 我有问题,我找不到任何可用的Whoto。
我的sqltablemodel目前有一个表-任务和5个基本属性,
a. date (date)
b. time (time)
c. subject (string)
d. body (string)
e. solved (bool)
在QML中,我创建了两个基本屏幕
a) listview
b) detail
当用户单击列表视图中的项目时,应用程序可以转到详细信息屏幕。详细信息将由当前数据填充(按选定的列表视图行)
我有两个问题。
a。 QML是否在我接受的情况下实施模型视图?如果我更改视图,qml会自动更改模型/数据库吗?
b。我不知道如何通过listview实际行来填充细节。你能帮助我吗?有可用示例的链接吗?
我测试了
等... 但是细节仍然是空白。
在qml中,我有类似的内容(此刻混乱,稍后将清除...很抱歉):
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"
}
}
}
详细信息
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")
}
}
}
}
型号:
#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();
}
答案 0 :(得分:0)
好的,我的代码中有几个问题。
a)我必须创建一个新变量mData并将其添加到我的模型中(在listveiw.qml中)。它必须在委托中!
Component {
id: myDelegate
Item {
id: myItem
property variant mData: model
visible: true
...
...
b)然后,我必须创建全局变量(在main.qlm中):
property var gColor
property var gType
property var gSubject
property var gBody
property var gDate
property var gTime
c)最后,我必须在双击(在listveiw.qml中)后将listView1.currentIndex和全局变量填充为valeus
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
}
然后我可以打电话(详细信息。qml)
text: gSubject
text: gDdate
text: gTime
etc...
答案 1 :(得分:0)
当然,如果MouseArea
在委托中,则无需使用currentItem.mData
,您可以直接使用属性:
gColor = color
gType = type
gSubject = subject
gBody = body
gDate = date
gTime = time
您可能会读到this。创建SQL表的QML模型非常容易。