JonMorehouse
JonMorehouse

Reputation: 1383

QT Widget Pointer

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)

Here's my 'animated object' header:

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;

};

My animated object constructer:

Animated_object::Animated_object(char * _image_url, QWidget * _window) : QWidget(_window) {....}

and here is my character header (character inherits from animated_object)

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);

};

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).

My character paintEvent looks like this:

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");

}

It is being called, (I used the std::cout to test that) but is not painting anything...

Finally, here is where my main widget is called.

Hill_hopper::Hill_hopper(): Game(500,500, "Hill Hopper") {

    Character * character = new Character(window);

    window->show();
    application->exec();


}

And here's the game constructor:

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);

}

Any help would be greatly appreciated

Upvotes: 0

Views: 1626

Answers (2)

hyde
hyde

Reputation: 62908

Looks like you are missing Q_OBJECT macro in your headers. Though that might not be the problem if it gets called anyway.

In any case, I suggest you use Qt Creator for creating new classes, it will create the .h and the .cpp file skeletons for you, avoiding forgetting stuff like this.

For a fast-updating game, you should probably have just one game area widget, where you draw everything moving. If you are drawing only QPixMaps (no direct text or line drawing, turn pieces of text to QPixmaps first), then just turning widget to QGLWidget will make rotating and scaling QPixMap "sprites" very fast without needing to write any OpenGL code yourself .But if the suggested QGraphicsView is fast enough, then it will do a lot if work for you, you should experiment with that first.

Upvotes: 1

Patrice Bernassola
Patrice Bernassola

Reputation: 14446

You should use QGraphicsView widget which is design for this kind of think. In the scene (QGraphicsScene) of the QGraphicsView you can directly add widgets. The build-in system will manage your widgets and fire the paint event when needed. In addition you have a lot of useful functionnality to find, move, etc. your widgets.

Upvotes: 0

Related Questions