This code places an Elementary segment control widgets on a window, to exemplify part of the widget's API.
Let's start adding a segment control to our window:
Now will add an item only with label:
Really simple. To add an item with only an icon, the icon needs to be created first, them added with this same function:
If an item with label and icon is required, it can be done as well. In this case, instead of a label (or icon) centered, the item will display an icon at left and the label at right:
But, if you need to add some items that can have or not a label, but want that all of them looks the same way, with icon at left, just add an empty string label. It's done on our example to illustrate that:
So far, all the item were added to the last position of the widget, but if something different is required, it can be done using another insertion function. Let's suppose we want to put an item just before the last item:
There are two ways to delete items. Using the item handle, like:
Or using item's index:
To set properties of an item already added to the widget, you just need to get the item and set icon or label, as the following code shows:
Finally, it's possible to select an item from the code, and also get the selected item. We will select the item at the center of the widget and print its position.
See the full example, whose window should look like this picture: