0

I'm trying to write a function, which gets a List containing some QWidget's from the UI. The function should take each widget, detect its Class and do some checks.
The Problem is, that I don't really know how to access e.g. the QLineEdit's functions when I'm in fact dealing with QWidget.
Is there a way to get this working? Or is there another completely different approach?

bool CLASS::inputValid(QList<QWidget *> widgetList) {
bool everythingValid = true;

foreach (QWidget *widget, widgetList) {
  if (QString(widget->metaObject()->className()) == "QLineEdit") {
      // check if text is empty
         if(widget->text()->length() == 0)  // I know this doesn't work this way
         everythingValid = false;
  }

  else if (QString(widget->metaObject()->className()) == "QTextEdit") {
      // check if text is empty
          if(widget->text()->length() == 0) // I know this doesn't work this way
          everythingValid = false;
  }
}

return everythingValid;

}
  • You are trying to access derived class methods from a pointer to base class. You can do a `static_cast` to `QLineEdit*`. Check this [link](http://stackoverflow.com/questions/2436125/c-access-derived-class-member-from-base-class-pointer) – Noel Jun 16 '15 at 09:09
  • easy as that. exactly what I needed! Just did not know how to search for. – Ricardo Lopes Jun 16 '15 at 09:17
  • 1
    Use [`qobject_cast`](http://doc.qt.io/qt-4.8/qobject.html#qobject_cast) instead of `static_cast`. That way you can check if the cast was successful. With `static_cast` you will have to know the type of the object you are casting. – thuga Jun 16 '15 at 09:23

0 Answers0