Note that there are some explanatory texts on larger screens.

plurals
  1. POQT Widget Pointer
    primarykey
    data
    text
    <p>I'm fairly new to QT and am struggling getting a basic painting example to work. I am creating a game where I instantiate a widget in my main game controller. I want to pass this to multiple different objects so that each object can have its own paintEvent and paint its objects accordingly. (IE, the character would paint seperately, the scenery etc etc)</p> <p>Here's my 'animated object' header:</p> <pre><code>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; }; </code></pre> <p>My animated object constructer:</p> <pre><code>Animated_object::Animated_object(char * _image_url, QWidget * _window) : QWidget(_window) {....} </code></pre> <p>and here is my character header (character inherits from animated_object)</p> <pre><code>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); }; </code></pre> <p>I instantiate a character by passing the constructor my main widget pointer. So I have another class that can call multiple characters and they would all paint to the same widget (hopefully).</p> <p>My character paintEvent looks like this:</p> <pre><code>void Character::paintEvent(QPaintEvent * event) { QPainter painter(this);//pass it in window to ensure that it is painting on the correct widget! cout &lt;&lt; "PAINT EVENT " &lt;&lt; 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"); } </code></pre> <p>It is being called, (I used the std::cout to test that) but is not painting anything...</p> <p>Finally, here is where my main widget is called.</p> <pre><code>Hill_hopper::Hill_hopper(): Game(500,500, "Hill Hopper") { Character * character = new Character(window); window-&gt;show(); application-&gt;exec(); } </code></pre> <p>And here's the game constructor:</p> <pre><code>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-&gt;set_parameters(title); } </code></pre> <p>Any help would be greatly appreciated</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload