PDA

View Full Version : Access form's widget's in other class



p3c0
12th June 2010, 13:13
Hello,

i have a form manager.ui whose cpp file manager.cpp is as follows,


#include "manager.h"
#include "ui_manager.h"

manager::manager(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::manager)
{
ui->setupUi(this);
}


manager::~manager()
{
delete ui;
}

void manager::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void manager::on_PB_General_clicked()
{
GetSystem_General();
}


This GetSystem_General() is in file applysettings.cpp file as folows,


#include "applysettings.h"
#include "ui_manager.h"
#include "../extras/Connections.h"


void GetSystem_General()
{
//Here i want to access the manager class's widget QLabel for e.g LB_Cpu and set its text using setText method like,
ui->LB_Cpu->setText("intel");
}

so my question is how to access the manager class widgets in this applysettings.cpp.

Lykurg
12th June 2010, 13:33
so my question is how to access the manager class widgets in this applysettings.cpp.Normaly you shouldn't do that. better use getter and setter methods or return a value from GetSystem_General() and set it to your line edit inside your class manager. But if you really whant to change your gui in that other function, just pass a pointer to your manager class to GetSystem_General().

p3c0
12th June 2010, 13:45
Thanks for reply Lykurg,
but the applysettings.cpp which contains GetSystem_General() is fully implemented. Restricted to use the getter setter methods.
what pointer i have to pass to GetSystem_General() ?
can you give an example

Zlatomir
12th June 2010, 14:20
I strongly recommend that you follow one of the first two recommendation that Lykurg gave you.

The first is way to go, so use getter and setter functions for manager class (to access/set/modify the ui components)

The pointer passing "stuff" won't be able to access the ui pointer (because is default private) and i don't recommend to make it public for this reason (when is another way to solve the problem, keep that pointer private ;) )