我对 QT 相当陌生,并且正在努力获得一个基本的绘画示例。我正在创建一个游戏,我在我的主游戏控制器中实例化一个小部件。我想将它传递给多个不同的对象,以便每个对象都可以拥有自己的paintEvent 并相应地绘制其对象。(即,角色会单独绘画,风景等)
这是我的“动画对象”标题:
class Animated_object: public QWidget {
public:
Animated_object(char * _image_url, QWidget * window);
~Animated_object();
protected:
QImage * get_image();//this will return the image for this object
QRect * get_rectangle();//this will return the rectangle of coordinates for this point
protected:
virtual void paintEvent(QPaintEvent * event) = 0;
virtual void set_image(char * _image_url) = 0;
protected:
QWidget * window;
char * image_url;//this is the imageurl
QImage * image;
QRect * rectangle;
};
我的动画对象构造函数:
Animated_object::Animated_object(char * _image_url, QWidget * _window) : QWidget(_window) {....}
这是我的角色标题(角色继承自动画对象)
class Character : public Animated_object {
public:
Character(QWidget * _window);
~Character();
void operator()();//this is the operator for the character class -- this is responsible for running the character
void set_image(char * _image_url) {};
void paintEvent(QPaintEvent * event);
};
我通过向构造函数传递我的主小部件指针来实例化一个字符。所以我有另一个可以调用多个字符的类,它们都会绘制到同一个小部件(希望如此)。
我的角色paintEvent看起来像这样:
void Character::paintEvent(QPaintEvent * event) {
QPainter painter(this);//pass it in window to ensure that it is painting on the correct widget!
cout << "PAINT EVENT " << endl;
QFont font("Courier", 15, QFont::DemiBold);
QFontMetrics fm(font);
int textWidth = fm.width("Game Over");
painter.setFont(font);
painter.translate(QPoint(50, 50));
painter.drawText(10, 10, "Game Over");
}
它正在被调用,(我使用 std::cout 来测试它)但没有画任何东西......
最后,这里是调用我的主要小部件的地方。
Hill_hopper::Hill_hopper(): Game(500,500, "Hill Hopper") {
Character * character = new Character(window);
window->show();
application->exec();
}
这是游戏构造函数:
Game::Game(int _height, int _width, char * title): height(_height), width(_width) {
int counter = 0;
char ** args;
application = new QApplication(counter, args);
window = new QWidget();
desktop = QApplication::desktop();
this->set_parameters(title);
}
任何帮助将不胜感激