如何在qt C ++中从另一个类访问ui [英] How to access ui from another class in qt C++
问题描述
我要求我需要从其他班级访问ui。我必须为该类的标签指定一个png。为此,我在widget.cpp中创建了一个函数,将png分配给标签。从另一个类(ApplicationWindow)我创建了一个widget类对象,通过该对象我调用了widget类函数。这里的问题是我能够访问widget类fucntion但无法将png分配给label。能否为您提供解决方案。
我的尝试:
Widget.cpp
void Widget :: Display_image()
{
qDebug()< ;<在显示功能中;
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
无效ApplicationWindow :: closeApp()
{
Widget obj; //从这里我需要访问ui
obj.Display_image();
}
这里我可以使用qDebug打印In Display function,但标签不显示图像。
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.
推荐答案
你需要访问正确的对象,如ApplicationWindow的成员。你的Widget是一个本地对象,在离开函数后会被销毁。
使用一些指针通常有帮助。
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.
这篇关于如何在qt C ++中从另一个类访问ui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!