I don't quite understand how I can name my columns, and how I can change their type. For example, output in the first, a float in the second, and a QDataType in the third.
However, in the third one, I want to apply formatting. How do I tell the delegate that it is in the 3rd column, and it needs to apply date formatting?
The examples use TableModelColumn
, but do not use the model class from QAbstractListModel:
TableView {
id: statisticsTable
model: WD_RX
interactive: false
delegate: Rectangle {
id: delegate
implicitWidth: 70
implicitHeight: 20
Text {
text: display
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
}
}
QVariant WeatherDataReceiver::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
if (index.column() == 0)
return index.row();
if (index.column() == 1)
return QString::number(/*some float*/, 'f', 1);
if (index.column() == 2)
return QVariant(/*some QDataTime*/);
}
return QVariant(0);
}
I use Context
to register the type.
main.cpp:
WeatherDataReceiver wdr;
engine.rootContext()->setContextProperty("WD_RX", &wdr);
I want to sign the columns and apply Qt.formatTime(/*some date*/, "hh:mm")
to the third column.
My only solution was a condition on the type of the returned value, but I think this is not a very good solution, but also the naming of the column is left in question:
TableView {
id: statisticsTable
model: WD_RX
interactive: false
delegate: Rectangle {
id: delegate
implicitWidth: 70
implicitHeight: 20
Text {
text: {
if (getType(display) !== 'object')
return display
else
return Qt.formatTime(display, "hh:mm")
}
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
}
}
To apply different a front-end to each column, you have to use DelegateChooser
. Then, you will be able to specify the index of the column you're working on.
And for giving them a title, here's an example of how you can implement the headerData
function:
In your model.h
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
In your model.cpp
QVariant ModelTransactions::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal)
{
switch (section)
{
case 0:
return "Client";
case 1:
return "Document";
case 2:
return "Référence client";
case 3:
return "Type transaction";
case 4:
return "Opérateur";
case 5:
return "Montant";
case 6:
return "Référence bénéficiaire";
case 7:
return "Date";
case 8:
return "Token";
case 9:
return "Caissier";
case 10:
return "idTransaction";
default:
return QVariant ();
}
}
else if (orientation == Qt::Vertical)
return section + 1;
return QVariant ();
}
In the if
part, you can give a name to each column. In the else
you can give a number to each row, or a name if you find it usefull.
In your qml file, you should add a HorizontalHederView
(also VerticialHeaderView
) item to customize your titles:
HorizontalHeaderView
{
id: hHeaderView
syncView: tableView // give a id to your tableview and then paste it here
width: parent.width
height: parent.height / 10
delegate: Button
{ // replace cellWidth and cellHeight by value of your choice
implicitWidth: cellWidth
implicitHeight: cellHeight
width: cellWidth
height: cellHeight
background: Rectangle
{
anchors.fill: parent
color: Qt.rgba (0.0, 0.0, 1.0, 0.5)
}
contentItem: RowLayout
{
anchors.fill: parent
Text
{
text: model.display
color: "white"
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
}
}
}
}