Question

I have a very strange problem with QPixmap in Qt. I'm coding in C++ btw. Anyways the problem is, that as soon as I want to create a 9th QPixmap pointer in my main window class, the program crashes. so this works:

class MainWindow : public QMainWindow
{
    Q_OBJECT
    QPixmap *doorOpened, *doorClosed, *dirUp, *dirDown, *dirNone, *timePause, *timePlay, *timeStop;
     //QPixmap *doorOpen;
 public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};

and this crashes:

class MainWindow : public QMainWindow
{
    Q_OBJECT
    QPixmap *doorOpened, *doorClosed, *dirUp, *dirDown, *dirNone, *timePause, *timePlay, *timeStop;
     QPixmap *doorOpen;
 public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};

This stuff is partially done with the Qt Creator and window designer, as you might have guessed by the code.

So what am I doing wrong here that causes this strange behaviour?

Thanks in advance!

Était-ce utile?

La solution

Try initializing all of your pointers to zero first in your constructor before you initialize or access them.

Also QPixmap has a function isNull() . This can be useful for checking for errors with them.

Also if you use the default constructor (doorOpen = new QPixmap();) for a pixmap and then call load() with the filename you want to use, you are able to check the return value of load() to perform error checking.

http://qt-project.org/doc/qt-4.8/qpixmap.html#isNull

http://qt-project.org/doc/qt-4.8/qpixmap.html#load

Hope that helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top