[Qt-interest] Sanity check on using dynamic_cast<> with Qt

K. Frank kfrank29.c at gmail.com
Tue May 11 00:32:34 CEST 2010


Hello List -

I get a QWidget* out of a QTableWidget cell and I need to cast it to
derived type.

Using C++'s dynamic_cast<> seems to do the trick, but I would like to check
that this is the Qt-approved way of doing things.  (I seem to recall that Qt has
its own casting support, but that it is restricted to QVariant.)  Is
this the right
approach?

More specifically,

   QTableWidget::cellWidget (row, column)

returns a QWidget*, previously set with

   QTableWidget::setCellWidget (row, column, new RadioBox())

where RadioBox is my class that derives from QGroupBox (which, itself, derives
from QWidget).

I need to access the value() member function of my RadioBox class, and I do so
as follows:

   int value = dynamic_cast<RadioBox*>(tableWidget->cellWidget (row,
column))->value();

Is this the preferred Qt way of doing this?

(Like I said, it seems okay to me, and it does seem to work, so I'm
looking for a
sanity check that I'm going down the right path.)

Thanks.


K. Frank



More information about the Qt-interest-old mailing list