Results 1 to 4 of 4

Thread: From QObject to QWidget

  1. #1
    Join Date
    Jan 2006
    Posts
    14
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Cool From QObject to QWidget

    Suppose I have QObject* obj and I know it's a widget, how can I obtain the QWidget* widget corresponding to obj?

    Qt Code:
    1. const QToolBox* tb = (const QToolBox*) parent;
    2. QObjectList *z = tb->queryList("QWidget", 0, FALSE, FALSE);
    3. QObjectListIt it(*z);
    4. QObject *obj;
    5. int index = 0;
    6. while ( (obj = it.current()) != 0 )
    7. {
    8. ++it;
    9. QRect tab = ( (QWidget*)obj )->rect();
    10. if ( tab.contains(childRect.center()) )
    11. {
    12. index = tb->indexOf( (QWidget*)obj );
    13. break;
    14. }
    15. }
    16. delete z;
    To copy to clipboard, switch view to plain text mode 

    I don't know why but in this example ( (QWidget*)obj )->rect() works but indexOf( (QWidget*)obj ) doesn't. index is always -1. So I'm looking for something to replace (QWidget*)obj and obtain the correct result.

    AFAIK the rest is OK and works as expected but if you see a flaw, don't hesitate to say so. I've spent so much time staring at this code that I can't see anything.

  2. #2
    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: From QObject to QWidget

    It's not the matter of casting that QObject -- it is done (more or less) correctly. You could be using dynamic_cast instead, but let's say it's ok. Looks like that widget is not a child of that toolbox. remember indexOf will return only direct children (in the meaning of "pages", not Qt parent-child relation) of the toolbox, not their children or any other widgets.

    What exactly do you want to do?

  3. #3
    Join Date
    Jan 2006
    Posts
    14
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: From QObject to QWidget

    "Pages" is exactly what I'm interested in, not the widgets they contain... and what I'm trying to do is to retrieve the index of the pages.

    I'm writing a style plugin and when the time comes to draw a toolbox tab, all that Qt condescends to give me is a pointer to the toolbox widget (the parent of the tabs) and the QRect in which I'm supposed to draw one tab.

    I would like to retrieve the widget corresponding to this QRect and then its index. It looks like it isn't as easy as one may think. QToolbox doesn't provide enough informations like QTabBar with its "regular" tabs... so I have to hack a workaround.

  4. #4
    Join Date
    Feb 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: From QObject to QWidget

    I am not an expert in programming but all I could notice is that your QToolBox * tb has a const in front of it whcih means that you cannot change the data type hence no casting. That's where you invoke tb->indexOf where tb is of a consts datatype.

Similar Threads

  1. QtScript Binding problem with QWidget
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 09:38
  2. Replies: 0
    Last Post: 11th November 2008, 15:36
  3. Dynamic updates of a QWidget in a QScrollArea
    By plamkata in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2008, 23:45
  4. QObject and copy Constructors
    By December in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2008, 16:14
  5. Replies: 3
    Last Post: 8th March 2007, 14:54

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.