Results 1 to 5 of 5

Thread: qobject_cast() and typeid() problem

  1. #1
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy qobject_cast() and typeid() problem

    Hello,

    In my application, I have declared in the header file:

    Qt Code:
    1. QWidget *activeWidget
    To copy to clipboard, switch view to plain text mode 

    activeWidget basically represents the QWidget in my application which has input focus. In my Edit menu, I have an option called Paste, and upon clicking that I want the text from the clipboard to be pasted into the activeWidget, which can be a reference to either QLineEdit or QPlainTextEdit. However, this code is incorrect:

    Qt Code:
    1. connect(actionPaste, SIGNAL(triggered()), activeWidget, SLOT(paste()));
    To copy to clipboard, switch view to plain text mode 

    So, I have a function in my program called paste(). The new connection script is like this:

    Qt Code:
    1. connect(actionPaste, SIGNAL(triggered()), this, SLOT(paste()));
    To copy to clipboard, switch view to plain text mode 

    In the paste() function, I want to use typeid() to determine the type of the activeWidget pointer, which can be either QLineEdit or QPlainTextEdit. Both support the paste() slot. So, I was thinking of doing something like:

    Qt Code:
    1. activeWidget = someQLineEditWidget;
    2.  
    3. ...
    4.  
    5. void MyApp::paste() {
    6. qobject_cast<typeid(activeWidget*)>(activeWidget)->paste()
    7. }
    To copy to clipboard, switch view to plain text mode 

    So what I am trying to do is to find the type of widget activeWidget is (in this example, QLineEdit) during runtime using typeid(), and then cast activeWidget to that type, and use the paste() function of the QLineEdit. But with that code I get a bunch of errors :

    activeWidget cannot appear in a constant-expression
    'typeid' operator cannot appear in a constant-expression
    I know I can just check the type using "if" and "else" tags, but I want to try using typecasting as well, as it could be useful in the future.

    Thanks in advance,
    codeslicer

  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: 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.

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

    codeslicer (24th January 2009)

  4. #3
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: qobject_cast() and typeid() problem

    Thanks a lot, especially for the invokeMethod(); this will help a lot.

  5. #4
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qobject_cast() and typeid() problem

    It's just that the QLineEdit and QPlainTextEdit are made in Designer, but the 2nd code is all I need.

  6. #5
    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: qobject_cast() and typeid() problem

    Quote Originally Posted by codeslicer View Post
    It's just that the QLineEdit and QPlainTextEdit are made in Designer,
    I don't think that matters in any way. Or maybe I just don't see what you mean...

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.