Results 1 to 9 of 9

Thread: Modifying ui elements of one dialog from another dialog.

  1. #1
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Modifying ui elements of one dialog from another dialog.

    I am a total newbie. I have this doubt

    suppose one has to change text
    We use:

    ui->lineEdit->setText("hello world") ;

    for inside .cpp of any window, we use the above.

    But if I want to setText of one dialog from another window,how do I do that?
    I have a dialog called 'dialog',which has a lineEdit called "qwerty".
    How to set text of qwerty from another window,say MainWindow?

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Modifying ui elements of one dialog from another dialog.

    Hello,
    Use Signals and Slots. Same logic as your previous post.
    for example, emit a signal (from mainwindow) holding the QString that you wanna set from main window to the QDialog,
    catch the emitted signal by connecting it to a slot that you should create in the QDailog.
    Qt Code:
    1. QDialog *mDialog = new QDialog(this);
    2. connect(this,SIGNAL(sendText(QString)),mDialog,SLOT(getText(QString)));
    To copy to clipboard, switch view to plain text mode 
    the Slot getText should be something like:
    Qt Code:
    1. getText(QString txt)
    2. {
    3. ui->qwerty->setText("txt");
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to toufic.dbouk for this useful post:

    harvey_slash (5th October 2013)

  4. #3
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Modifying ui elements of one dialog from another dialog.

    I am totally new to signal slot, could you tell me what exactly is happening in your code ?
    What is sendText?
    Last edited by harvey_slash; 5th October 2013 at 16:25.

  5. #4
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Modifying ui elements of one dialog from another dialog.

    Sure ill tell you whats happening in English
    lets take your example, you have a main window and dialog , and you want to change the text of a line edit in the dialog upon an event that happens in the main window.
    lets say you want to press a button in the main window , and upon pressing it you want to set a specific text in that line edit of the dialog.
    so to do that in signals and slots , when pressing the button emit a signal holding a QString. now this signal is emitted , so you have to catch it somewhere. Here comes the role of the connect , you connect that signal to a slot in the dialog. So that when the signal is emitted that dialog can catch it and perform an action according to it by the connected slot.
    Qt Code:
    1. connect(this,SIGNAL(sendText(QString)),mDialog,SLOT(getText(QString)));
    To copy to clipboard, switch view to plain text mode 
    this: represents the parent of the signal , in our case it is the main window
    sendText: is the signal that you declare in the header file under the SIGNALS: ( you emit the signal in your cpp file when needed by : emit sendText(QString txt)
    mDialog: represents the parent of the slot , in our case it is the dialog
    getText: is the slot in dialog that you create to handle the signal
    when the signal sendText holding the QString is emitted , the dialog will catch it and execute the connected slot , hence you can set that QString in the dialog.
    so to sum it up, when the button in the main window is pressed it will get a string and and emit a signal , the signal is caught in the dialog and you perform actions according to the connected slot.
    check this link for better and more explanation Signals and Slots
    Hope this explains it.
    Last edited by toufic.dbouk; 5th October 2013 at 16:41.

  6. #5
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Modifying ui elements of one dialog from another dialog.

    I got most of the mechanism, but I don't know how to really use it.
    what definitions , etc do I have to do, and what is sendText?

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Modifying ui elements of one dialog from another dialog.

    You don't need sendText(), you can just connect to the QLIneEdit's textChanged(QString) signal.

    Qt Code:
    1. connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(setText(QString)));
    To copy to clipboard, switch view to plain text mode 

    So the only thing you really need is a slot in your dialog class
    Qt Code:
    1. class MyDialog : public QDIalog
    2. {
    3. Q_OBJECT
    4.  
    5. //...
    6.  
    7. public slots:
    8. void setText(const QString &text); // this consumes the text in whatever way you deem right
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. #7
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Modifying ui elements of one dialog from another dialog.

    Quote Originally Posted by anda_skoa
    You don't need sendText(), you can just connect to the QLIneEdit's textChanged(QString) signal.
    yea we went over this , was just making it clearer by emitting his own signal.
    in the header file just add :
    Qt Code:
    1. signals:
    2. void sendText(QString);
    To copy to clipboard, switch view to plain text mode 

    and in the .cpp file in the method where you need to emit the signal add:
    Qt Code:
    1. emit sendText(/*your string */);
    To copy to clipboard, switch view to plain text mode 
    Last edited by toufic.dbouk; 5th October 2013 at 17:17.

  9. #8
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Modifying ui elements of one dialog from another dialog.

    I am having difficulty understand this .
    could you give me some reference ?
    and, are there any other methods to get my job done ?


    Added after 9 minutes:


    Quote Originally Posted by anda_skoa View Post
    You don't need sendText(), you can just connect to the QLIneEdit's textChanged(QString) signal.

    Qt Code:
    1. connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(setText(QString)));
    To copy to clipboard, switch view to plain text mode 

    So the only thing you really need is a slot in your dialog class
    Qt Code:
    1. class MyDialog : public QDIalog
    2. {
    3. Q_OBJECT
    4.  
    5. //...
    6.  
    7. public slots:
    8. void setText(const QString &text); // this consumes the text in whatever way you deem right
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    could you please explain why you did everything ?
    I want to learn signal slot mechanisms.
    thanks.
    Last edited by harvey_slash; 5th October 2013 at 19:22.

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Modifying ui elements of one dialog from another dialog.

    The slot is a "receiver" for data sent/emitted by a signal.
    To create a slot you add a "slots" section to the class declaration of the receiver class, in this case I assume a QDialog subclass.
    A slots section is either public, protected or private, just like for normal C++ methods of the class.

    The connect() statement then hooks the signal and the slot up to each other, so that whenever the signal sends its value, it will be received by the slot.
    I.e. the slot will be called with the text as its argument.

    What you do in that method (the slot is just a normal C++ method in that regard) is up to you. You could e.g. set it on a label that is a child of the dialog
    Qt Code:
    1. void MyDialog::setText(const QString &text)
    2. {
    3. ui->label->setText(text);
    4. }
    To copy to clipboard, switch view to plain text mode 

    SIgnal/Slots are the most convenient way to letting other parts of the application know that something has happend.
    But of course this is C++, so it is also possible to use other forms of notification mechanisms, e.g. the observer pattern

    Cheers,
    _

Similar Threads

  1. madal dialog and modeless dialog problem
    By melody:p in forum Qt Programming
    Replies: 0
    Last Post: 25th September 2012, 09:39
  2. How to access objects of parent Dialog from Child Dialog .
    By ranjit.kadam in forum Qt Programming
    Replies: 4
    Last Post: 18th April 2011, 07:39
  3. closing child dialog closes parent dialog
    By sparticus_37 in forum Newbie
    Replies: 2
    Last Post: 28th May 2010, 20:46
  4. How to blur parent dialog when child dialog is displayed
    By abhilashajha in forum Qt Programming
    Replies: 4
    Last Post: 10th June 2009, 14:01
  5. Modifying Dialog problem
    By donmorr in forum Qt Tools
    Replies: 5
    Last Post: 26th May 2006, 13:56

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.