Results 1 to 4 of 4

Thread: Sending a signal & openinig a new window

  1. #1
    Join Date
    Feb 2017
    Posts
    8
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Sending a signal & openinig a new window

    Hi.
    I have a (login) window and an (employe) window.
    I used signals & slots to send one variable from the first window to the seconde
    What I want is when I click the login button the value of the username should be sent to employe window and open it on the same time.
    here's what I've done:

    login.h
    Qt Code:
    1. ...
    2. signals:
    3. void notifyValueSentToEmp(const QString username);
    4. ...
    To copy to clipboard, switch view to plain text mode 

    login.cpp
    Qt Code:
    1. ...
    2. void Login::on_pushButton_Login_clicked(){
    3. ...
    4. employe e;
    5. emit notifyValueSentToEmp(username);
    6. e.show();
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    When I use (e.show()) in login.cpp the variable is not sent

    employe.h
    Qt Code:
    1. ...
    2. private slots:
    3. void onValueSentToEmp(const QString usename);
    4. ...
    To copy to clipboard, switch view to plain text mode 

    employe.cpp
    Qt Code:
    1. ...
    2. void employe::onValueSentToEmp(QString username){
    3. // Here where I use the sent variable username
    4. }
    5. ...
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. Login l;
    2. employe e;
    3. QObject::connect(&l,SIGNAL(notifyValueSentToEmp(QString)),&e,SLOT(onValueSentToEmp(QString)));
    4. l.show();
    5. e.show();
    To copy to clipboard, switch view to plain text mode 
    When I use (e.show()) in the main the value is sent but the second window appears in the same time with first window.

    I hope you can help fix this.
    thank u

  2. #2
    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: Sending a signal & openinig a new window

    Qt Code:
    1. void Login::on_pushButton_Login_clicked(){
    2. ...
    3. employe e;
    4. emit notifyValueSentToEmp(username);
    5. e.show();
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    In this code, you are creating and showing a new employee window (on the stack). It is not the same instance of employee that you created in main() and which you hooked up to the signal. As soon as this on_pushButton_Login_clicked() slot exits, the new instance is destroyed. If you don't want this second window, then remove the lines of code that create and then show it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Feb 2017
    Posts
    8
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sending a signal & openinig a new window

    I did not put both of them.
    I'm using e.show in the main.cpp where I put the connect()
    I was just trying to say that if I want the second window to appear after clicking the button than I should put the e.show() in the on_pushButton_Login_clicked(). But I can't use it in connect() in the main.
    When I put it in the main with the connect it works but I have to show the two windows at the same time. And I don't want that.
    Thank u

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending a signal & openinig a new window

    Something like this :
    main.cpp
    Qt Code:
    1. Login l;
    2. employe e;
    3. QObject::connect(&l,SIGNAL(notifyValueSentToEmp(QString)),&e,SLOT(onValueSentToEmp(QString)));
    4. l.show();
    To copy to clipboard, switch view to plain text mode 
    login.cpp
    Qt Code:
    1. void Login::on_pushButton_Login_clicked(){
    2. ...
    3. emit notifyValueSentToEmp(username);
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 
    employe.cpp
    Qt Code:
    1. void employe::onValueSentToEmp(QString username){
    2. // Here where I use the sent variable username
    3. show();
    4. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Lesiok for this useful post:

    Akame.L (16th March 2017)

Similar Threads

  1. Replies: 3
    Last Post: 20th August 2015, 01:19
  2. sending a signal though the dbus.
    By savaliya_ambani in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 26th August 2010, 18:06
  3. Replies: 5
    Last Post: 21st April 2010, 21:36
  4. Sending signal to other process?
    By Boron in forum Qt Programming
    Replies: 3
    Last Post: 28th August 2009, 09:06
  5. sending data over signal
    By gyre in forum Newbie
    Replies: 1
    Last Post: 16th December 2007, 23: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.