You are not logged in.

1

Saturday, December 31st 2011, 4:19pm

[C++] Static private QPixmap - problem depending on QApplication

I want an Array of static Pixmaps in a class which extends QLabel.
I want some QLabels arranged in a parent widget, to easily show many pictures at one time, changing during runtime. In order to acces the hardrive only one time and saving memory, I would like to have these Pictures somehow stored as a static array, so that I can easily display new pictures by creating a QLabel and setting its pixmap to one of the pictures. (sounds confusing? look at the source code)

I tried this with QPixmap, if there is a better way for my plan, please tell me.

I tried the following (which doesn't work, but to give you an idea what I want to do. :
(I use php code tag since the highlight is better, it's ment to be C++)

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef KARTENFLAECHE_H
#define KARTENFLAECHE_H

#include  
class Kartenflaeche : public QLabel
{
    Q_OBJECT

public:
    Kartenflaeche(QWidget *parent 0);

private:
    static QPixmap Icons[3];


};

QPixmap Kartenflaeche::Icons[3] = {QPixmap("das.png"),QPixmap("das.png"),QPixmap("das.png")};



The application compiles, but the crashes since QPixmap: Must construct a QApplication before a QPaintDevice




so I tried to initialize Icons in main.cpp

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
#include 
#include "Kartenflaeche.h"



int main(int argcchar *argv[])
{
    QApplication a(argcargv);
    QWidget = new QWidget;
    QPixmap Kartenflaeche::Icons[3] = {QPixmap("das.png"),QPixmap("das.png"),QPixmap("das.png")};
    w->show();
    return a.exec();
}



which results in an compiler error

Source code

1
2
3
4
5
6
7
8
../blackjack/main.cpp: In Funktion »int main(int, char**)«:

../blackjack/main.cpp:11:35: Fehler: ungültige Verwendung des qualifizierten Namens »Kartenflaeche::Icons«
make: *** [main.o] Fehler 1
make: Leaving directory `/home/mensch/Dokumente/blackjack-build-desktop'
The process "/usr/bin/make" exited with code 2.
Error while building project blackjack (target: Desktop)
When executing build step 'Make'



the error message translated is like: Error: invalid use of the qualified name »Kartenflaeche::Icons«




How can I define a static private QPixmap, or rather is there a better way to build many QLabels sharing a static array of pictures?



Thank you for taking time to read and perhaps to awnser.

2

Saturday, December 31st 2011, 5:07pm

you cant make it static because it will ALWAYS get initialised before your scope limited QApplication.

However, I think there are better designs that will solve your problem anyway.

I would make a class with the purpose of loading those png files and providing access to them.
I would let that class be a member variable of something with a long lifetime, e.g. a MainWindow.

Any widget that wants one of those pixmaps would get it via signal/slot connection. This should be quick because qpixmap uses implicit data sharing.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Saturday, December 31st 2011, 5:45pm

you cant make it static because it will ALWAYS get initialised before your scope limited QApplication.


thank you for your reply.
In my second try, I only declare QPixmap before QAplication, i don't initialize it, do I? I try to initialize it in main, after QApplication, why doesn't that work?


However, I think there are better designs that will solve your problem anyway.

I would make a class with the purpose of loading those png files and providing access to them.
I would let that class be a member variable of something with a long lifetime, e.g. a MainWindow.

Any widget that wants one of those pixmaps would get it via signal/slot connection. This should be quick because qpixmap uses implicit data sharing.


ok, that's an Idea. Sorry I'm new to Qt. I just know signal slots in the context of connecting them. As far as I know slots and signals cannot return anything and are void? How should a widget get a Pixmap via signal/slot? Or do you mean that the signal wantaPixmap invokes the slot getaPicture() { label.setPicture(ImageHolder->givePicture(1); }


Anyway It would be very comfortable, if I could load the image data somehow static and display it somehow. I' don't need to do that with Pixmaps and Labels, if there are other ways to display image data from a static array, tell me.

This post has been edited 3 times, last edit by "Seife" (Dec 31st 2011, 5:55pm)


4

Saturday, December 31st 2011, 8:22pm

statics must be initialised in global scope. they cannot be initialised in e.g. int main(){...}
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

5

Saturday, December 31st 2011, 8:29pm

You dont really need a static, and you should be weary of using statics in general.

Here, you don't need to use a static. Using a static doesn't give you any benefit, so you shouldn't be trying to use it imo.

You can pass pixmaps from a signal to a slot quite easily e.g.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class emitter : public QObject
{
  Q_OBJECT

  QPixmap m_pix;
  void sendPixmap()
  {
    emit signalPixmap(m_pix);
  }

signals:
  void signalPixmap(QPixmap);
};

class slotter : public QObject
{
Q_OBJECT
public slots:
  void handlePixmap(QPixmap p)
  {
    // do something with p
  }
};

int main()
{
  // setup app etc
  emitter* e = new emitter();
  slotter* s = new slotter();
  QObject::connect(e, SIGNAL(signalPixmap(QPixmap)), s, SLOT(handlePixmap(QPixmap)));
}
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

6

Sunday, January 1st 2012, 2:31am

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Kartenflaeche : public QLabel
{
    Q_OBJECT

public:
    Kartenflaeche(QWidget *parent = 0);

private:
    static QPixmap* _icon;
};

QPixmap* Kartenflaeche::_icon = 0;

Kartenflaeche::Kartenflaeche(QWidget *parent)
   : QLabel(parent)
{
   if( !_icon )
      _icon = new QPixmap("das.png");
}


Pointers are typically the better option in Qt. If you are concerned about concurrency, you can use a mutex when setting _icon.

This post has been edited 1 times, last edit by "rocketman768" (Jan 1st 2012, 2:36am)


7

Sunday, January 1st 2012, 1:47pm

misread. nm.

rocket, that's just a funny looking singleton now. Might as well make a proper one in that case...
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (Jan 1st 2012, 1:57pm)