Conclusion and exercise¶
Thus, we have discussed basic principles of Qt. It is a good alternative to classic implementation of connection between component, even though it brings some overheads. QObject is a base class for almost each class in Qt. It is used to create a hyerarchical model, automatic memory management, and for realization of signal-slot model. Signals and slots help objects to communicate with each other. Signals can be connected to few slots, as well slot can be connected to few signals. Slot function is called whenever the dedicated signal is triggered.
Exercise¶
Let’s create this time a image viewer. It will show pics from a specified folder. It should has a label that will show an image and two buttons: “Next” and “Previous”, that will change the current image. Few hints on how to do it, although you can have a better idea of implementation:
- Create Qt widget application project.
- Create a class inherited from QWidget.
- Add one
QLabeland twoQPushButtonsas attributes of this class.
Note
You at look previos examples to refresh memories, or check out QPushButton and QLabel documentation to see a bit more about these classes.
- Implement a function, that would set up the path for viewer’s directory
Note
Please, check out QString class documentaion. It is a common class used for strings in Qt.
5. Implement slots, that will set next and previos image in the folder as the current pixmap for our label.
Hint
QDir is the class of choice for you. It has a function operator [] which returns the name of a file at the position specified inside the brackets, for instance:
QDir dir("/home/user");
QString fileName= dir[1];
will return the name of the second file in “/home/user directory”
- Connect signals of your buttons to dedicated slots you have created.
- Create an instance of your calss in main().
Good luck!