如何在两个 类 之间连接函数
how to connect function between two classes
我在两个文件中有两个不同的class:
class Game: public QGraphicsView()
class Window: public QMainWindow()
{
public: Window();
Game *game;
public slots: void test() {game = new Game();};
}
在 Window.cpp
我使用 test() 函数开始一个新游戏:
Window::Window() {test();}
现在 Game.cpp
我用两个 QPushButton
创建了一个 QMessageBox
QMessageBox *box= new QMessageBox();
QPushButton *btYES = box->addButton(tr("YES"),QMessageBox::ActionRole);
QPushButton *btNO = box->addButton(tr("NO"),QMessageBox::ActionRole);
box->exec();
if (box->clickedButton() == btYES) {Window::test();}
if (box->clickedButton() == btNO) {close();}
如您所见,我想将函数 test()
连接到 Game.cpp
中的 btYES
,但该函数在 Window.cpp
中,其功能是启动一个新游戏。
可以吗?
QPushButton 在 pressed/released
时发出事件
因此,您可以将释放信号连接到插槽:
connect(button, SIGNAL(released()), windowClass, SLOT(handleButton()));
在您的情况下,您需要通过 class 发送它,因此您可能需要分两步完成。
游戏中:
// connect the button to a local slot
connect(btYES, SIGNAL(released()), this, SLOT(handleYesButton()));
// in the slot emit a signal - declare the signal in the header
game::handleYesButton()
{
emit userChoiceYes();
}
在window
// connect the signal in game to a your slot:
connect(game, SIGNAL(userChoiceYes()), this, SLOT(test()));
然后当 btnYes 为 pressed/released 时,释放的信号被发射 - 你在 handleYesButton() 中选择它并发射你自己的信号,你的 window class 连接到并处理它在 test()
基于@code_fodder 的回答,但你甚至不需要另一个插槽,而且 QPushButton 的基本信号是 clicked()
。这是 documentation :
A push button emits the signal clicked() when it is activated by the
mouse, the Spacebar or by a keyboard shortcut. Connect to this signal
to perform the button's action. Push buttons also provide less
commonly used signals, for example pressed() and released().
首先,不要在 class Game
中添加另一个插槽,只需将按钮的信号 clicked()
连接到另一个信号 :
connect(btYES, SIGNAL(clicked()), this, SIGNAL(btYesClicked()));
当您按下按钮 btYes
时,来自 class Game
的信号现在会发出。现在,您只需 将此信号连接到 class Window
中的插槽 test()
:
connect(game, SIGNAL(btYesClicked()), this, SLOT(test()));
我在两个文件中有两个不同的class:
class Game: public QGraphicsView()
class Window: public QMainWindow()
{
public: Window();
Game *game;
public slots: void test() {game = new Game();};
}
在 Window.cpp
我使用 test() 函数开始一个新游戏:
Window::Window() {test();}
现在 Game.cpp
我用两个 QPushButton
QMessageBox
QMessageBox *box= new QMessageBox();
QPushButton *btYES = box->addButton(tr("YES"),QMessageBox::ActionRole);
QPushButton *btNO = box->addButton(tr("NO"),QMessageBox::ActionRole);
box->exec();
if (box->clickedButton() == btYES) {Window::test();}
if (box->clickedButton() == btNO) {close();}
如您所见,我想将函数 test()
连接到 Game.cpp
中的 btYES
,但该函数在 Window.cpp
中,其功能是启动一个新游戏。
可以吗?
QPushButton 在 pressed/released
时发出事件因此,您可以将释放信号连接到插槽:
connect(button, SIGNAL(released()), windowClass, SLOT(handleButton()));
在您的情况下,您需要通过 class 发送它,因此您可能需要分两步完成。
游戏中:
// connect the button to a local slot
connect(btYES, SIGNAL(released()), this, SLOT(handleYesButton()));
// in the slot emit a signal - declare the signal in the header
game::handleYesButton()
{
emit userChoiceYes();
}
在window
// connect the signal in game to a your slot:
connect(game, SIGNAL(userChoiceYes()), this, SLOT(test()));
然后当 btnYes 为 pressed/released 时,释放的信号被发射 - 你在 handleYesButton() 中选择它并发射你自己的信号,你的 window class 连接到并处理它在 test()
基于@code_fodder 的回答,但你甚至不需要另一个插槽,而且 QPushButton 的基本信号是 clicked()
。这是 documentation :
A push button emits the signal clicked() when it is activated by the mouse, the Spacebar or by a keyboard shortcut. Connect to this signal to perform the button's action. Push buttons also provide less commonly used signals, for example pressed() and released().
首先,不要在 class Game
中添加另一个插槽,只需将按钮的信号 clicked()
连接到另一个信号 :
connect(btYES, SIGNAL(clicked()), this, SIGNAL(btYesClicked()));
当您按下按钮 btYes
时,来自 class Game
的信号现在会发出。现在,您只需 将此信号连接到 class Window
中的插槽 test()
:
connect(game, SIGNAL(btYesClicked()), this, SLOT(test()));