Results 1 to 11 of 11

Thread: Signals & slots

  1. #1
    Join Date
    Nov 2014
    Posts
    12
    Qt products
    Qt5

    Default Signals & slots

    Hello! I use Qt5. My problem is nex: i want to send a signal from file "Scene"

    Scene.h

    Qt Code:
    1. signals:
    2. void signalShowStatus( const int & );
    3.  
    4. private:
    5. void sendStatus();
    To copy to clipboard, switch view to plain text mode 

    Scene.cpp

    Qt Code:
    1. void Scene::sendStatus()
    2. {
    3. int N = m_game.segment();\\ m_game.segment() steadily increasing
    4. emit signalShowStatus( N );
    5. }
    To copy to clipboard, switch view to plain text mode 

    to file "mainwindow"

    mainwindow.h

    Qt Code:
    1. private slots:
    2. void slotSetStatus( const int &N );
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. connect( ui->widget , SIGNAL( signalShowStatus( int ) ),
    6. this, SLOT( slotSetStatus( int ) ) );
    7. }
    8.  
    9. void MainWindow::slotSetStatus( const int &N )
    10. {
    11.  
    12. ui->lcdnumber->display( N );
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    Without fault, but does not count on widget. Where did I go wrong?
    Thanks)

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signals & slots

    What is the type of ui->widget?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2014
    Posts
    12
    Qt products
    Qt5

    Default Re: Signals & slots

    Type is Scene.

  4. #4
    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: Signals & slots

    Is "Scene" derived from QWidget? Is it a widget you have added to MainWindow using Qt Designer, and you have named it "widget"? If both of these are true, then the problem might be because you defined your slot / signal pair to use "const int &" as an argument, but your connect statement just says "int".

  5. #5
    Join Date
    Nov 2014
    Posts
    12
    Qt products
    Qt5

    Default Re: Signals & slots

    It both true, thanks) But i forgott to say one thing: there is another class "Game", wherein a change, that I need! May it is a reason?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signals & slots

    Do you get any warnings about signals or slots on the console while the application is running?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Signals & slots

    Some notes:
    (1) Is Scene a Q_OBJECT ? It does not suffice that it is derived from a Q_OBJECT. Only Q_OBJECTs can declare slots and signals.
    (2) Have you got linker or run time errors (as Wysota has asked)? You should because there is no slot slotSetStatus(int) in sight. Similarly, signalShowStatus(int) isn't declared. int and int& are different types.
    (3) Do not count on sync. Your signal brings a reference to a local object which need not exist when the slot is triggered because sendStatus() has returned in the meantime. Either emit signalShowStatus(m_game.segment()) or signal an int instead of a reference.

  8. #8
    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: Signals & slots

    Quote Originally Posted by Radek View Post
    (1) Is Scene a Q_OBJECT ? It does not suffice that it is derived from a Q_OBJECT. Only Q_OBJECTs can declare slots and signals.
    Same for the MainWindow.
    It is actually more likely to be missing in the receiver class, since a missing MOC run on a sender class leaves the signal function unimplemented, which leads to a build error.

    Quote Originally Posted by Radek View Post
    You should because there is no slot slotSetStatus(int) in sight. Similarly, signalShowStatus(int) isn't declared.
    They look both ok.

    Quote Originally Posted by Radek View Post
    int and int& are different types.
    Yes, but "int" and "const int&" are as far as signal/slots are concerned.

    That's why we usually just use "QString" in SIGNAL and SLOT macros, despite the method signature usually being "const QString&".
    The signal/slot system calls this "normalized signatures".

    Quote Originally Posted by Radek View Post
    (3) Do not count on sync. Your signal brings a reference to a local object which need not exist when the slot is triggered because sendStatus() has returned in the meantime.
    No. This here is effectively a Qt:irectConnection. The signal returns once all connected slots have been executed.

    Cheers,
    _

  9. #9
    Join Date
    Nov 2014
    Posts
    12
    Qt products
    Qt5

    Default Re: Signals & slots

    I have no any errors) Безымянный.jpg. I poizmenyat int to const int & in connect, but there is an error: cannot open output file release \MySnale.exe: Permission denied


    Added after 7 minutes:


    anda_skoa, I know English not well, but how I uderstood: signal in Scene already sends a modified variable? Yes?
    Last edited by igoreshka3333; 20th December 2014 at 10:30.

  10. #10
    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: Signals & slots

    Quote Originally Posted by igoreshka3333 View Post
    I have no any errors) Безымянный.jpg. I poizmenyat int to const int & in connect, but there is an error: cannot open output file release \MySnale.exe: Permission denied
    You are on Windows. Windows blocks access to a file that is already opened. You are still running the program. The compiler can't write the file.

    Quote Originally Posted by igoreshka3333 View Post
    anda_skoa, I know English not well, but how I uderstood: signal in Scene already sends a modified variable? Yes?
    Not sure what you mean. The signal sends the value you have in that variable.

    Cheers,
    _

  11. #11
    Join Date
    Nov 2014
    Posts
    12
    Qt products
    Qt5

    Default Re: Signals & slots

    Thanks a lot for your replyes. The problem is fixed: method which must send a signal not performed. so I emit the signal in another place)))

Similar Threads

  1. Replies: 2
    Last Post: 18th April 2013, 12:15
  2. Qt signals slots again
    By bmpix in forum Newbie
    Replies: 4
    Last Post: 6th December 2011, 19:54
  3. Help with Signals and slots
    By chetu1984 in forum Newbie
    Replies: 5
    Last Post: 10th March 2011, 22:30
  4. Signals and Slots
    By merry in forum Qt Programming
    Replies: 4
    Last Post: 22nd February 2007, 08:11
  5. help with signals and slots
    By superutsav in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2006, 12:49

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.