我想使用Qt5 / qml使用月中的几天创建类似于水平线的日历。使用行数和列数均计为一个月的QAbstractTableModel是否可行。有许多实现为垂直列表(ListView)的示例。我将感谢同样实现的例子。
**Add as qquickwidget in my programm**
engine = new QQmlEngine(this);
quickWidget = new QQuickWidget(engine, this);
quickWidget->setSource(QUrl("qrc:/qml/calendar.qml"));
QQmlContext *context = quickWidget->rootContext();
dayViewModel = new DayViewModel(context);
context->setContextProperty("dayViewModel", dayViewModel);
qmlRegisterType<DayViewModel>("dayViewModel", 1, 0, "dayViewModel");
topLayout->addWidget(quickWidget);
topWidget->setLayout(topLayout);
dayviewmodel.cpp
DayViewModel::DayViewModel(QObject *parent)
: QAbstractTableModel(parent)
{
monthTile = NULL;
}
void DayViewModel::setNumberOfDays(CalendarMonthTile* tile)
{
monthTile = tile;
}
QHash<int, QByteArray> DayViewModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[TextRole] = "TextRole";
return roles;
}
int DayViewModel::rowCount (const QModelIndex &/*parent*/) const
{
return 1;
}
int DayViewModel::columnCount (const QModelIndex &/*parent*/) const
{
return 31;
}
QVariant DayViewModel::data (const QModelIndex & index, int role) const
{
switch (role) {
case TextRole:
return QString("%1").arg(index.column());
}
return QVariant();
}
.qml文件 take from here
TableView {
anchors.fill: parent
columnSpacing: 1
rowSpacing: 1
clip: true
model: dayViewModel
delegate: Rectangle { ????
implicitWidth: 100
implicitHeight: 50
Text {
text: display
}
}
}
我不知道如何将模型公开给qml?