Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have instances of Qt's QIcon as static members of my own class?

Tags:

c++

qt

My class represents a sequence of items. There can be many instances of such sequences, but they are always displayed in the GUI as a part of a tree structure. The sequence is responsible for filling the tree with its data, and it has its own icon in the tree. Since the icon is the same for all sequences, I made it static:

class Sequence
{
public:
    Sequence() { }
    /* ... */

protected:
    QList<SeqItem *> items_;
    static const QIcon treeIcon_;
};

const QIcon Sequence::treeIcon_ = QIcon(":/icons/seq.png");

The problem is that when I run the application, it crashes with:

QPixmap: Must construct QApplication before a QPaintDevice.

Possibly because the static members are created before the main window itself. So my question is: is it possible to have QIcons as static members of my class, and if so, how?

like image 525
neuviemeporte Avatar asked Nov 15 '11 13:11

neuviemeporte


2 Answers

Maybe use static initialization in a function that you call after application initialization.

static QIcon getSeqIcon() {
    static QIcon icon = QIcon(":/icons/seq.png");
    return icon;
}

Not sure if you also need to destruct it before the application though. If you do, then maybe have a static shared pointer and manually release at application shutdown.

like image 139
Pete Avatar answered Sep 23 '22 13:09

Pete


As the error message implies, I think that not. However, you could make the treeIcon_ a static pointer, and have it initialized inside your QApplication's subclass constructor. Perhaps even better, make it a field inside your QApplication and use qApp to access it from inside Sequence

like image 38
Basile Starynkevitch Avatar answered Sep 19 '22 13:09

Basile Starynkevitch