Results 1 to 6 of 6

Thread: focusChanged() getting qwidget type

  1. #1
    Join Date
    Mar 2009
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default focusChanged() getting qwidget type

    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.

    Qt Code:
    1. connect(app, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(setWidgets(QWidget*, QWidget*)));
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: focusChanged() getting qwidget type

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

    Qt Code:
    1. object->metaObject()->className(); // this will give u proper class name
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: focusChanged() getting qwidget type

    Quote Originally Posted by noobasaurus View Post
    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.
    Qt Code:
    1. QLineEdit *le = qobject_cast<QLineEdit*>(widget);
    2. QComboBox *cb = qobject_cast<QComboBox*>(widget);
    To copy to clipboard, switch view to plain text mode 
    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.

    Qt Code:
    1. if(widget->inherits("QComboBox")){
    2. QComboBox *cb = (QComboBox*)widget;
    3. //...
    4. } else if(widget->inherits("QLineEdit")){
    5. QLineEdit *le = (QLineEdit*)widget;
    6. // ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:

    certqt (15th November 2010)

  5. #4
    Join Date
    Mar 2009
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: focusChanged() getting qwidget type

    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!

    Quote Originally Posted by talk2amulya View Post
    u can use the meta object information available to every QObject:

    Qt Code:
    1. object->metaObject()->className(); // this will give u proper class name
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: focusChanged() getting qwidget type

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Mar 2009
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: focusChanged() getting qwidget type

    Quote Originally Posted by wysota View Post
    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.

Similar Threads

  1. Convert between a custom data type wrapped in a QVariant
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 09:07
  2. Replies: 0
    Last Post: 11th November 2008, 15:36
  3. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  4. QWidget and type
    By nowire75 in forum Newbie
    Replies: 1
    Last Post: 8th November 2007, 19:55
  5. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.