Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a requirement that I need to access ui from another class. I have to assign a png to a label from that class. For this I have created a function in my widget.cpp to assign the png to the label. And from another class(ApplicationWindow) I have created a widget class object and through that object I am calling widget class function. Here the problem is I am able to access the widget class fucntion but unable to assign the png to label. Can you please provide the solution for this.

What I have tried:

Widget.cpp

void Widget::Display_image()
{
qDebug()<<"In Display function";
QPixmap pixmap("/root/rfid.png");
ui->label_image->setPixmap(pixmap);
ui->label_image->setScaledContents( true );
ui->label_image->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );

}


ApplicationWindow.cpp


void ApplicationWindow::closeApp()
{
Widget obj; // from here I need to access ui
obj.Display_image();

}

Here Iam able to print "In Display function" with qDebug but label not displaying image.
Posted
Updated 27-Mar-19 21:35pm

1 solution

You need to access the correct object like a member from the ApplicationWindow. Your Widget is a local object and gets destroyed after leaving the function.

Using some pointers often helps.
 
Share this answer
 
Comments
Member 13740197 24-Apr-19 1:43am    
Hi
After using pointers also I am unable to access ui. Below is which I have used.

Widget *obj;
obj->Display_image();

also tried below

Widget *obj = new Widget();
obj->Display_image();

Can you please provide me the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900