Results 1 to 5 of 5

Thread: How can I call a function on another Form?

  1. #1
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Post How can I call a function on another Form?

    Hi,

    I've a two Forms (Form1 and Form2) in a program. When program starts two forms are run.

    I have several codes on Form1 eg. many Key_Events

    if (event->key() == Qt::Key_M) {
    .......
    }
    else if (event->key() == Qt::Key_Y) {
    .........
    }
    else if (event->key() == Qt::Key_G) {
    .....
    ..........
    }

    On Form2, I add several pushbuttons.

    When I pressed a button, I want to run (eg. Key_M Event of Form1).
    When I pressed another, do another event.

    I want to get sample code or example project.

    Thanks

  2. #2
    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: How can I call a function on another Form?

    Maybe you can clarify:
    - do you want to call a function on the other object
    or
    - do you want to send events to the other object

    Cheers,
    _

  3. #3
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: How can I call a function on another Form?

    Thanks for your reply

    When I click playButton on PlayerForm, I want to fire Key_P Event on MainWindow Form.

    eg.

    At Form2 (PlayerForm)

    void PlayerForm:n_playButton_clicked()
    {
    //fire the event Key_P on Form1 and want to run playSong(); ........
    }

    void PlayerForm:n_muteButton_clicked()
    {
    //fire the event Key_M on Form1 and want to run muteSong();
    }

    void PlayerForm:n_stopButton_clicked()
    {
    //fire the event Key_S on Form1 and want to run stopSong();
    }
    at Form1 (MainWindow)

    if (event->key() == Qt::Key_P) {
    playSong();
    showVisualization();
    setVolumeNormalization();
    }
    else if (event->key() == Qt::Key_M) {
    muteSong();
    }
    else if (event->key() == Qt::Key_S) {
    stopSong();
    }
    Last edited by binary001; 17th October 2015 at 11:57.

  4. #4
    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: How can I call a function on another Form?

    Calling a function is easy, since you either call the function directly or make it a slot and let form2 emit a signal.

    If you need to send an event, see QCoreApplication::postEvent().

    Cheers,
    _

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can I call a function on another Form?

    When I click playButton on PlayerForm, I want to fire Key_P Event on MainWindow Form.
    A better design would be something like this:

    - On PlayerForm, you implement a "play()" signal.
    - On MainWindow, you implement a "onPlay()" slot.
    - When you construct your PlayerForm instance, you connect PlayerForm:: play() to MainWindow:: onPlay()
    - When the user clicks the play button on PlayerForm, PlayerForm emits the "play()" signal
    - MainWindow receives the signal in its onPlay() slot, and you do whatever it is you want to do.

    The problem with posting events like you propose is that if you post them to the MainWindow's event loop, it will be the same as if the user had clicked the P key in the MainWindow. So the user can click P in either PlayerForm or MainWindow, and the same thing will happen. If you want to use PlayerForm to show the status of the song or whatever, but the user clicks P in MainWindow, PlayerForm won't know about it, so the two will not be synchronized any more.

  6. The following user says thank you to d_stranz for this useful post:

    binary001 (30th November 2015)

Similar Threads

  1. Replies: 2
    Last Post: 2nd December 2013, 04:43
  2. Replies: 4
    Last Post: 2nd August 2012, 07:42
  3. call form 1 from form 2
    By saff in forum Qt Programming
    Replies: 4
    Last Post: 13th December 2010, 17:15
  4. How to call to variables of another form
    By matulik in forum Newbie
    Replies: 3
    Last Post: 14th May 2010, 18:32
  5. Replies: 1
    Last Post: 28th July 2006, 14:10

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.