Results 1 to 8 of 8

Thread: Access to widgets from a Dialog to another one

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Access to widgets from a Dialog to another one

    Hi everybody !

    I am a newbie both on Qt and C++ ( I come from 12 years of Delphi and VisualBasic6...), 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 ! ).

    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 ).

  2. #2
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Access to widgets from a Dialog to another one

    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
    Qt Code:
    1. class MyDialog:public QDialog,...
    2. {
    3. public :
    4. QLabel label;
    5. };
    To copy to clipboard, switch view to plain text mode 

    ur widget class
    mywidget.h
    Qt Code:
    1. #include "mydialog.h"
    2. class MyWidget:public QWidget,...
    3. {
    4. public :
    5. QLabel label;
    6. MyDialog mydlg; // the object of mydialog to access label
    7. void setDlgLabel();
    8. };
    To copy to clipboard, switch view to plain text mode 

    mywidget.cpp
    Qt Code:
    1. void MyWidget::setDlgLabel()
    2. {
    3. mydlg.label.setText("Success");
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by jpn; 6th August 2008 at 12:09. Reason: missing [code] tags
    "A fool can ask More questions that a wise man cannot answer"
    /home/nithyanair/Pics/image.jpg

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Access to widgets from a Dialog to another one

    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 .

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

  4. #4
    Join Date
    Aug 2008
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Access to widgets from a Dialog to another one

    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 ?

  5. #5
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Access to widgets from a Dialog to another one

    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)
    Last edited by Nithya; 6th August 2008 at 12:38.
    "A fool can ask More questions that a wise man cannot answer"
    /home/nithyanair/Pics/image.jpg

  6. #6
    Join Date
    Aug 2008
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Access to widgets from a Dialog to another one

    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 !
    Attached Files Attached Files

  7. #7
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Access to widgets from a Dialog to another one

    As per my understanding u wanted to access "MainWindowImpl" in "void form2Impl::funct3()"

    so try like this

    Qt Code:
    1. form2impl.h
    2.  
    3. /****************************************************************************
    4. **
    5. ** Created using Edyuk 1.0.0
    6. **
    7. ** File : form2impl.h
    8. ** Date : sam. 2. aot 16:27:03 2008
    9. **
    10. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    11. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    12. **
    13. ****************************************************************************/
    14.  
    15. #ifndef _FORM2IMPL_H_
    16. #define _FORM2IMPL_H_
    17.  
    18. #include <QDialog>
    19. #include "ui_form2.h"
    20. #include "mainwindowimpl.h"
    21.  
    22.  
    23. class form2Impl : public QDialog, public Ui::Dialog //H�itage multiple.
    24. {
    25. Q_OBJECT
    26. public:
    27. /*cf doc de "Qt::WindowFlags", tr� riche... */
    28. MainWindowImpl *win;
    29. form2Impl( QWidget * parent = 0, Qt::WFlags f = 0 );/*Fen�re Standard*/
    30. //MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = Qt::FramelessWindowHint );/*fen�re sans cadre.*/
    31. /**Do not create object of same class**/
    32. // form2Impl* pointr22;
    33. private slots:
    34. void funct3();
    35. private :
    36.  
    37.  
    38. };
    39.  
    40.  
    41. #endif // !_FORM2IMPL_H_
    42.  
    43.  
    44. form2.cpp
    45.  
    46.  
    47. /****************************************************************************
    48. **
    49. ** Created using Edyuk 1.0.0
    50. **
    51. ** File : form2.cpp
    52. ** Date : sam. 2. aot 16:31:45 2008
    53. **
    54. **
    55. ****************************************************************************/
    56.  
    57. #include "form2impl.h"
    58. #include "mainwindowimpl.h"
    59. #include "ui_mainwindow.h"
    60.  
    61.  
    62. form2Impl::form2Impl ( QWidget * parent, Qt::WFlags f)
    63. : QDialog(parent, f)//
    64. {
    65. win=(MainWindowImpl *)parent;
    66. setupUi(this);
    67. connect(pB_1, SIGNAL(clicked()), this, SLOT(funct3()));
    68. connect(pB_Close, SIGNAL(clicked()), this, SLOT(close()));
    69.  
    70. }
    71.  
    72. void form2Impl::funct3()
    73. {
    74. QApplication::beep();
    75. win->lbl_1->setText("abcd");
    76. /* cr� un pointeur sur classe :*/
    77. // MainWindowImpl pt;
    78. // MainWindowImpl *pointr=&pt;
    79. //MainWindowImpl *pointr= new MainWindowImpl (this);
    80. //pt.lbl_1->setText("abcd");
    81. //pointr->lbl_1->setText("abcd");
    82. //pointr->lbl_1->repaint();
    83. //label1->setText("ABRACADABRA !");
    84. //QString strg = MainWindowImpl::pointr4->lbl_1->text();
    85. //label1->setText(strg);
    86. //pointr= pointr4;
    87.  
    88.  
    89. //pt.lbl_1->setText("abcd");
    90. /* MainWindowImpl::aa->lbl_1->setText("abcd");*/
    91. //Ui::MainWindow *aa; //plante car impl�entation parall�e
    92. //aa->lbl_1->setText("a4cd");
    93. //MainWindowImpl::ui.lbl_1->setText("AK47");
    94. //MainWindowImpl::
    95.  
    96. // Ui::MainWindow pt; //On ne r�up�e que la partie widget de l'h�itage multiple de la classe MainWindowImpl
    97. // Ui::MainWindow* pointeur=&pt ;
    98. // pointeur->lbl_1->setText("abcd");
    99. //pt.lbl_1->repaint();
    100. //pointeur->pB_Close->setText("abcd");
    101.  
    102. //st = pointeur->lbl_1->text();
    103. // st = pt.lbl_1->text();
    104. //pB_Close->setText(st);
    105.  
    106.  
    107. }
    To copy to clipboard, switch view to plain text mode 
    "A fool can ask More questions that a wise man cannot answer"
    /home/nithyanair/Pics/image.jpg

  8. #8
    Join Date
    Aug 2008
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Access to widgets from a Dialog to another one

    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!

Similar Threads

  1. Replies: 2
    Last Post: 16th May 2008, 14:39
  2. Resizing the dialog boxes
    By anju123 in forum Qt Programming
    Replies: 4
    Last Post: 14th September 2007, 10:41
  3. QGraphicsView: Dialog Position Doubt
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 6th August 2007, 17:48
  4. Access widgets
    By Gayathri in forum Newbie
    Replies: 2
    Last Post: 17th November 2006, 14:37
  5. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 14:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.