Panegyric

Standard

It means an expression/article of praise. Its’ been a while since I have updated my blog. I’m happy actually. Not that rainbows and butterfly kind of happy but life-is-going-good happy. I have a social circle and they talk to me too, which is nice. As per social conventions, you should say good things about people who have a positive impact on you. I have 4+1 elements in this set of human entities and in this panegyric I’ll try and describe these 4+1 elements.

At work, I have nothing much to do so I was working on Qt/QML. Qt is to C++ what Android is to Java. Like Android, Qt is not pure C++ and rather extends C++ and makes it more awesome (as if C++ needed to be any more awesome). QML is a declarative language that is used to design UIs and together with Qt, one can create powerful desktop applications independent of platform. A friend asked me for a comprehensive tutorial and I’ll try to elucidate a sample music player application I made in Qt/QML. It’s nothing much really but it does implement concepts of models, views, delegates and exposing variables from C++ code to QML. You can download the code here and must have the latest updated version of Qt Creator.

So I have 4+1 elements in this set of human entities. 4 of us are in a group and we talk on this messaging application where they laugh and respond or simply respond. The other element is whom this article is meant for. The Guy and I discuss not about piffling things such as human dealings (a.k.a bitching) and talk about better things such as world happenings and imaginative ideas that may not happen in real life. This one is rathe for new ideas, adventures and experiences and values friendship. This one thinks that I’m erudite so I assume that there is something wrong with this one. And as a matter of fact, there is. This one has his own world and is lost most of the time in that world. It would be condescending to say that The Guy should adjust the volume of his voice but its’ ok. At least he speaks not shit like most other humans do.

Sweet P is sweet. This one is happy go lucky and has an infectious smile. This one is artistic and sees a world full of colours and possibilities. This one also yearns for new experiences and has a lot to see and do. This one too thinks that I’m erudite so there must be something wrong with her too. And there is. This one is too happy. I wish that this one gets to see the fucked up side of life soon. One becomes a true sage when they experience downward spiral as zealously as upward spiral.

Open Qt Creator and start a new Qt Quick application. Like most languages today, it scaffolds a basic skeleton for your application. A “main.cpp” file and a “main.qml” file. The CPP file is the controller and QML is the application UI. I added a “player.h” file that defines a class for the music player and a “player.cpp” that defines the controller logic for the music player. If you run the application, it will show a menubar with buttons on the bottom and a “load area” on top. The “load area” is where the UI of the music player will be loaded on click. QML looks like CSS on steroids and if you have an understanding of CSS its’ easy to grasp QML quickly.

QML is used to define “elements” of a UI. Every element has an ID and we refer to an element by its ID. Qt Creator is called so because you can create a UI by dragging and dropping (and editing properties) an element in the Creator window. If you see my “main.qml” file, there is a Loader element. This is the “load area” and we can load an external QML directly into this load are with the Loader element. To make an element clickable, we define, within that element, a mouse area. Mouse area defines an “onClicked” signal which is used to define the function of the element if it is clicked.

Loader {
 id: contentLoader
 x: 1
 y: 0
 width: 1023
 height: 324
}
MouseArea {
 id: mouseArea2
 anchors.fill: parent
 onClicked: {
     contentLoader.source = "mediaPlayer.qml"
 }
}

The important and confusing part, like margins in CSS, are anchors. You must read about anchors yourself but it basically means how an element is positioned relative to its’ parent.

Coming to “mediaPlayer.qml”, it looks much the same as “main.qml”. The focus areas are “FolderListModel”, ListView and MediaPlayer. In QML and in general, data is cast into a model. Model is how your data is defined. In this case, we are going to access a folder pointed to by the “musicUrl” variable (we will come back to this). A Model is used by a View to format the data you’re accessing. In this case, we are formatting it in a List and hence the ListView. A View uses a delegate. A Delegate defines how each element in a data set is rendered. In our case, each song in our music folder is enclosed in a clickable rectangle. If you click on it, it will start playing that song. We are using QtMultimedia library to play songs and it defines a MediaPlayer element. You can check out the available parameters of MediaPlayer and QtMultimedia library in the links. Here, we update the “source” parameter of the MediaPlayer element when we click on a song shown in the list. The available parameters of MediaPlayer are used further down the code for example: mediaPlayer.metaData.title and mediaPlayer.metaData.author.

 FolderListModel {
   id: folderListModel
   folder: musicUrl // TODO: write C++ Controller. Done
   nameFilters: [qsTr("*.mp3")]
   showDirs: false
 }
 ListView {
   id: playList
   x: 9
   y: 8
   width: 261
   height: 308
   spacing: 5
   flickDeceleration: 1000
   model: folderListModel
   delegate: Rectangle {
     x: 23
     y: 21
     width: 234
     height: 56
     gradient: Gradient {
         GradientStop {
         position: 0
         color: "#ffffff"
     }
     GradientStop {
         position: 0.881
         color: "#ae5151"
     }
  }
 Text {
   id: song
   text: model.fileName
   anchors.fill: parent
   verticalAlignment: Text.AlignVCenter
   horizontalAlignment: Text.AlignHCenter
   font.pixelSize: 12
   wrapMode: Text.WordWrap
   MouseArea {
     anchors.fill: parent
     onClicked: {
         mediaPlayer.source = model.fileURL
     }
   }
  }
 }
}

The third element of the set of human entities is Soni. This one is our darling. We all revere this one and look up to this one. This one is quite mature and emotionally and mentally stable. This one seldom thinks that I’m erudite so there is not much wrong with her. This one is a bit haughty and I feel lucky that I’m in talking terms with this one. Lucky because she is sorted enough to filter out obtuse humans out of her life. This one has experienced much and knows how to hold her composure and stature in direst of situations. This one is the coolest human entity I have come across till now. Right now, this one is globetrotting.

Yanka is not in the messaging group but the technical aspect of this article is meant for this one. This one is one hap chick. This one has a flamboyant style statement. This one is amiable and if you ask this one for some time, she will give you time. This one will sit and listen to you and talk to you. Although this one speaks almost 100 words within a minute but you can slow it down and process it mentally if you aren’t distracted by that sparkling smile. This one thinks that I’m intelligent but I give her that… not her fault, this one doesn’t know me much. Which is good for this one’s mental well being.

Lady D. This one is awesome. This one thinks I’m a fucking idiot and hence there is nothing wrong with this one. This one is different. Out of the pack. I can relate with this one and I like to think that this one and I have same mental frequencies. This one is funny, intelligent… pretty. A whole package just to take your breath away. This one is a bit narcissistic but that’s alright. It suits this one’s persona. You can sit with this one and observe her without getting involved and you won’t be bored. This one has an active (if not over active) mind and cannot sit still. I find many a similarity between this one and myself. I’m mentally (over) active and cannot sit still either. This one is striving to be mentally and emotionally stable like Soni and will eventually get there. This one has naught capacity for bullshit. This one forms clear opinions and characterises deliberately and clinically which I find utterly cool. This one thinks that she is all been there and done that but the only experience that she has is of that one city which was her first home away from home. Given all her imperfections though, this one is perfect in every way. If I ever decide to take a chance with some human, this one would be the one.

Coming to the “player.h” file, here we define a class that will eventually help us get the songs out of our directory and into the list model. In Qt, every class must extend the QObject class. I won’t go into the nitty gritty pieces of this class and directly jump to the implementation of the function definitions. QStandardPaths define all the standard paths in the OS for example, all the paths in the $PATH variable in a terminal are defined in this header file. In “player.cpp”, we extract the standard path to our music folder in the constructor of class “player”.

player::player(QObject *parent) : QObject(parent)
{
     musicPaths = QStandardPaths::standardLocations(QStandardPaths::MusicLocation);
     musicUrl = QUrl::fromLocalFile(musicPaths.isEmpty() ? QDir::homePath() : musicPaths.first());
}

The musicUrl variable is set in the controller as shown above. We must expose this variable somehow to QML so that it is accessible in QML. To do this, we add this variable to the root context of the main application engine.

engine.rootContext()->setContextProperty(QStringLiteral("musicUrl"), p.getMusicUrl());

This piece of wonder basically allows us to execute a function defined in our controller whenever the said variable is referred to in the QML. This is pretty fucking cool.

Well, this panegyric comes to an end. These 4+1 elements of this set of human entities are awesome. All opinions presented here are a solely the authors perspective. It is not the author’s intention to degrade anybody but if you feel degraded, feel free to take out time and jump off of a cliff.

Yours truly,
Ninja.

Advertisements

One thought on “Panegyric

  1. Its such as you learn my mind! You seem to know a lot about this,
    like you wrote the e book in it or something.
    I feel that you simply could do with a few % to force the message house a bit, but instead of that, that is wonderful blog.

    A great read. I’ll certainly be back.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s