PDA

View Full Version : Signals & slots



igoreshka3333
19th December 2014, 16:33
Hello! I use Qt5. My problem is nex: i want to send a signal from file "Scene"

Scene.h


signals:
void signalShowStatus( const int & );

private:
void sendStatus();

Scene.cpp


void Scene::sendStatus()
{
int N = m_game.segment();\\ m_game.segment() steadily increasing
emit signalShowStatus( N );
}

to file "mainwindow"

mainwindow.h


private slots:
void slotSetStatus( const int &N );

mainwindow.cpp


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
connect( ui->widget , SIGNAL( signalShowStatus( int ) ),
this, SLOT( slotSetStatus( int ) ) );
}

void MainWindow::slotSetStatus( const int &N )
{

ui->lcdnumber->display( N );

}

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

wysota
19th December 2014, 17:06
What is the type of ui->widget?

igoreshka3333
19th December 2014, 17:33
Type is Scene.

d_stranz
19th December 2014, 20:04
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".

igoreshka3333
19th December 2014, 20:53
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?

wysota
20th December 2014, 08:06
Do you get any warnings about signals or slots on the console while the application is running?

Radek
20th December 2014, 08:34
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.

anda_skoa
20th December 2014, 10:15
(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.



You should because there is no slot slotSetStatus(int) in sight. Similarly, signalShowStatus(int) isn't declared.

They look both ok.



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".



(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::DirectConnection. The signal returns once all connected slots have been executed.

Cheers,
_

igoreshka3333
20th December 2014, 10:30
I have no any errors) 10829. 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?

anda_skoa
20th December 2014, 15:11
I have no any errors) 10829. 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.



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,
_

igoreshka3333
20th December 2014, 17:49
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)))