Search code examples
c++qtqpainterqpixmap

How to declare and instantiate a variable in Qt?


At first I want to apologize for this stupid question, but I'm fairly new to Qt, so forgive me ;)

I'm drawing QPixmaps. But for performance issues I only want to load my QPixmaps once to use them over and over again. At the moment I'm loading them always when update is called, but it should be possible to load them only once in the constructor. How would the .h file and the declaration of the QPixmap variables look in this case? Couldn't figure it out so far.


Solution

  • Qt is a C++ library, so you just write c++ code:

    Header:

    class foo : public QWidget {
    public:
        foo(QWidget *parent = 0);
    
    private:
        QPixMap *bar;
    };
    

    Source:

    foo::foo(QWidget *parent) : QWidget(parent) {
        bar = new QPixMap("bar.png");
        // Some error checking...
    }