interface
-
can i pass QLabel or QLineEdit to QWidget parameter?
i have this:
class Interface
{
protected:
void forStudents(QWidget w)
{
w->setVisible(false);
}
void forStaffs(QWidget);
}class A : Interface
{
QLabel label;
void func();
}
void A::func()
{
forStudents(label); // doesnt work
}
class B : Interface
{
QLineEdit lineedit;
}anyone can help me?
-
Hi, You can't just pass a QLable as QWidget.
you can cast it then pass it ass parameter but it's not recommended but in some cases it helps if you use it carefully.
it's better practice to pass exact type to function. but if you had to cast it's better to use less powerful casts of C++11 or Qt. -
Something does not look right in the code that you supplied, perhaps a typo when creating the post or the code is incorrect, for the function I would expect:
void forStudents(QWidget* w) // Note the pointer to a widget { w->setVisible(false); // This will not compile without the pointer argument since you use the arrow operator -> }
When this is done, you should be able to call the function as follow:
void A::func() { forStudents(&label); // Taking the address of the label }
It should work when working with pointers since QLabel is derived from QWidget.
Hope this helpsPS: Please use markup when posting code, it makes it easier to read and the intent clear.