简体   繁体   English

从C ++代码发出BB级联按钮的“ clicked()”信号

[英]Emit a “clicked()” signal for a BB Cascades button from C++ code

I need to simulate a button click from C++ code. 我需要模拟C ++代码中的按钮单击。 I have written the following code so far 到目前为止,我已经编写了以下代码

/*
 * signalhandler.hpp
 *
 *  Created on: Nov 14, 2013
 *      Author: fnazeem
 */

#ifndef SIGNALHANDLER_HPP_
#define SIGNALHANDLER_HPP_

#include <QObject>



namespace bb
{
    namespace cascades
    {
        class Button;
    }
}

class signalhandler : public QObject
{
    Q_OBJECT

public:
    signalhandler();

public slots:
    void onClicked(bb::cascades::Button *button);



};


#endif /* SIGNALHANDLER_HPP_ */

in signalhandler.cpp so far this is what i have wriiten. 到目前为止,在signalhandler.cpp中这就是我写的内容。 Assume that I have the address of a specific button, and I want to emit a "clicked" signal for that button, without manually clicking it. 假设我有一个特定按钮的地址,并且我想为该按钮发出“被单击”信号,而无需手动单击它。

/*
 * signalhandler.cpp
 *
 *  Created on: Nov 14, 2013
 *      Author: fnazeem
 */


#include "signalhandler.hpp"
#include <bb/cascades/Button>



void signalhandler::onClicked(bb::cascades::Button *button){



    emit button->clicked();

}

When I build the project I get an error in emit button->clicked(); 当我构建项目时,我在emit button->clicked();出错emit button->clicked();

this is the error 这是错误

C:/bbndk/target_10_1_0_1020/qnx6/usr/include/bb/cascades/controls/abstractbutton.h:60:14: error: 'void bb::cascades::AbstractButton::clicked()' is protected C:/bbndk/target_10_1_0_1020/qnx6/usr/include/bb/cascades/controls/abstractbutton.h:60:14:错误:“ void bb :: cascades :: AbstractButton :: clicked()”受保护

It says clicked is a protected method. 它说单击是一种受保护的方法。 IS there any work around to solve this issue and meet my goal? 有什么解决的方法可以解决这个问题并达到我的目标?

You can't (shouldn't) simply emit signals from outside of the target class (sender), unless the class is designed to allow it. 您不能(不应该)简单地从目标类(发送者)外部发出信号,除非该类被设计为允许它。

What you should do instead is force the class to emit the signal by itself. 相反,您应该执行的是强制类自行发出信号。 If you designed the class (which is not the case here), you typically add a function which simply emits the signal, like for example emitClicked() { emit clicked(); } 如果您设计了类(这里不是这种情况),通常会添加一个简单地发出信号的函数,例如, emitClicked() { emit clicked(); } emitClicked() { emit clicked(); } . emitClicked() { emit clicked(); }

Some classes already have such functions. 一些类已经具有此类功能。 But if they don't, you need another trick: 但是,如果没有,您还需要另一个技巧:

If you're dealing with QWidgets , you can generate user input events which are processed as if the user really clicked on a specific screen coordinate; 如果您正在使用QWidgets ,则可以生成用户输入事件,这些事件的处理就像用户真正点击了特定的屏幕坐标一样。 taking all conditions and states into account that would normally also be considered (not necessarily resulting in emitting a signal). 考虑到通常也会考虑的所有条件和状态(不一定会导致发出信号)。 That can be done by calling qApp->notify(widget, event) where event is a pointer to an QEvent instance initialized before this call, eg: QMouseEvent event(QEvent::User, pos, Qt::LeftButton, button, stateKey); 这可以通过调用qApp->notify(widget, event)来完成,其中event是指向在此调用之前初始化的QEvent实例的指针,例如: QMouseEvent event(QEvent::User, pos, Qt::LeftButton, button, stateKey);

I see you're using the BlackBerry Cascades system. 我看到您正在使用BlackBerry Cascades系统。 It's not based on QWidgets, but they also use the QObject event system to process events in a similar way. 它不基于QWidgets,但它们也使用QObject事件系统以类似方式处理事件。 So you should be able to apply the method from above. 因此,您应该能够从上面应用该方法。

void QAbstractButton::click () [slot]

should do the work for you. 应该为您完成工作。 it will emit the clicked() signal as well as all functionality combined with the click behavior of the button 它将发出clicked()信号以及所有功能以及按钮的点击行为

maybe due this fact, it will not be necessary to create the signalhandler class. 也许由于这个事实,所以没有必要创建signalhandler类。

cheers 干杯

Change 更改

emit button->clicked();

To

emit button->click();

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

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