Reputation: 6378
I need to simulate a button click from C++ code. 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. 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();
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
It says clicked is a protected method. IS there any work around to solve this issue and meet my goal?
Upvotes: 3
Views: 5685
Reputation: 45665
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(); }
.
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; 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, e.g.: QMouseEvent event(QEvent::User, pos, Qt::LeftButton, button, stateKey);
I see you're using the BlackBerry Cascades system. It's not based on QWidgets, but they also use the QObject event system to process events in a similar way. So you should be able to apply the method from above.
Upvotes: 4
Reputation: 2522
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
maybe due this fact, it will not be necessary to create the signalhandler
class.
cheers
Upvotes: 0