简体   繁体   English

c++ QPushButton信号和槽

[英]c++ QPushButton signal and slot

I have a problem creating a QPushButton and linking its signal to my slots.我在创建QPushButton并将其信号链接到我的插槽时遇到问题。 First, I created a class with the slot:首先,我创建了一个带有插槽的 class:

class A : public QWidget{

  public slots:
    void handleButton();

};

There is my handleButton function in the.cpp在.cpp中有我的handleButton function

void A::handleButton(int row, int col){
    m_button->setText("Example");
    // resize button
    m_button->resize(100,100);
}

Then I want to connect the button.然后我想连接按钮。

QObject::connect(m_button, SIGNAL(clicked()), qApp, SLOT(handleButton()));

But I got an error when I start the application:但是当我启动应用程序时出现错误:

"No such slot"

Make sure that qApp is an object of class A (ie where your slot is defined).确保qApp是 class A的 object(即定义插槽的位置)。

That said, the signatures are wrong: a signal links to a slot only if the signature match也就是说,签名是错误的:只有签名匹配时,信号才会链接到插槽

http://qt-project.org/doc/qt-4.8/signalsandslots.html http://qt-project.org/doc/qt-4.8/signalsandslots.html

The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot.信号和槽机制是类型安全的:信号的签名必须与接收槽的签名相匹配。

And your slot hasn't the right signature:你的插槽没有正确的签名:

http://qt-project.org/doc/qt-4.8/qabstractbutton.html#clicked http://qt-project.org/doc/qt-4.8/qabstractbutton.html#clicked

void QAbstractButton::clicked ( bool checked = false ) [signal]

You have a few errors in this code, if you define "void handlebutton()" then you must implement void handlebutton() NOT void handlebutton(inx x, int y) this code should not even compile.这段代码中有一些错误,如果你定义了“void handlebutton()”,那么你必须实现 void handlebutton() NOT void handlebutton(inx x, int y) 这段代码甚至不应该编译。

More: in QT you CAN ONLY connect SIGNALS and SLOTS with the same parameters so you can connect SIGNAL(clicked()) with SLOT(handlebutton()) but not SIGNAL(clicked() with SLOT(handleButton(int, int)).更多:在 QT 中,您只能使用相同的参数连接 SIGNALS 和 SLOTS,因此您可以将 SIGNAL(clicked()) 与 SLOT(handlebutton()) 连接,但不能将 SIGNAL(clicked() 与 SLOT(handleButton(int, int)) 连接。

Another problem is that connect is executed at runtime so You must compile and run before Qt can show you the error.另一个问题是 connect 是在运行时执行的,因此您必须先编译并运行,然后 Qt 才能向您显示错误。

So a possible solution is:所以一个可能的解决方案是:

define and implement the slot void handlebutton() and connect that to the signal clicked(), then define another method handleButton (int x, int y) that you will call from inside handleButton().定义并实现插槽 void handlebutton() 并将其连接到信号 clicked(),然后定义您将从 handleButton() 内部调用的另一个方法 handleButton (int x, int y)。

I really hope that makes sense to you.我真的希望这对你有意义。

Your class definition should look like:您的 class 定义应如下所示:

class A : public QWidget
{
Q_OBJECT
public slots:
  void handleButton(int, int);
};

And you should connect it like:你应该像这样连接它:

QObject::connect(m_button, SIGNAL(clicked()),qApp, SLOT(handleButton(int a, int b)));

where a and b are variables for row and column.其中ab是行和列的变量。

This should work.这应该工作。 Try understanding basic C++. :)尝试了解基本的 C++。:)

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

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