- Game Programming Using Qt Beginner's Guide
- Witold Wysota Lorenz Haas
- 263字
- 2021-08-20 10:45:16
Time for action – adding properties to the board class
In this exercise, we will be adding a useful property to the board class. The property is going to hold information about the player who should make the next move. The type of the property is going to be the TicTacToeWidget::Player
enumeration that we created earlier. For the getter and the setter methods, we are going to use the two functions that we created earlier: currentPlayer()
and setCurrentPlayer()
.
Open the header file for our class and modify the class definition as shown in the following code:
class TicTacToeWidget : public QWidget { Q_OBJECT Q_ENUMS(Player) Q_PROPERTY(Player currentPlayer READ currentPlayer WRITE setCurrentPlayer NOTIFY currentPlayerChanged) public: enum Player { Invalid, Player1, Player2, Draw };
What just happened?
Since we want to use an enumeration as a type of a property, we have to inform Qt's meta-object system about the enum. This is done with the Q_ENUMS
macro. Then, we declare a property called currentPlayer
and mark our two existing methods as getter and setter for the property. We also use the NOTIFY
keyword to mark currentPlayerChanged
as a signal that is sent to inform about a change in the value of the property. We won't be using this extra information in our small game, and we don't require currentPlayer
to be a property at all, but it is always a good idea to try and find good candidates for properties and expose them because some day, someone might want to use our class in a way we hadn't predicted and a particular property might become useful.
- 軟件項(xiàng)目估算
- Python科學(xué)計(jì)算(第2版)
- Learning Spring 5.0
- SQL Server 2012數(shù)據(jù)庫技術(shù)及應(yīng)用(微課版·第5版)
- R Deep Learning Cookbook
- 零基礎(chǔ)輕松學(xué)SQL Server 2016
- 碼上行動:用ChatGPT學(xué)會Python編程
- Creating Stunning Dashboards with QlikView
- C/C++程序員面試指南
- Scala Reactive Programming
- Vue.js應(yīng)用測試
- 零基礎(chǔ)學(xué)Scratch 3.0編程
- Scratch從入門到精通
- 計(jì)算語言學(xué)導(dǎo)論
- 你好!Python