PDA

View Full Version : focusChanged() getting qwidget type



noobasaurus
6th April 2009, 20:35
Hello,

I need some help with the QApplication focusChanged() signal.

I have 2 classes lineedit and combobox which inherit from the Qt widget clases corresponding to those (QLineEdit, QComboBox). They hold some list of old data that was entered in them.

What im trying to do is display this information into a qlistWidget when the lineedit and comboboxes are clicked. I decided to use focusChanged since i have a lot of them (50+ over a large app)

i have a slot that takes 2 parameters (setWidgets(QWidget*, QWidget*)) the old and new widgets.


connect(app, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(setWidgets(QWidget*, QWidget*)));


I was wondering how i could tell what type these QWidgets are so i can qobject_cast to the right one(lineedit or combobox) and retrieve these lists of information. Is there even a way?

talk2amulya
6th April 2009, 21:03
u can use the meta object information available to every QObject:


object->metaObject()->className(); // this will give u proper class name

wysota
6th April 2009, 21:18
I was wondering how i could tell what type these QWidgets are so i can qobject_cast to the right one(lineedit or combobox) and retrieve these lists of information. Is there even a way?

qobject_cast is there exactly because of the purpose of casting without knowing the right type.

QLineEdit *le = qobject_cast<QLineEdit*>(widget);
QComboBox *cb = qobject_cast<QComboBox*>(widget);
At least one of the above pointers will be null. If a pointer is not null it means Qt was able to cast the object properly to the required type.

If you knew the type, you could have cast it directly.


if(widget->inherits("QComboBox")){
QComboBox *cb = (QComboBox*)widget;
//...
} else if(widget->inherits("QLineEdit")){
QLineEdit *le = (QLineEdit*)widget;
// ...
}

noobasaurus
6th April 2009, 23:10
Thank you very much guys for your prompt help on this.
Im using this below. I didnt know this is available because im really new to QT. I learned something new!


u can use the meta object information available to every QObject:


object->metaObject()->className(); // this will give u proper class name

wysota
6th April 2009, 23:15
If you use qobject_cast, there is really no point in comparing class names yourself using QMetaObject::className(). Just make the cast and see if resulting pointer is null or not. And remember meta-object for a class is only generated if the class contains the Q_OBJECT macro.

noobasaurus
7th April 2009, 00:49
If you use qobject_cast, there is really no point in comparing class names yourself using QMetaObject::className(). Just make the cast and see if resulting pointer is null or not. And remember meta-object for a class is only generated if the class contains the Q_OBJECT macro.

Awesome! I actually did it this way. Just casted it and if it wasnt null did what i wanted and it works correctly! :) thanks for your help! Yeah ive gotten the classes written correctly ive seen people forget to include the Q_OBJECT line a lot.