Results 1 to 7 of 7

Thread: Problem with qobject_cast

  1. #1
    Join Date
    Jul 2009
    Location
    Oulu, Finland
    Posts
    23
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Problem with qobject_cast

    Hi there,

    I have some strange problem with the qobject_cast. I was successfully using the same code at the another class and after I moved the code to the new class it won't work anymore. If I build the project with the debug configuration and run it at the debug mode I can't even debug it normally because debugger stops automatically at the qmetaobject.cpp files "QObject *QMetaObject::cast(QObject *obj) const" function. And this happens even though I haven't set the breakpoint in it... And it won't continue debugging through there even I press "Continue".

    Here's the code where I use qobject_cast... the problem goes away if I comment out these lines. So the problem should be related to this, I assume

    Note that at the code the "component" is type QWidget *component
    Qt Code:
    1. void GUIComponent::setComponentOptions(const QString componentOptions)
    2. {
    3. options = componentOptions;
    4.  
    5. if(QSlider *slider = qobject_cast<QSlider *>(component))
    6. {
    7. if(options == "horizontal")
    8. slider->setOrientation(Qt::Horizontal);
    9. }
    10. else if(QPushButton *button = qobject_cast<QPushButton *>(component))
    11. {
    12. button->setText(options);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    Probably it is just some stupid mistake like usually

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qobject_cast

    Where is "component" initialized? Are you sure it's initialized and it's a valid pointer? Could you provide a backtrace when you application crashes?

    Appart from that, I would recommend you not to declare the pointer in the same if():

    Qt Code:
    1. QSlider *slider = qobject_cast<QSlider *>(component);
    2. if(slider) {
    3. if(options == "horizontal")
    4. slider->setOrientation(Qt::Horizontal);
    5.  
    6. } else {
    7. QPushButton *button = qobject_cast<QPushButton *>(component);
    8. if(button)
    9. button->setText(options);
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with qobject_cast

    what (type especially) is "component"?

    (btw, I disagree on the "don't declare a pointer in an if" thing.)

  4. #4
    Join Date
    Jul 2009
    Location
    Oulu, Finland
    Posts
    23
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qobject_cast

    component type: QWidget *

    So it's a pointer to a QWidget but it usually points to some specific widget like for example QSlider, QPushButton or anything that is inherited from QWidget.

    The widget gets initialized at the another class (xml parser) and pointer to that widget is then passed on to the "component" with setter function.

    Qt Code:
    1. void ServiceDescriptionParser::readComponentType(GUIComponent *component)
    2. {
    3. Q_ASSERT(isStartElement() && name() == "type");
    4.  
    5. QString type = readElementText();
    6.  
    7. if(type == "slider") {
    8. QSlider *slider = new QSlider();
    9. component->setComponent(slider);
    10. qDebug() << "GUI Element Type: " << type;
    11. }
    12. else if (type == "button") {
    13. QPushButton *button = new QPushButton();
    14. component->setComponent(button);
    15. qDebug() << "GUI Element Type: " << type;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    And the setter function...

    Qt Code:
    1. void GUIComponent::setComponent(QWidget *component)
    2. {
    3. this->component = component;
    4. parentService->addWidget(this->component);
    5. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qobject_cast

    Are you 100% sure you are not calling setComponentOptions() before setComponent()? Did you set component to 0 in the constructor to avoid having a pointer to some arbitrary (invalid) place in the memory between the moment where the class is instanced and setComponent() is called? Are you also sure the component is not deleted somewhere else without calling setComponent()? You could use QPointer in order to prevent this situation from happening.

  6. #6
    Join Date
    Jul 2009
    Location
    Oulu, Finland
    Posts
    23
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qobject_cast

    Thank you guys for your replies.

    I got it solved... it was just a stupid mistake, as I thought

    The problem was that I didn't set the "component" pointer to NULL at the start and therefore I didn't even put any inspection before trying to do the casting.

    So the solution was to set the "component" to NULL when I initialize the instance which includes the "component" and then do the inspection...

    Qt Code:
    1. if(component != NULL)
    To copy to clipboard, switch view to plain text mode 

    ...before the qobject_cast functions.

  7. #7
    Join Date
    Jul 2009
    Location
    Oulu, Finland
    Posts
    23
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Problem with qobject_cast

    Seems like you would have solved the problem also Victor, we posted the solution for this problem simultaneously

    I spent last friday struggling with this problem and after the weekend the solution just came to me... so obvious. This just shows how the coding works, you just need some breaks for any now and then to get some fresh ideas, because when you stare your own code too long time you just become "numb" for it.

Similar Threads

  1. Problem with QAbstractListModel
    By eekhoorn12 in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2009, 14:26
  2. Replies: 1
    Last Post: 23rd April 2009, 09:05
  3. Replies: 19
    Last Post: 3rd April 2009, 23:17
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  5. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36

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.