1

I need to simulate mouse click on UI button using QTest but I can't figure out how to access it.

I've got a MediaPanel class :

class PhMediaPanel : public QWidget
{
    Q_OBJECT

public:
    explicit PhMediaPanel(QWidget *parent = 0);
    //... a lot of functions
private:
    Ui::PhMediaPanel *ui;

};

And a MediaPanelTest :

#include "MediaPanelTest.h"
#include <QObject>

class MediaPanelTest : public QObject
{
    Q_OBJECT
public:
    explicit MediaPanelTest(QObject *parent = 0);

private slots:
    //The tests
};

So how can I simulate button click on Ui::PhMediaPanel *ui member?

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • Create your widget as usual and call [QTest::mouseClick](http://qt-project.org/doc/qt-4.8/qtest.html#mouseClick) with the member panel widget as parameter. – NoDataDumpNoContribution Jun 13 '14 at 13:37
  • @Trilarion And how can I know which button is clicked ? (It have 6 buttons) – Thomas Ayoub Jun 13 '14 at 13:38
  • OP wants to click on a specific button defined in the UI. To use `mouseClick` OP has to know the position of the button. I would declare the UI pointer protected and derive a test class from your Widget class. In this case you can access the UI pointer. – OnWhenReady Jun 13 '14 at 13:41
  • @DominikSelzer Unfortunately, I can't make it protected (I'm not the only one working on this). Do I have to make it with buttons positions ? – Thomas Ayoub Jun 13 '14 at 13:44
  • I misunderstood the question. Maybe you could search through the [children](http://doc.qt.digia.com/qq/qq03-big-brother.html) by name or order? – NoDataDumpNoContribution Jun 13 '14 at 13:50

1 Answers1

3

Try the following approach:

BUTTONCLASS* button = WIDGET->findChild<BUTTONCLASS*>("name of the button");

As far is i know this should give you the widget without exposing the UI pointer.

OnWhenReady
  • 939
  • 6
  • 15