1

I made a signal slot in Qt and the program runs without error or warnings about the connect i made. The problem is that when i want to use the signal slot, it always returns NULL.

Main.cpp

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);


  Game* game = new Game;

  Scrabble mainWindow;
  mainWindow.show();

  QObject::connect(&mainWindow,SIGNAL(getTurn()),game,SLOT(giveTurn()));

  return a.exec();
}

Game.h

class Game: public QObject
{
    Q_OBJECT
public:
    Game(QObject *parent = 0);
    ~Game();
private:
    int m_turn;
public slots:
    int giveTurn();

};

Game.cpp

Game::Game(QObject *parent)
    :QObject(parent)
{
    m_turn = 1;
}
Game::~Game()
{

}

int Game::giveTurn()
{
    return m_turn;
}

Scrabble.h

class Scrabble : public QMainWindow
{
    Q_OBJECT

public:
    explicit Scrabble(QWidget *parent = 0);
    ~Scrabble();

private:
    Ui::Scrabble *ui;
signals:
    int getTurn();

};

when i use int turn = emit getTurn(); in Scrabble.cpp, turn will become 0 and not 1. Does anyone know what i'm doing wrong?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
breght
  • 57
  • 10
  • 2
    Using signals like this is extremely unusual, and isn't even documented to work (http://stackoverflow.com/questions/11890644/qt-return-value-of-signal-works-why-is-the-official-doc-saying-it-is-impossibl for instance). If you need a return value, a signal isn't what you should be using, you should be calling an ordinary function. – Mat Aug 04 '13 at 12:58
  • You should rename this to "Signal/Slot with return value doesn't work". See also this discussion: http://stackoverflow.com/questions/5842124/can-qt-signals-return-a-value As Mat says, while it *might* work, Qt's signal/slots are not intended to be used like this and you should look for another solution. Which Qt version do you use? – Frank Osterfeld Aug 04 '13 at 13:07
  • i use Qt 5.0.2 and i'm using the signal/slot to connect the model and the view. – breght Aug 04 '13 at 13:10
  • Found the problem: i did the emit in scrabble.cpp before making the connect in main.cpp. – breght Aug 08 '13 at 10:16

3 Answers3

0

You're using signals and slots incorrectly. Signals cannot return value. See the Signals & Slots documentation page:

Signals are automatically generated by the moc and must not be implemented in the .cpp file. They can never have return types (i.e. use void).

Returning values from signals is not required when you use Qt features correctly. Maybe you should create another question and describe what you want to do and why you need such connection. You're definitely doing something wrong.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
0

Signals/slots cant return any value. Possible solution:

Scrabble:

signal: void requestTurn();
public slot: receiveTurn(int);

Game:

public slot: onrequestTurn();
signal: sendTurn(int);
András Kovács
  • 847
  • 1
  • 10
  • 29
0

emit "keyword" is highly undocumented right now, but from Qt's source, it is only empty define, so your code

int turn = emit getTurn(); 

will be expanded to:

int turn = getTurn(); 

However, this is not covered in oficial documentation and it might change any time - so - don't use it!

Now, please note that turn variable is not getting value from slot, but from signal. There is nothing about passing return value from slot to signal - and it doesn't make sense (well, it may make sense in your sample, but what if I connect multiply slots to a single signal - what slot will return value, what if slots are executed asynchronously - should we wait for return value, etc.).

You can use regular function call (just call giveTurn() function: int turn = giveTurn()).

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91