PDA

View Full Version : making a class aware of the gui



aegis
2nd April 2007, 17:26
hi,

i have a form design that i implement using the multiple inheritance approach, so i have a class {i name it gui} that represents my form



class gui : public QDialog, private Ui::mainFormGUI
{
Q_OBJECT

//the constructor
public:
gui (QWidget *parent = 0);

//the buttons of the functions....
private slots:

private:
typeA test;
};



the problem is that i want to make typeA access the gui interface....{for example write on a QTextEdit..... How can i do this?

i thought that i could pass to the typeA class a {this}pointer of gui {i.e. gui* p_gui} and then access the QTextEdit from p_gui but unfortunatelly it doesn't work....

any other ideas?

thanks in advance ,
N.A

marcel
2nd April 2007, 17:49
Is the testA object in another thread. Because if it is, then you will need some synchronization.

To access a QTextEdit from the interface, you could add public wrapper functions in the gui class for the QTextEdit members that you need ( because the text edit is private in the gui, you cannot access it from the testA object ).

For example, for the setText method:

void gui::setText( QString text )
{

mTextEdit->setText( text );
}

Another solution would be to change the inheritance from the UI from private to public.

Hope this helps.

Marcel.

Brandybuck
2nd April 2007, 18:53
If I understand your question correctly, then make typeA a friend of class gui.

aegis
2nd April 2007, 19:30
thank you all for your answers
unfortunatelly nothing worked....

caseChangerClass.h


#ifndef caseChangerClass_H
#define caseChangerClass_H

#include "ui_caseChanger.h"
#include "classA.h"

#include <QDialog>
#include <QMenu>


//class caseChangerClass : public QDialog, private Ui::mainFormCaseChanger
class caseChangerClass : public QDialog, private Ui::mainFormCaseChanger
{
Q_OBJECT

//the constructor
public:
caseChangerClass(QWidget *parent = 0);


private:
typeA test;

public: void caseChangerClass::setText( QString text );

};

#endif



caseChangerClass.cpp


#include "caseChangerClass.h"

#include <QtGui>
#include <QString>

caseChangerClass::caseChangerClass(QWidget* parent):QDialog(parent),test(this)
{

setupUi(this);


}

void caseChangerClass::setText( QString text )
{

textEdit->setText( text );

}


classA.h


#ifndef classA_H
#define classA_H

#include "caseChangerClass.h"

class caseChangerClass;

class typeA
{


//the constructor
public:
typeA(caseChangerClass* gui);
~typeA();

caseChangerClass* m_gui;

};

#endif


classA.cpp


#include "classA.h"

typeA::typeA(caseChangerClass* gui);
{

m_gui = gui;

//this is what i tryied to do earlier....
//m_gui->textEdit->append("hi!");

gui->setText( "HI" );

}


main.cpp


#include <QApplication>

#include "caseChangerClass.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
caseChangerClass myApp;
myApp.setWindowFlags(Qt::WindowStaysOnTopHint);
myApp.show();
return app.exec();
}


ui_caseChanger.h


/************************************************** ******************************
** Form generated from reading ui file 'caseChanger.ui'
**
** Created: Mon 2. Apr 20:23:18 2007
** by: Qt User Interface Compiler version 4.2.2
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
************************************************** ******************************/

#ifndef UI_CASECHANGER_H
#define UI_CASECHANGER_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QTextEdit>
#include <QtGui/QWidget>

class Ui_mainFormCaseChanger
{
public:
QTextEdit *textEdit;

void setupUi(QWidget *mainFormCaseChanger)
{
mainFormCaseChanger->setObjectName(QString::fromUtf8("mainFormCaseChanger"));
textEdit = new QTextEdit(mainFormCaseChanger);
textEdit->setObjectName(QString::fromUtf8("textEdit"));
textEdit->setGeometry(QRect(80, 20, 251, 171));

retranslateUi(mainFormCaseChanger);

QSize size(444, 207);
size = size.expandedTo(mainFormCaseChanger->minimumSizeHint());
mainFormCaseChanger->resize(size);


QMetaObject::connectSlotsByName(mainFormCaseChange r);
} // setupUi

void retranslateUi(QWidget *mainFormCaseChanger)
{
mainFormCaseChanger->setWindowTitle(QApplication::translate("mainFormCaseChanger", "test", 0, QApplication::UnicodeUTF8));
Q_UNUSED(mainFormCaseChanger);
} // retranslateUi

};

namespace Ui {
class mainFormCaseChanger: public Ui_mainFormCaseChanger {};
} // namespace Ui

#endif // UI_CASECHANGER_H



why this doesn't work??

marcel
2nd April 2007, 23:34
I noticed typeA* test, in classChangerClass, is initialized in the constructor list. This cannot be a good thing because setupUi is called after you set the text in the text edit, in the constructor of typeA.

Try constructing "test" after setupUi.

Marcel

aegis
3rd April 2007, 00:13
unfortunatelly i cant setup test after because c++ will try to initialize it implicitly with a default constructor...and since i dont have a default constructor i will get an error message....

any other ideas??

marcel
3rd April 2007, 00:22
Don't call setText in the constructor of testA!
Make another method that calls setText, which will be called after setupUI. It's not really that hard to make this work.

aegis
3rd April 2007, 00:36
Don't call setText in the constructor of testA!
Make another method that calls setText, which will be called after setupUI. It's not really that hard to make this work.
i didn't understand correctly what you said, but i do now and i get the same error that typeA does not name a type...

aegis
3rd April 2007, 00:48
if in classA.h i remove the line4 {#include "caseChangerClass.h"}
then it proceeds with compilation and i get this error:

classA.cpp:9: error: invalid use of undefined type `struct caseChangerClass'

any ideas...?

Brandybuck
3rd April 2007, 00:54
caseChangerClass has a typeA member, but the typeA class takes a caseChangerClass in the constructor. Though it is permitted in C++, it is a sign of a bad design.

aegis
3rd April 2007, 00:56
caseChangerClass has a typeA member, but the typeA class takes a caseChangerClass in the constructor. Though it is permitted in C++, it is a sign of a bad design.
yes but i cant find any other way to make typeA now about the gui.....in particular about the textEdit field of the gui...