Setting an icon is not very difficult either. An icon is represented with the QIcon class. And you can create an icon provided that it has an absolute or relative path in the filesystem. I recommend providing the absolute path in this example. But for deployment considerations, you might use the relative path, or better, the resource system.
On Linux, and some other OS's, there is a convenient way to set an icon from an icon theme. It can be done by using the static method:. For example, in the screenshot at the beginning of this chapter, the smiley comes from the Oxygen KDE icon theme and was set by:.
Qt widely uses inheritance, especially in the Widgets module. The following graph shows some of these inheritances:. QObject is the most basic class in Qt. Most of classes in Qt inherit from this class. QObject provides some very powerful capabilities like:.
Widgets are able to respond to events and use parenting system and signals and slots mechanism. All widgets inherit from QObject. The most basic widget is the QWidget. QWidget contains most properties that are used to describe a window, or a widget, like position and size, mouse cursor, tooltips, etc. Remark : in Qt, a widget can also be a window.
In the previous section, we displayed a button that is a widget, but it appears directly as a window. There is no need for a "QWindow" class.
This inheritance is done in order to facilitate properties management. Shared properties like size and cursors can be used on other graphical components, and QAbstractButton provides basic properties that are shared by all buttons. Parenting system is a convenient way of dealing with objects in Qt, especially widgets. Any object that inherits from QObject can have a parent and children. This hierarchy tree makes many things convenient:. You can also note that when the application is closed, button1 , which is allocated on the stack, is deallocated.
Since button2 has button1 as a parent, it is deleted also. You can even test this in Qt Creator in the analyze section, by searching for a memory leak — there won't be any.
There is clearly no benefit in putting a button inside a button, but based on this idea, we might want to put buttons inside a container, that does not display anything. This container is simply the QWidget. Note that we create a fixed size widget that acts as a window using setFixedSize.
This method has the following signature:. Until now, we have put all of our code in the main function. This was not a problem for our simple examples, but for more and more complex applications we might want to split our code into different classes.
What is often done is to create a class that is used to display a window, and implement all the widgets that are contained in this window as attributes of this class. You can see that Qt Creator automatically generates a class template.
Notice that there are some new elements in the header :. All these elements will be explained in the next chapter, and none of them are needed now.
Implementing the window is done in the constructor. We can declare the size of the window, as well as the widgets that this window contains and their positions. For example, implementing the previous window that contains a button can be done in this way :.
Nearly all UI toolkits have a mechanism to detect a user action, and respond to this action. Some of them use callbacks , others use listeners , but basically, all of them are inspired by the observer pattern.
Observer pattern is used when an observable object wants to notify other observers objects about a state change. Here are some concrete examples:.
Observer pattern is used everywhere in GUI applications, and often leads to some boilerplate code. Qt was created with the idea of removing this boilerplate code and providing a nice and clean syntax, and the signal and slots mechanism is the answer. Instead of having observable objects and observers, and registering them, Qt provides two high level concepts: signals and slots.
Here are some examples of signals and slots from our well known QPushButton class. As you can see, their names are quite explicit. These signals are sent when the user clicked pressed then released , pressed or released the button. In order to respond to a signal, a slot must be connected to a signal. Qt provides the method QObject:: connect. If you want to get some information about what these macros do, please read the last section of this chapter. Remark : Basically, signals and slots are methods, that might or might not have arguments, but that never return anything.
While the notion of a signal as a method is unusual, a slot is actually a real method, and can be called as usual in other methods, or whilst responding to a signal. The signals and slots mechanism is useful to respond to buttons clicks, but it can do much more than that. For example, It can also be used to communicate information. Let's say while playing a song, a progress bar is needed to show how much time remains before the song is over.
A media player might have a class that is used to check the progress of the media. An instance of this class might periodically send a tick signal, with the progress value. This signal can be connected to a QProgressBar , that can be used to display the progress. The hypothetical class used to check the progress might have a signal that have this signature :.
You can see that the signal and the slot have the same kind of parameters, especially the type. If you connect a signal to a slot that does not share the same kind of parameters, when the connection is done at run-time you will get a warning like:.
This is because the signal transmits the information to the slot using the parameters. The first parameter of the signal is passed to the first one of the slot, and the same for second, third, and so forth. You may also provide the name of the variable if you want. It is actually even better. Remember our button app? Let's try to actually make something with this app, like being able to close it while clicking on the button.
At the time of the creation of this tutorial, the latest sources were Qt 5. Next, we are going to install Qt from sources. From the download page, we download the Qt5 sources.
We use the TAR file. We save ourselves some trouble. The ZIP file has Windows line endings. The command will decompress all the files to a directory qt-everywhere-opensource-src We go to the created directory. The installation is easy and straightforward, but it takes considerable time. Before we start building Qt5, we might want to install some additional libraries. For instance, if we want to connect to MySQL from Qt, we need to have libmysqld-dev installed on our system.
We install the library the classic way. On Unix systems, installation of a software is divided into three steps. First we run the configure script. The script will ask whether we want the commercial or open source edition of the Qt5 library. The script will configure the library for our machine type. This can be changed by the -prefix parameter of the configure script. Note that the installation word has two meanings here. It is the whole process consisting of all three steps.
The open source downloads can be found on the qt. For commercial use consider getting a Qt Commercial license. To do this follow these simple steps:.
Click on that and designer will switch to design mode and open up the file. You should see a blank widget. Now do this:. Do similarly for a Line Edit and place it to the right of the Label. The exact position is not important. Click on the widget background so that both of your new widgets the label and line edit get deselected. In the toolbar at the top click on the "Lay out Horizontally" button or press Ctrl-H to add all widgets to a horizontal layout.
0コメント