-1

I'm trying to create a QLabel icon with qpixmap. This is my function:

void myClass::myFunction()
{
    QPixmap on_icon(":/path");

    ui.label_1.setPixmap(on_icon);
}

My problem is I can use on_icon only in myFunction. How can I use it on different functions like :

void myClass::myOtherFunction()
{
    ui.label_2.setPixmap(on_icon);
}
bladekel
  • 37
  • 7

3 Answers3

0

As Thuga commented. One possibility is to make your QPixmap a member variable of myClass. Another way is to get the pixmap from the label wherever you need it with the QLabel::pixmap() method.

void MyClass::myFunction()
{
    QPixmap on_icon(":/path");
    ui->label_1->setPixmap(on_icon);
}

void MyClass::myOtherFunction()
{
     QPixmap *on_icon = ui->label_1->pixmap();
     ui->label_2->setPixmap(*on_icon);
}
Elvis Teixeira
  • 604
  • 4
  • 13
  • This should work if `myFunc` is called before `myOtherFunc`, otherwise there will be no pixmap in `label_1` yet. If you are sure that this is order then please past your error here. – Elvis Teixeira Apr 06 '17 at 12:57
0

QLabel has a .pixmap property which holds the current QPixmap loaded onto it, so you could pass an image straight to the label and recover it later as needed.

Another possibility is to use a pointer to QPixmap to prevent it from going out of scope.

And as others already stated, you could make QPixmap a member variable of your class.

Diego Victor de Jesus
  • 2,575
  • 2
  • 19
  • 30
-1

I solved my problem with load() Here is my codes.

in .h file

QPixmap on_icon;

in .cpp file

myClass::myClass
{
    on_icon.load(":/path");
}

void myClass::myFunction()
{
    ui->label_2->setPixmap(on_icon);
}

For the others like me to solve this problem

QPixmap on_icon(":/path")  

is equal to

 on_icon.load(":/path") 
bladekel
  • 37
  • 7