How to pass a Qwidget to a function into a children class ?
I know that it is a recurrent theme, but I dont know to do this :
I want to pass two different widgets to a function into a class I have instantiated from main app.
The two different widgets can be Qlabel and /or QTextedit.
So, I'd want to have a function like this
Myfuntion(Qwidget a_widget) {
A_widget_to_remember=a_widget
.... }
Later in another function I want to do something with 'a_widget_to_remember'
if A_widget_to_remember=Qlabel ....
if A_widget_to_remember=QTextedit ....
1.- How I must to pass the label or Qtextedit to the function ?
Using pointers ? Can anyonbe give some easy code ?
2.- How can I identify what kind of Qwidget is 'A_widget_to_remember'
Thanks.
Re: How to pass a Qwidget to a function into a children class ?
- You clearly want to pass a pointer to your object, otherwise how can you keep track of it? Also, you might want to have a look at overloaded functions.
- There are different possible approaches, depending exactly on what you want to do, how generic a solution you want it to be. The easiest would be to have two different variables in your class, one for QLabel and another for QTextEdit, and a flag that tells you which should be used.
Re: How to pass a Qwidget to a function into a children class ?
Quote:
Originally Posted by
tonnot
1.- How I must to pass the label or Qtextedit to the function ?
Using pointers ? Can anyone give some easy code ?
Pass the widgets by pointer. You cannot pass them by value, even if that made sense, because they do not have the requisite copy constructor.
Code:
class MyClass {
...
private:
};
{
aWidget = widget;
}
Quote:
2.- How can I identify what kind of Qwidget is 'A_widget_to_remember'
QObject qobject_cast to the relevant type and check the returned pointer.
Re: How to pass a Qwidget to a function into a children class ?