I'm trying to rewrite my application using QML modules and droping Qt Widgets. I have a very complex QTreeView which is used to display Tracks, Albums and Artists.
Here's what I have right now:
I'm building subclasses of QStandardItem where I've redefined type() in order to paint them correctly in my delegate, like this:
switch (item->type()) {
case Miam::IT_Album:
this->paintRect(painter, o);
this->drawAlbum(painter, o, item);
break;
case Miam::IT_Artist:
this->paintRect(painter, o);
this->drawArtist(painter, o, item);
break;
...
default:
QStyledItemDelegate::paint(painter, o, index);
break;
}
It was painful to write (managing brushes, pixels, drawing lines), but it worked quite well!
Now let's rewrite to QML
Work in progress:
My problem is to be able to display multiples data roles on the same column. From what I've understood, in QML you need to use a TableViewColumn for each Column of your TreeView (or TableView). And each column is mapped to a single role.
This is my work-in-progress TreeView:
TreeView {
alternatingRowColors: false
TableViewColumn {
id: column
title: qsTr("Artists")
role: "text"
delegate: Rectangle {
implicitHeight: height
implicitWidth: width
color: Material.background
Image {
id: disc
sourceSize.height: 20
sourceSize.width: 20
source: "qrc:/images/disc.png"
visible: libraryItemModel.data("cover")
}
Label {
anchors.left: disc.visible ? disc.right : undefined
text: styleData.value
color: Material.foreground
}
}
}
model: libraryItemModel
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.rightMargin: jumpLetters.width
anchors.right: parent.right
horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff
style: TreeViewStyle {
backgroundColor: appSettings.menuPaneColor
frame: Rectangle {
border.width: 0
}
}
}
In my legacy LibraryItemModel class, I was doing stuff like this when reading my local SQLite database:
auto loadTrack = [=] (QSqlRecord& r) -> TrackItem* {
TrackItem *trackItem = new TrackItem;
trackItem->setText(r.value(trackTitle).toString());
trackItem->setData(r.value(uri).toString(), Miam::DF_URI);
trackItem->setData(r.value(trackNumber).toString(), Miam::DF_TrackNumber);
trackItem->setData(r.value(disc).toString(), Miam::DF_DiscNumber);
trackItem->setData(r.value(trackLength).toUInt(), Miam::DF_TrackLength);
if (r.value(rating).toInt() != -1) {
trackItem->setData(r.value(rating).toInt(), Miam::DF_Rating);
}
trackItem->setData(r.value(artist).toString(), Miam::DF_Artist);
trackItem->setData(r.value(album).toString(), Miam::DF_Album);
trackItem->setData(!r.value(host).toString().isEmpty(), Miam::DF_IsRemote);
return trackItem;
};
QSqlQuery q(...);
while (q.next()) {
...
albumItem->appendRow(loadTrack(r));
}
What solutions do I have to make a multi-role Column? What else can I do? The line :
visible: libraryItemModel.data("cover")
doesn't work for instance.