Results 1 to 3 of 3

Thread: How to access controls of one window from another?

  1. #1
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How to access controls of one window from another?

    I have created a Qt Gui application which has two forms: mainwindow.ui and childwindow.ui.

    I have a LineEdit controls on both forms. How can I access the text on the mainwindow from the childwindow and vice-versa?

    Normally, on the same from I will use the code:
    Qt Code:
    1. ui->lineEdit1->text();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to access controls of one window from another?

    Quote Originally Posted by gtan View Post
    I have created a Qt Gui application which has two forms: mainwindow.ui and childwindow.ui.

    I have a LineEdit controls on both forms. How can I access the text on the mainwindow from the childwindow and vice-versa?

    Normally, on the same from I will use the code:
    Qt Code:
    1. ui->lineEdit1->text();
    To copy to clipboard, switch view to plain text mode 
    There are multiple ways you could go about this, depending on the structure of your application and how much it is appropriate that one window should “know” about the other.

    For example, you could add a parameter to the constructor for the child window and pass it a QLineEdit* that is the address of lineEdit1 in the main window. The child window would then save that pointer and be able to access the main window’s line edit through it.

    The child window could declare a QLineEdit* in the public part of its interface and copy the pointer to its line edit there in the constructor. So long as the main window keeps a copy of the pointer to the child window, it would then also have access to the child window’s line edit.

    There is no formula for this; you have to design methods by which the two objects (windows) will communicate.

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

    gtan (21st September 2012)

  4. #3
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to access controls of one window from another?

    Alright. Thank you!

Similar Threads

  1. Replies: 2
    Last Post: 4th August 2010, 20:10
  2. Replies: 4
    Last Post: 10th February 2010, 07:56
  3. Replies: 1
    Last Post: 12th June 2009, 10:26
  4. access main window from function
    By eric in forum Qt Programming
    Replies: 6
    Last Post: 19th January 2008, 22:29
  5. Hiding controls changes spacing of other controls
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2007, 21:47

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.