Results 1 to 5 of 5

Thread: Difficult Cast I cannot figure out!

  1. #1
    Join Date
    Nov 2007
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Difficult Cast I cannot figure out!

    Hello, I've been having a really difficult time trying to figure out how to make the following cast work properly and haven't been able to. I want to assign one class into another class that is different, and yet has an identical definition. The two classes are QAbstractTextDocumentLayout::Selection and QTextEdit::ExtraSelection. They are identical structs so I should be able to assign one into the other with a proper cast, but I cannot figure it out. Here's what I want to do with all the cast operators I've tried taken out... I was able to get it to work by simply copying the QList into a QVector item by item, but it was bugging me that I couldn't figure out how to do it this way which is a lot cleaner

    Qt Code:
    1. QAbstractTextDocumentLayout::PaintContext ctx;
    2. ctx.selections = QVector<QAbstractTextDocumentLayout::Selection>::fromList(textEdit.extraSelections());
    To copy to clipboard, switch view to plain text mode 

    Thanks!

    Josh

  2. #2
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Difficult Cast I cannot figure out!

    You're trying to cast type QTextEdit::ExtraSelection to type QAbstractTextDocumentLayout::Selection.
    See method definition from QAssistant:

    QVector<T> QVector::fromList ( const QList<T> & list )
    Vector type and list types should be same.

  3. #3
    Join Date
    Nov 2007
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficult Cast I cannot figure out!

    I had tried this, but can't get it to work. What modifications would you make to my code to get it to compile?

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Difficult Cast I cannot figure out!

    You cannot cast different object types to one another unless casting operators are defined for them. Although both classes, in this case, have the same public variables, there is no guarantee that their internal, non-public structure is the same, or that it will remain so in the future. In short, you can't do this, because C++ won't allow you to compile such potentially dangerous and ill-structured code.

    Also, since the constituent QTextCursor and QTextCharFormat classes do not possess assignment, copy or similar operators, you cannot perform the assignment manually. At best, all you can do is examine the classes involved to see if they provide any functionality that will allow you to perform this operation on an item-by-item basis. The effort, though, seems dubious.

  5. #5
    Join Date
    Nov 2007
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficult Cast I cannot figure out!

    Thanks, that makes good sense, I wasn't thinking about that. I guess Qt should have made them the same type...

Similar Threads

  1. Draw figure underneath other figure
    By Mnemonic in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2010, 13:38
  2. Replies: 0
    Last Post: 4th November 2009, 06:12
  3. Replies: 3
    Last Post: 24th April 2009, 22:41
  4. Difficult:Dynamic Icon size change?!?
    By nupul in forum Qt Programming
    Replies: 4
    Last Post: 10th April 2006, 09:39
  5. Replies: 12
    Last Post: 3rd April 2006, 06:11

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.