Pergunta

how should the member of a QWidget-Class normally designed private or public?

class MyWidget : public QWidget {

   public:
       MyWidget( QWidget *parent = 0);
       QLineEdit *myLine;
}

or

class MyWidget : public QWidget {

   public:
       MyWidget( QWidget *parent = 0);

   private:
       QLineEdit *myLine;
}

I prefer the second definition with the private member, but this version is bad to test because I have no access to the member with the findChild( QString ).

Other part is should a QWidget class nested other widgets? Problem is there is no direct access to the nested widgets and this isn't really usefull for automated gui tests where the mouse click should mabyee push a button or something else..

Foi útil?

Solução

The second choice is generally regarded as the "best programming practice", and if you need to access the private members via code from somewhere else (including your test suite), then you need to implement getters and setters.

Generally, test frameworks should also be using APIs to access objects rather than directly accessing members, for it is that API that you typically want to test within unit tests. If you really can't give up the notion that the test framework should directly access the private members, then you can look into using C++ friend classes. But you didn't hear that from me.

If you need to access something generically (like by a string name), that's exactly what Qt Properties was designed to do for you. So declare your items as a property and list the getters/setters/signals/etc that way too.

Aside: Unfortunately, implementing getters and setters is somewhat boring work, which is why I ended up implementing a QtCreator plugin tool that implements getters, setters and creates signal definitions and emits the signal. If you ever need to actually do something more complex in the future, then simply remove the and replace the auto-getter/setter with your more complex definitions.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top