Results 1 to 6 of 6

Thread: typecast of QWidget

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default typecast of QWidget

    hi friends,

    i got a problem in QList
    as i subject all my object in a Widget class to a QList by
    Qt Code:
    1. QList wlist;
    2. wlist = widget->findChildren<QWidget *>();
    To copy to clipboard, switch view to plain text mode 

    this list have QLabel, QTextEdit, etc ..
    but all were typecast to QWidget in the QList ..

    now i want to access a label in wlist.at(2) .. which currently returning a QWidget pointer ..

    please help ..
    "Behind every great fortune lies a crime" - Balzac

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: typecast of QWidget

    Have you tried qobject_cast?

  3. The following 2 users say thank you to numbat for this useful post:

    Kyef (23rd October 2016), wagmare (11th August 2009)

  4. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: typecast of QWidget

    thanks for reply ..
    yes but i dont know right way to access to on QList.at(i) ...

    how to call a typical QLabel function ex:setText on the QList item with type cast ..
    "Behind every great fortune lies a crime" - Balzac

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: typecast of QWidget

    try this
    Qt Code:
    1. ...
    2. QList<QWidget *> wlist = widget->findChildren<QWidget *>();
    3. QLabel *label = qobject_cast<QLabel *>(wlist.at(0));
    4. if (!label)
    5. return;
    6. label->setText("some text");
    7. ...
    To copy to clipboard, switch view to plain text mode 
    Last edited by spirit; 11th August 2009 at 12:13.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. The following user says thank you to spirit for this useful post:

    wagmare (11th August 2009)

  7. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: typecast of QWidget

    yeah it worked .. thanks to all ..
    "Behind every great fortune lies a crime" - Balzac

  8. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: typecast of QWidget

    or you can made even simpler
    Qt Code:
    1. QList<QLabel *> labels = widget->findChildren<QLabel *>();
    2. if (!labels.count())
    3. return;
    4. labels.at(0)->setText("some text");
    5. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Promoting the parent QWidget of a QWidget form
    By extrakun in forum Qt Tools
    Replies: 6
    Last Post: 16th April 2010, 14:19
  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. Error in put one QWidget in another QWidget
    By xjtu in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2008, 16:05
  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
  •  
Qt is a trademark of The Qt Company.