Results 1 to 5 of 5

Thread: qobject_cast() and typeid() problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qobject_cast() and typeid() problem

    This code is invalid - as the type identification is done at runtime, the compiler can't decide what function to call when building the code. The correct code is:

    Qt Code:
    1. QLineEdit *le = qobject_cast<QLineEdit*>(activeWidget);
    2. if(le) le->paste();
    3. else {
    4. QPlainTextEdit *pte = qobject_cast<QPlainTextEdit*>(activeWidget);
    5. if(pte) pte->paste();
    6. else qFatal("Unknown widget found");
    7. }
    To copy to clipboard, switch view to plain text mode 

    Or a simple and brilliant abuse of one of Qt's features:
    Qt Code:
    1. QMetaObject::invokeMethod(activeWidget, "paste");
    To copy to clipboard, switch view to plain text mode 

    which makes use of a fact that paste() is a slot in both classes.

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

    codeslicer (23rd January 2009)

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.