PDA

View Full Version : Access to widgets from a Dialog to another one



jean
6th August 2008, 11:45
Hi everybody !

I am a newbie both on Qt and C++ ( I come from 12 years of Delphi and VisualBasic6...:o), so I apologize if my question is a dump one. I have googled web, searched within Qt4 doc (something very nicely done !) and scanned many titorials, books, code examples without finding any usefull information.

Using to Dialog forms (1 and 2) I would like to access to properties/functions of a widget (for example a label->setText()) belonging to one form from another form. To do so, I have tried many combination of constructors, functions, classes, pointers,generic pointers ... but none of my attemps have been succefull : I only can instanciate à new class of Dialog1 (no usefull for me !) from Dialog2 oraccess to few members of the instanciated class of Dialog1 (not its widgets ! :mad:).

Dialogs 1 et 2 has been build via QDesigner then instanciated by multiple heritage from QDialog and Ui::.:

Could you help me ? I really accessing widget from another form a complicated procedure ? (this is done in a snapshot with VB6 or delphi :p).

Nithya
6th August 2008, 11:56
Create the object of required dialog/widget in the other one and access the 'label' through that object

e.g.
ur dialog class
mydialog.h

class MyDialog:public QDialog,...
{
public :
QLabel label;
};

ur widget class
mywidget.h

#include "mydialog.h"
class MyWidget:public QWidget,...
{
public :
QLabel label;
MyDialog mydlg; // the object of mydialog to access label
void setDlgLabel();
};

mywidget.cpp

void MyWidget::setDlgLabel()
{
mydlg.label.setText("Success");
}

if you are inheriting any ui (as public Ui::..) and label is in that class also, u go the same way,
(use mydlg.label->setText(".."); if label is a pointer object.

aamer4yu
6th August 2008, 11:56
I didnt get exactly what you mean.
But if u want any communication between dialogs, you can make one member of another and so on.

Other method of comunication is Signals and Slots (http://doc.trolltech.com/4.4/signalsandslots.html) .

If u explain what exactly u want to achieve, may be we can tell you. (I havent worked on delphi/VB)

jean
6th August 2008, 12:25
Thanks Nithya for replying.
I understand that I would have to re-implement in a class (Dialog1) a function for all the widgets I would like to access from another class (Dialog2). This seems to me as possible for some few widgets but if I have a form with a lot of widgets, this method would be too much complicated. Moreover, the Dialog1 widgets class is already implemented in the header automatically created from the .ui file by uic. Why I cannot access them ?:(

Nithya
6th August 2008, 12:33
I guess Ur understanding is wrong.u needn't have to reimplement the ui classes members.
Since all the members inside theui class is public if u inherit the class publically

i.e
class Dialog1:public QDialog,public Ui::Form
{
};


please see whether u have public key word while giving multiple iheritance as like above code.

I got to know that u have access to ur ui object from ur posting that


"Dialogs 1 et 2 has been build via QDesigner then instanciated by multiple heritage from QDialog and Ui::.:"

Plz see the last line of my reply.(plz re read MyDialog as Dialog1 and MyWidget as Dialog2)

jean
6th August 2008, 13:27
1- to aamer4yu : if QDialog2 is a class member of QDialog1, maybe I am wrong but I presume that instanciating Dialog1 will automatically allocate memory for Dialog2. This is not exactly what I want, since in some cases, Dialog2 could be unnecessary (imagine Dialog1 is the main form, and Dialog2 an accesory form, only used to change parameters in Dialog1).

2- to Nithya : Both Dialog1 and Dialog2 classes are public inheriting parents.

3- I have included here a sample of my code, as compact as possible.

Thanks for your advises and helps !

Nithya
6th August 2008, 16:04
As per my understanding u wanted to access "MainWindowImpl" in "void form2Impl::funct3()"

so try like this



form2impl.h

/************************************************** **************************
**
** Created using Edyuk 1.0.0
**
** File : form2impl.h
** Date : sam. 2. aot 16:27:03 2008
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
************************************************** **************************/

#ifndef _FORM2IMPL_H_
#define _FORM2IMPL_H_

#include <QDialog>
#include "ui_form2.h"
#include "mainwindowimpl.h"


class form2Impl : public QDialog, public Ui::Dialog //H�itage multiple.
{
Q_OBJECT
public:
/*cf doc de "Qt::WindowFlags", tr� riche... */
MainWindowImpl *win;
form2Impl( QWidget * parent = 0, Qt::WFlags f = 0 );/*Fen�re Standard*/
//MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = Qt::FramelessWindowHint );/*fen�re sans cadre.*/
/**Do not create object of same class**/
// form2Impl* pointr22;
private slots:
void funct3();
private :


};


#endif // !_FORM2IMPL_H_


form2.cpp


/************************************************** **************************
**
** Created using Edyuk 1.0.0
**
** File : form2.cpp
** Date : sam. 2. aot 16:31:45 2008
**
**
************************************************** **************************/

#include "form2impl.h"
#include "mainwindowimpl.h"
#include "ui_mainwindow.h"


form2Impl::form2Impl ( QWidget * parent, Qt::WFlags f)
: QDialog(parent, f)//
{
win=(MainWindowImpl *)parent;
setupUi(this);
connect(pB_1, SIGNAL(clicked()), this, SLOT(funct3()));
connect(pB_Close, SIGNAL(clicked()), this, SLOT(close()));

}

void form2Impl::funct3()
{
QApplication::beep();
win->lbl_1->setText("abcd");
/* cr� un pointeur sur classe :*/
// MainWindowImpl pt;
// MainWindowImpl *pointr=&pt;
//MainWindowImpl *pointr= new MainWindowImpl (this);
//pt.lbl_1->setText("abcd");
//pointr->lbl_1->setText("abcd");
//pointr->lbl_1->repaint();
//label1->setText("ABRACADABRA !");
//QString strg = MainWindowImpl::pointr4->lbl_1->text();
//label1->setText(strg);
//pointr= pointr4;


//pt.lbl_1->setText("abcd");
/* MainWindowImpl::aa->lbl_1->setText("abcd");*/
//Ui::MainWindow *aa; //plante car impl�entation parall�e
//aa->lbl_1->setText("a4cd");
//MainWindowImpl::ui.lbl_1->setText("AK47");
//MainWindowImpl::

// Ui::MainWindow pt; //On ne r�up�e que la partie widget de l'h�itage multiple de la classe MainWindowImpl
// Ui::MainWindow* pointeur=&pt ;
// pointeur->lbl_1->setText("abcd");
//pt.lbl_1->repaint();
//pointeur->pB_Close->setText("abcd");

//st = pointeur->lbl_1->text();
// st = pt.lbl_1->text();
//pB_Close->setText(st);


}

jean
7th August 2008, 09:42
To Nithya :

Nithya, your answer is exactly what I wanted :) , it works nicely!
Moreover, you make me understanding a basic C++ concept that I was unable to apply.
Many thanks for your help ! (I apologize for my confused attached file : I prepared a cleaner one with explanations of what I was looking for, but I did not joined the right file).

Thank for your help!