3

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?

leemes
  • 44,967
  • 21
  • 135
  • 183
DesirePRG
  • 6,122
  • 15
  • 69
  • 114
  • 1
    Did you try to change public for protected here : `class signalhandler : public QObject` ? – Thomas Ayoub Nov 14 '13 at 08:58
  • 2
    All signals are protected (in Qt4) and the signal slot system is designed for signals to be emitted by their classes only (or private sub-classes etc.) I remember that QtTest module has some input simulation stuff. I think it generates events (rather than signals) which the widgets turn into signals like the events came from users (but they came from your program). Event generation is possible (in contrast to foreign signal emits). – leemes Nov 14 '13 at 08:59
  • I meant something like this function: [QTest::mouseClick](http://qt-project.org/doc/qt-4.8/qtest.html#mouseClick) – leemes Nov 14 '13 at 09:01
  • At @Samoth I did try after you said it, but the problem till exists – DesirePRG Nov 14 '13 at 09:02

3 Answers3

4

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.

leemes
  • 44,967
  • 21
  • 135
  • 183
0
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

Zaiborg
  • 2,492
  • 19
  • 28
0

Change

emit button->clicked();

To

emit button->click();
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144