Results 1 to 5 of 5

Thread: Odd QString/QChar Addr Issues

  1. #1
    Join Date
    Jul 2011
    Location
    Santa Clara, CA
    Posts
    13
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Odd QString/QChar Addr Issues

    So I'm working with a QTextEdit and came across some weird address issues...The code is below but the long and short of it is I'm seeing different addresses when I call constData() on the QString. I'm OK with this, sure, maybe QT changes the state the QString when you call constData(), but what I can't wrap my head around is that when first called in the for loop the addresses are equal, yet later they are not. Each iteration the same thing happens - they're back to being equal at the beginning of the loop. Consistently off by 0x800. The "c" pointer is invalid and dereferencing it returns 0. The full call yields the correct QChar info.

    Anybody have any idea what is going on? I feel like I'm caught in some weird "gotcha" but I have no idea what. I'm not even entirely sure what the difference is between top/bottom.
    The first addresses make sense since a QChar is 16bits, and the saved pointer is working as it should (it matches). Though for whatever reason that one call does not cooperate...

    Qt Code:
    1. const QChar *c = results_box->toPlainText().constData();
    2. for (int i = 0 ; i < results_box->toPlainText().size() ; ++i)
    3. {
    4. std::cout << " c=" << &(c[i]) << std::endl;
    5. std::cout << "full=" << &(results_box->toPlainText().constData()[i]) << std::endl;
    6. if (0)
    7. {
    8. std::cout << "Addr different" << std::endl;
    9. }
    10. else
    11. {
    12. std::cout << "Full call: " << &(results_box->toPlainText().constData()[i]) << " : " << (results_box->toPlainText().constData()[i]).toLatin1() << std::endl;
    13. std::cout << "Saved call:" << &(c[i]) << " : " << c[i].toLatin1() << std::endl;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    Qt Code:
    1. c=0000000000455818
    2. full=0000000000455818
    3. Full call: 00000000004560A8 : .
    4. Saved call:0000000000455818 :
    5. c=000000000045581A
    6. full=000000000045581A
    7. Full call: 00000000004560AA : .
    8. Saved call:000000000045581A :
    9. c=000000000045581C
    10. full=000000000045581C
    11. Full call: 00000000004560AC : \
    12. Saved call:000000000045581C :
    To copy to clipboard, switch view to plain text mode 

    TIA!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Odd QString/QChar Addr Issues

    Not sure what you are asking for.

    Are you wondering why different objects have different addresses?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Odd QString/QChar Addr Issues

    Anybody have any idea what is going on?
    Every time you call "results_box->toPlainText()" you are asking QTextEdit to return to you a temporary QString that contains the text edit content converted to plain text. As anda_skoa said, you're getting a different object every time, so it shouldn't be a surprise that they have different addresses.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Jul 2011
    Location
    Santa Clara, CA
    Posts
    13
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Odd QString/QChar Addr Issues

    OK, sorry for long reply, didn't get subscript notif. Sorry you guys didn't understand, let me try to explain better:

    Let's take a look at the outside (not in 'if' statement) calls:
    Qt Code:
    1. c=0000000000455818
    2. full=0000000000455818
    3. c=000000000045581A
    4. full=000000000045581A
    5. c=000000000045581C
    6. full=000000000045581C
    To copy to clipboard, switch view to plain text mode 

    So No, every time "toPlainText()" is called it's not returning a new object. It's returning the same memory (base at 0x455818). The addresses under "full" are 16 bit sequential and toPlainText() is called each time.. (818, 81A, 81C). This is fine. Rather expected behavior. I know you're not guaranteed that the object is the same each time - but it is here for each iteration.

    I'm concerned about the difference in behavior outside and inside.

    Obviously 'c' is pointing to same address as "results_box->toPlainText()" each call (which implies it's the same object returned each time), but inside (and only inside) the 'if' statement when I call: "results_box->toPlainText().constData()" it returns a new address (ok, fine, let's say that's expected because it returned a new object) but then AFTER (the next iteration) outside the 'if' they point to the same addr again. This behavior of matching and not matching is very confusing to me.

    So basically what happens is as follows:
    Qt Code:
    1. Iteration 0:
    2. Outside 'if': 'c' and full call point to same address (ie. 0x80) (EXPECTED
    3. Inside 'if': 'c' points to 0x80, full call points to 0x880 (MAYBE EXPECTED (if each call yields new object))
    4. Iteration 1:
    5. Outside 'if': 'c' and full call point to same address again (0x82) (NOT expected if we take assumption that full call returns new object)
    6. Inside 'if': 'c' points to 0x82 (EXPECTED) but full call points to arbitrary address ('c' no longer valid)
    7. Iteration 2:
    8. Outside 'if': 'c' and full call point to same address (NOT expected, especially since 'c' was just invalid at end of iteration 1)
    9. ...
    To copy to clipboard, switch view to plain text mode 

    So basically I'm concerned with the difference in behavior between inside and outside the 'if'. You guys are saying that it's returning a new object each time. If that's the case then it's strange that outside the 'if' statement it literally returns the same exact base address for each of the many iterations. OUTSIDE the 'if' the print doesn't reflect what you're saying, but INSIDE the 'if' statement it does. I don't get how that's possible.

    Synopsis:
    You're likely saying "why do you care?" My code (how I stumbled on this issue) was originally as follows:
    Qt Code:
    1. const QChar *c = results_box->toPlainText().constData();
    2. for (int i = 0 ; i < results_box->toPlainText().size() ; ++i)
    3. {
    4.  
    5. if (0)
    6. {
    7. //never goes in here, I forgot what it was originally..
    8. }
    9. else
    10. {
    11. std::cout << "Saved call:" << &(c[i]) << " : " << c[i].toLatin1() << std::endl; //!!!! c[i] returns ASCII 0 for all values, completely unexpected to me - I just wanted a pointer to the char data inside the field.
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    I was originally thinking as you guys are saying that each call will return a new QString object each time and the QString is no longer valid but when I printed the addresses outside the 'if' statement in the for loop it all shows the same object at the original base

    Hope that elaborates what I originally tried (and what went wrong) and how I got lead into this interesting scenario.
    Last edited by Syndacate; 6th March 2017 at 18:57.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Odd QString/QChar Addr Issues

    Quote Originally Posted by Syndacate View Post
    I was originally thinking as you guys are saying that each call will return a new QString object each time and the QString is no longer valid
    Exactly.
    That's what it does.

    Quote Originally Posted by Syndacate View Post
    but when I printed the addresses outside the 'if' statement in the for loop it all shows the same object at the original base
    Since the data gets temporarily allocated and the deallocated, the next allocation can sometimes happen at the same memory location.

    Why do you even want to convert the text edit's content into plain text multiple times?
    The resulting string content won't change if the internal content doesn't change.

    Cheers,
    _

Similar Threads

  1. Convert QString to QChar
    By davinciomare in forum Newbie
    Replies: 1
    Last Post: 12th October 2016, 09:36
  2. Casting Byte to QChar or QString
    By Habie in forum Qt Programming
    Replies: 1
    Last Post: 16th December 2015, 20:27
  3. From QString to QChar
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2009, 03:14
  4. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 00:38
  5. Q3PopUpmenu: Qchar * to QString
    By user_mail07 in forum Qt Programming
    Replies: 2
    Last Post: 1st February 2007, 20:33

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.