This forum has been archived. All content is frozen. Please use KDE Discuss instead.

QML plasmoid: How to show only the day of the month

Tags: None
(comma "," separated)
kdea
Registered Member
Posts
23
Karma
0
Hi, I'm starting a simple (but missing) plasmoid: Show current day of month at the panel.

To do it, I'm learning QML. After reading some tutorials and a lot of try&error, I've got the plasmoid to show current date&time, and it's refreshed on real time. But I can't get it to show only the day of the month.

This is the "main.qml" I'm using:

Code: Select all
import QtQuick 2.0
import QtQuick.Layouts 1.1
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
 
Item {
    Plasmoid.compactRepresentation: CompactRepresentation {}
    // Plasmoid.fullRepresentation: FullRepresentation {}
}



This is the "CompactRepresentation.qml" that works (shows full date and time):

Code: Select all
import QtQuick 2.0
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.plasma.core 2.0 as PlasmaCore

Item {
        PlasmaCore.DataSource {
                id: dataSource
                engine: "time"
                connectedSources: ["Local"]
                interval: 500
                onNewData:{
                        local.text = data.Local.DateTime
                }
        }
        PlasmaComponents.Label {
            id: local
            text: dataSource.data.Local.DateTime
        }
}


And this is the "CompactRepresentation.qml" that doesn't work (should show only the day of the month):

Code: Select all
import QtQuick 2.0
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.plasma.core 2.0 as PlasmaCore

Item {
        PlasmaCore.DataSource {
                id: dataSource
                engine: "time"
                connectedSources: ["Local"]
                interval: 500
                onNewData:{
                        local.text = data.Local.DateTime.date().day()
                }
        }
        PlasmaComponents.Label {
            id: local
            text: dataSource.data.Local.DateTime.date().day()
        }
}


If run, it shows an empty plasmoid.

In theory, the type of "data.Local.DateTime" is QDateTime, so it should be possible to access to its "date()" method, that returns a QDate type, and it has a "day()" method, that returns an int type. So what's wrong? Any suggestion to fix it?

Thank you
User avatar
anda_skoa
KDE Developer
Posts
783
Karma
4
OS
Maybe use Qt.formatDate()?

Cheers,
_


anda_skoa, proud to be a member of KDE forums since 2008-Oct.
kdea
Registered Member
Posts
23
Karma
0
Thank you, it worked. This is my final file "CompactRepresentation.qml":

Code: Select all
    import QtQuick 2.0
    import org.kde.plasma.components 2.0 as PlasmaComponents
    import org.kde.plasma.core 2.0 as PlasmaCore
     
    Item
    {
        PlasmaCore.DataSource
        {
            id:                 time_src
            engine:             "time"
            connectedSources:   ["Local"]
            interval:           1000            // 1 sec (period to refresh)
        }
       
        PlasmaComponents.Label
        {
            id:     date_label
            text:   Qt.formatDate( time_src.data.Local.DateTime, "d" )
        }
    }


And this is an example of my panel ("13" is the plasmoid):

Image

I'm sharing the plasmoid (file "day_of_month.zip"), for those who would like to have it in their panel:

day_of_month.zip

To install it, just download the file "day_of_month.zip" to some folder in your harddisk, open a shell, go to the folder and run:

plasmapkg2 --install day_of_month.zip

Then rigth-click on your kde panel, select "Add graphical elements", search for
"Day of month", and double-click on the item found. The plasmoid will be added to your panel. Move it to your favourite place (perhaps near to the clock).


Bookmarks



Who is online

Registered users: abc72656, Bing [Bot], daret, Google [Bot], Sogou [Bot], Yahoo [Bot]