PDA

View Full Version : Accessing the UI ! Help !



fitzy
28th November 2009, 21:36
Hi there !

I am trying to change the value of a QProgressBar from a class named anotherClass (not from my gui class, which is guiClass).

It builds, but the resulting build crashes ! :mad:

Here is the error in QT Creator :


... exited with code -1073741819

=> -1073741819 indicates an access violation in windows. An access violation means your process has tried to access memory (ie dereference a pointer) that does not belong to it.


Here is my code :


class guiClass : public QMainWindow
{
Q_OBJECT

public:
guiClass(QWidget *parent = 0);
~guiClass
Ui::guiClass *ui;

};

class anotherClass : public QProgressBar {

Q_OBJECT

public:
anotherClass(QObject* parent);
void anything();
guiClass *guiClass;

};



void anotherClass::anything(){
this->guiClass->ui->progressBar->setValue(80); // THIS IS THE WRONG PART
}

Lykurg
28th November 2009, 21:43
Did you initialize your ui and did you set the pointer guiClass right? Can we see the implementation?

EDIT: why do you don't use signals and slots? Any particular reasons for that. Because your case seems to be classical case for using signals.

squidge
28th November 2009, 22:01
Put a BP on the "wrong part" and check the values of guiclass and ui.

You do initialise guiClass in anotherClass don't you?

fitzy
28th November 2009, 22:50
.
.
Thank you !


Did you initialize your ui and did you set the pointer guiClass right? Can we see the implementation?


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


//this->ui->progressBar->setValue(80); // IT WORKS, but it is in guiClass

anotherClass *anotherclass = new anotherClass(this);
anotherclass->anything();

}

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

@Lykurg : I don't use SIGNALS and SLOTS because I don't know how to use them ;)
Moreover, I'd prefer to do it without signals and slots.

@fatjuicymole : what is a BP ?

PS: I'm a beginner.

squidge
28th November 2009, 23:15
BP = Breakpoint

Where do you initialise guiClass in anotherClass?

faldzip
29th November 2009, 00:04
I don't use SIGNALS and SLOTS because I don't know how to use them ;)

So maybe it is time to learn how to use them. And why you would like not to use signals and slots?

Lykurg
29th November 2009, 07:26
guiClass ::guiClass (QWidget *parent)
: QMainWindow(parent), ui(new Ui::guiClass )
{
ui->setupUi(this);


//this->ui->progressBar->setValue(80); // IT WORKS, but it is in guiClass

anotherClass *anotherclass = new anotherClass(this);
anotherclass->anything();

}

I knew it! you haven't set the pointer guiClass in anotherClass. So you accessing a null pointer!


class anotherClass : public QProgressBar {

Q_OBJECT

public:
anotherClass(QObject* parent);
void anything();
guiClass *guiClass;
void setBackPointer(guiClass *p) {guiClass = p;} // <- add

};

// and then

guiClass ::guiClass (QWidget *parent)
: QMainWindow(parent), ui(new Ui::guiClass )
{
ui->setupUi(this);
anotherClass *anotherclass = new anotherClass(this);
anotherclass->setBackPointer(this); // <- add
anotherclass->anything();
}

you can also use the parent of your constructor for setting the back pointer, but you really should learn signal and sots...

fitzy
29th November 2009, 09:13
You're right !
I didn't set the pointer guiClass in anotherClass.

Thank you so much !