简体   繁体   English

具有返回值的信号/插槽不起作用

[英]Signal/Slot with return value doesn't work

I made a signal slot in Qt and the program runs without error or warnings about the connect i made. 我在Qt中创建了一个信号插槽,程序运行时没有错误或有关我进行的连接的警告。 The problem is that when i want to use the signal slot, it always returns NULL. 问题是,当我要使用信号插槽时,它总是返回NULL。

Main.cpp 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 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.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(); 当我使用int turn = emit getTurn(); in Scrabble.cpp, turn will become 0 and not 1. Does anyone know what i'm doing wrong? 在Scrabble.cpp中,转弯将变为0,而不是1。有人知道我在做什么吗?

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. 信号是由Moc自动生成的,不能在.cpp文件中实现。 They can never have return types (ie use void). 它们永远不能有返回类型(即使用void)。

Returning values from signals is not required when you use Qt features correctly. 正确使用Qt功能时,不需要从信号返回值。 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. 您肯定做错了什么。

Signals/slots cant return any value. 信号/插槽不能返回任何值。 Possible solution: 可能的解决方案:

Scrabble: 拼字游戏:

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

Game: 游戏:

public slot: onrequestTurn();
signal: sendTurn(int);

emit "keyword" is highly undocumented right now, but from Qt's source, it is only empty define, so your code emit “ keyword”的信息目前尚未高度公开,但是从Qt的源头来看,它只是空的定义,因此您的代码

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 . 现在,请注意turn变量不是从slot获取值,而是从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() ). 您可以使用常规函数调用(只需调用giveTurn()函数: int turn = giveTurn() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM