Results 1 to 5 of 5

Thread: Changing the type of QList

  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 Changing the type of QList

    Hi friends,

    I was going though the qobject_cast of a QLineEdit to QWidget ... now i got the problem that im having a QList of QLineEdit and have to type cast it to QList of QWidget ..
    present i was typecasting each and every QlineEdit in the list in a for() loop..

    but is there any other way to change the type of a QList
    Ex:

    QList <QLineEdit *> lList can be typecasted to QList<QWidget*> as QLineEdit is inherited from QWidget ..


    Thanks

    Wagmare
    "Behind every great fortune lies a crime" - Balzac

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing the type of QList

    why do you want to do that?

  3. #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: Changing the type of QList

    Thanks for reply Felix

    Im having a common function having the argument
    Qt Code:
    1. QList<QWidget *>
    To copy to clipboard, switch view to plain text mode 
    say void myWidget::enableAll(QList<QWidget *> list)
    and
    im getting all the QLineEdit in one widget in a QList<QLineEdit *> lList using findChild and i have to pass to the function say
    enableAll(lList)..

    in this case i have to typecast each and every QLineEdit into QWidget so i can pass it as a argument ..

    ex:
    Qt Code:
    1. for(int i =0; i < lList.size(); i++)
    2. {
    3. lList[i] = qobject_cast<QWidget*>(lList[i]);
    4. }
    5.  
    6. enableAll(&lList);
    To copy to clipboard, switch view to plain text mode 


    but is there any other way i can easily cast a QList<QLineEdit> to QList<QWidget *>

    Thanks & Regards
    "Behind every great fortune lies a crime" - Balzac

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Changing the type of QList

    "enableAll()" could be a template method:
    Qt Code:
    1. class MyClass ... {
    2. protected:
    3. template< typename T >
    4. void enableAll( QList<T*>& widgets ){
    5. foreach( T * t, widgets ){
    6. t->setEnabled(true);
    7. }
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 
    You can use it like any other method now:
    Qt Code:
    1. QList<QWidget*> widgets;
    2. QList<QLineEdit*> lineEdits;
    3.  
    4. enableAll(widgets);
    5. enableAll(lineEdits);
    To copy to clipboard, switch view to plain text mode 
    If you use it with a type that does not have "setEnabled()" method, you will get a compile time error.

    ----
    edit:
    I think VC++ does not support member template methods, I'm using gcc, its supported there. If you are using a compiler which does not support this feature, just make this method a regular template (not a member of a class).

    ----
    @down:
    ok, my mistake
    Last edited by stampede; 19th October 2011 at 12:25. Reason: updated contents

  5. The following user says thank you to stampede for this useful post:

    wagmare (20th October 2011)

  6. #5
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing the type of QList

    Quote Originally Posted by stampede View Post
    I think VC++ does not support member template methods
    VS2008 does

Similar Threads

  1. Replies: 0
    Last Post: 6th May 2011, 22:18
  2. Mac OSX: QList<(type)>::detach_detach.* coming up unresolved.
    By rickbsgu in forum Installation and Deployment
    Replies: 0
    Last Post: 1st September 2010, 18:27
  3. Replies: 4
    Last Post: 20th August 2010, 13:54
  4. 'QList' does not name a type
    By Qt Coder in forum Qt Programming
    Replies: 3
    Last Post: 22nd September 2009, 14:28
  5. Replies: 7
    Last Post: 16th August 2009, 09:03

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.