PDA

View Full Version : Access variables of other classes



przemek
1st August 2010, 03:04
Hello, I have some problem - I cannot use a variable from other class.
For example I have in class Setts QSpinBox SB1 and int i. I would like to check value of 'i' and set value of SB1 in method in class Pac.
I tried Setts::set_ui->SB1->setValue(1); but I got error: object missing in reference to 'Setts::set_ui'

setts.h


#include <QSpinBox>
#include "ui_setts.h>

namespace Ui {
class Setts;
}

class Setts : public QDialog {
Q_OBJECT
public:
Setts(QWidget *parent = 0);
~Setts();
Ui::Setts *set_ui;

protected:
void changeEvent(QEvent *e);

private:
QSpinBox *SB1;
int i;
}


setts.cpp


#include "setts.h"
#include "ui_setts.h"

Setts::Setts(QWidget *parent) :
QDialog(parent),
set_ui(new Ui::Setts)

{
set_ui->setupUi(this);
this->showFullScreen();
}

Thanks in advanced
Przemek

Diath
1st August 2010, 03:11
What about moving variables from private to public? :P

przemek
1st August 2010, 03:13
I tried, without results:(

Lykurg
1st August 2010, 06:51
Use getter and setter methods. Also search the forum, there are hundreds post of that topic.

przemek
1st August 2010, 13:16
Thanks Lykurg, but I have question. Is it alone way to use getter and setter methods? Is not possible to access variable without extra methods?

Zlatomir
1st August 2010, 13:20
That is the reason why you declared your variables with "private" access specifier, so they can't be accessed/modified directly, only by their "public" getter/setter functions.

LE: also notice that: Setts::set_ui->SB1->setValue(1); //most likely you tried that before constructing the actual object, set_ui is only a pointer... you construct your UI object in your class constructor... something like... set_ui = new UI_Class_Name(most likely 'this' as a parent); only after that you can do "stuff" to the UI pointer.

When you use pointers you <need to> have two(2) objects, the pointer and the actual object that is "pointed to".

Zlatomir
1st August 2010, 14:10
Sorry for double post... I edited (corrected) the last one a little bit, and a little advice

Most likely, the setter functions you will want(need) to declare as slots, so that you can connect (other objects) signals to them.

tbscope
1st August 2010, 15:24
You can also use events or signals and slots.

Zlatomir
2nd August 2010, 12:32
I attached the sample project (as i promised ;) )

I used signals and slots and created some signals and slots for keeping the ui pointers private (you can do that, or you can inherit from both the ui_generated class and QDialog, QMainWindow or QWidget) i like (and use most) the private pointer member and code what signals and slots i need.5029