[Qt-interest] how I can know the widget is editable?
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Fri Mar 20 17:12:14 CET 2009
Shabd Swarup V wrote on Friday, March 20, 2009 4:25 PM:
> Using metaObject className() would return the actual implemented
> class name and not the Qt base class name. I think this will not be
> suitable in your case.
Then I didn't understand the actual problem: in my suggestion that was exactly the idea that you get the *actual class* (or dynamic type of the instance).
So for example MySuperLineEdit (instead of just QLineEdit). Because I assumed that the "well known list of editable widgets" would be
- countable (e.g. around 30 widgets)
- well known (we don't deal with plugin-widgets, for example, which type we don't know in advance)
> Even if you use a QMap to store the list of "potentially editable"
> widgets, you would need to type cast to that type of widget to be
> able to call the appropriate method to find out if it is editable,
> eg.
> isReadOnly() in case of QLineEdit, QTextEdit.
Yes, but that would be only ONE dynamic typecast, as opposed to (potentially) 30 typecasts (as to find out if the widget is in the "list of well known editable widgets")
;)
Another possibility would be to use the Qt Meta Object system method inherits(): that would solve the problem if you don't want to deal with all the possible "MySuperLineEdit" widgets:
bool SomeClass::isWidgetEditable(const QWidget *widget) {
bool result;
if (widget->inherits(QLineEdit)) {
// here we can use a "static cast", because we KNOW now that we have a (sub-)class of QLineEdit (1)
QLineEdit *lineEdit = static_cast<QLineEdit *>(widget);
result = !lineEdit->isReadOnly() && lineEdit->isEnabled();
} else if (widget->inherits(QComboBox)) {
QComboBox *comboBox = static_cast<QComboBox *>(widget);
result = comboBox->isEnabled();
} else if (...) {
...
} else {
// we don't know this widget type
result = false;
}
return result;
}
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list