Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How can I display text (such as events) in a QML Calendar?
Forum Updated to NodeBB v4.3 + New Features

How can I display text (such as events) in a QML Calendar?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 292 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QtNewbieCoder
    wrote on last edited by
    #1

    I'm currently trying to make some sort of planner which allows the user to input data for events and have it show on the calendar. Although I'm not sure how to do that, I tried a method similar to that as of the Calendar Quick Control but can't seem to get it to work... Any help is appreciated!

    My code:
    main.qml

    import QtQuick 2.2
    import QtQuick.Controls 1.2
    import QtQuick.Controls.Private 1.0
    import QtQuick.Controls.Styles 1.1
    
    
    ApplicationWindow{
        id: applicationWindow
        visible: true
        width: 1600
        height: 1200
        minimumWidth: 400
        minimumHeight: 300
        color: "#f4f4f4"
    
        title: "Smart Planner"
    
        SystemPalette {
            id: systemPalette
        }
    
        Flow {
            id: row
            anchors.fill: parent
            anchors.margins: 20
            spacing: 10
            layoutDirection: Qt.LeftToRight
    
            Calendar{
                id: calendar
                width: parent.width
                height: (parent.height - 100)
                frameVisible: true
                weekNumbersVisible: true
                focus: true
                locale: Qt.locale()
    
                style: CalendarStyle{
                    dayDelegate: Item {
                        readonly property color sameMonthDateTextColor: "#444"
                        readonly property color selectedDateColor: Qt.platform.os === "osx" ? "#3778d0" : systemPalette.highlight
                        readonly property color selectedDateTextColor: "white"
                        readonly property color differentMonthDateTextColor: "#bbb"
                        readonly property color invalidDatecolor: "#dddddd"
    
                        Rectangle {
                            anchors.fill: parent
                            border.color: "transparent"
                            color: styleData.date !== undefined && styleData.selected ? selectedDateColor : "transparent"
                            anchors.margins: styleData.selected ? -1 : 0
                        }
    
                        Text {
                            visible: eventModel.startDate(styleData.date).length > 0
                            text: tasks.taskName
                            anchors.centerIn: parent
                            anchors.margins: -1
                            width: 12
                            height: width
                        }
    
                        Label {
                            id: dayDelegateText
                            text: styleData.date.getDate()
                            anchors.left: parent.left
                            anchors.leftMargin: 15
                            anchors.top:parent.top
                            anchors.topMargin: 8
                            font.bold: true
                            font.pointSize: 12
                            color: {
                                var color = invalidDatecolor;
                                if (styleData.valid) {
                                    // Date is within the valid range.
                                    color = styleData.visibleMonth ? sameMonthDateTextColor : differentMonthDateTextColor;
                                    if (styleData.selected) {
                                        color = selectedDateTextColor;
                                    }
                                }
                                color;
                            }
                        }
                    }
                }
            }
        }
    
    Button {
        id: button
        x: 1238
        y: 1096
        anchors.bottom: parent.bottom
        anchors.margins: 20
        width: 157
        height: 36
        text: "Add"
        anchors.right: parent.right
        anchors.rightMargin: 205
        anchors.bottomMargin: 68
    }
    
    Button {
        id: mainMenu
        x: 1423
        y: 1096
        width: 157
        height: 36
        text: "Main Menu"
        anchors.right: parent.right
        anchors.rightMargin: 20
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 68
    }
    }
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QtQml>
    #include "tasks.h"
    
    int main(int argc, char *argv[])
    {
    
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    tasks.h

    #ifndef TASKS_H
    #define TASKS_H
    
    #include <QDateTime>
    #include <QObject>
    #include <QString>
    
    class Tasks: public QObject{
        Q_OBJECT
    
    public:
    
        explicit Tasks(QObject *parent = 0);
       // Tasks(QString, QString, QString, QString);
    
        void setName(QString);
        void setInfo(QString);
        void setStartDate(QString);
        void setEndDate(QString);
    
    private:
        QString taskName;
        QString taskInfo;
        QString startDate;
        QDateTime storedSDate;
        QString endDate;
        QDateTime storedEDate;
    };
    
    
    #endif // TASKS_H
    
    

    tasks.cpp

    #include <QDateTime>
    #include <QObject>
    #include <QString>
    
    #include "tasks.h"
    
    Tasks::Tasks(QObject *parent):
        QObject(parent){}
    
    /*Tasks::Tasks(QString name = "N/A", QString info = "N/A" ,QString sDate = "00/00/0000", QString eDate = "00/00/0000"){
        taskName = name;
        taskInfo = info;
        startDate = sDate;
        endDate = eDate;
    }*/
    
    void Tasks::setName(QString name){
        taskName = name;
    }
    
    void Tasks::setInfo(QString info){
        taskInfo = info;
    }
    
    void Tasks::setStartDate(QString sdate){
        QDate storedSDate = QDate::fromString(sdate, "dd/MM/yyyy");
    }
    
    void Tasks::setEndDate(QString edate){
        QDate storedEDate = QDate::fromString(edate, "dd/MM/yyyy");
    }
    

    sqlevent.h

    #ifndef SQLEVENT_H
    #define SQLEVENT_H
    
    #include <QList>
    #include <QObject>
    #include <QDebug>
    #include <QFileInfo>
    #include <QSqlError>
    #include <QSqlTableModel>
    
    #include "tasks.h"
    
    class SqlEvent : public Tasks
    {
        Q_OBJECT
    
    public:
        SqlEvent();
    
        Q_INVOKABLE QList<QObject*> tasksSpecified(QDate storedSDate);
    
        void print();
    
    private:
    
         static void createDatabase();
         QSqlQuery query;
    };
    
    #endif // SQLEVENT_H
    

    sqlevent.cpp

    #include "sqlevent.h"
    #include "tasks.h"
    
    #include <QDebug>
    #include <QFileInfo>
    #include <QSqlError>
    #include <QSqlTableModel>
    
    void SqlEvent::SqlEvent(){
        createDatabase();
    }
    
    QList<QObject*> SqlEvent::tasksSpecified(QDate storedSDate){
        const QString queryStr = QString::fromLatin1("SELECT * FROM Event WHERE '%1' >= storedSDate AND '%1' <= storedEDate").arg(storedSDate.toString("dd/mm/yyyy"));
        QSqlQuery query(queryStr);
        if (!query.exec())
            qFatal("Query failed");
    
        QList<QObject*> tasks;
        while (query.next()) {
            Tasks *tasks = new Tasks(this);
            tasks->setName(query.value("name").toString());
            tasks->setInfo(query.value("info").toString());
            tasks->setStartDate(query.value("sdate").toString());
            tasks->setEndDate(query.value("edate").toString());
    
            tasks.append(tasks);
        }
        return tasks;
    }
    
    void SqlEvent::createDatabase(){
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(":memory:");
        if (!db.open()) {
            qFatal("Cannot open database");
            return;
        }
    
        query.prepare("INSERT INTO tasks (name, info, startDate, endDate) "
                         "VALUES (:name, :info, :sdate, :edate)");
    }
    
    void SqlEvent::print(){
           query.bindValue(":name", "TestName");
           query.bindValue(":info", "TestInfo");
           query.bindValue(":sdate", "04/02/2019");
           query.bindValue(":edate", "05/02/2019");
           query.exec();
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved