Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString issue
QtWS25 Last Chance

QString issue

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vamorian
    wrote on last edited by
    #1

    So my problem here is this : I want to define a QString in a function give it a value and then use it in another Function....how do i do this ?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      welcome to devnet

      "Well, what is still open with this guideline?":http://doc.qt.io/qt-5/qstring.html#details

      @
      #include <QString>
      #include <QDebug>

      void otherFoo ( const QString & refStr )
      {
      qDebug() << refStr;
      }

      void foo ()
      {
      QString str = "Hello";
      otherFoo ( str );
      }
      @

      Note: just typed, not tested.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SysTech
        wrote on last edited by
        #3

        I'm just getting back into c++ after a number of years but in general you have to be careful of scope.

        if you have a function and you use a QString in that function it might go out of scope:

        @
        void somefunction()
        {
        QString myString( "Hello World" );
        }
        @

        myString only lives while in somefunction.

        But in my past you can pass things by reference so you could so something like:

        @
        void funca()
        {
        QString myString("Hello World");

        funcb( &myString );
        }

        void funcb( QString *stg )
        {
        stg-> ... do whatever ...
        }
        @

        You can also new up your own version and keep it around until you want to delete it.

        [edit: added missing coding tags @ SGaist]

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          There are almost no usecases where you would want to use a pointer or new with QString.
          If you want to pass it into a function do it by reference (like koahnig suggested).
          If you need a copy somewhere don't hesitate either. QString, like many other Qt types uses PIMPL idiom and copy on write, so it's pretty cheap to pass aound.
          Naked pointers lead do leaks and maintanace problems. This is true for simple types like QString especially.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SysTech
            wrote on last edited by
            #5

            @Chris,

            I admit to being out of touch with c++ for years and also being new to Qt.

            Having not studied QString I admit to naivety. However having worked for so long with languages where you as the coder must keep track of things I'm used to being very very aware of pointers and references.

            I understand new paradigms offer greater protection but frankly a pointer is a pointer is a pointer. If you know what you are doing memory leaks don't happen.

            But I will listen and learn. Thanks for the input!

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              bq. I’m used to being very very aware of pointers and references.

              That's great. I'm all for it. If anything it makes you a better coder. Qt is a great library that keeps a lot of that stuff from you, but knowing how an engine works makes you a better driver even if you don't plan on building a car yourself :) You can also see a kind of new wave in c++ where naked pointers are becoming more and more faux pas, with light smart pointers and "RAII":http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization as the preferable way of resource management.
              Anyway, a QString is really a pointer to internal data structure that has some storage, so having a pointer to it is almost like char*** - a level of indirection that you neither want nor need (most of the time).

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                Hi,

                To add to Chris' notes, Qt has numerous "value" classes that are intended to be used this way -- allocated on the stack, not the heap. See the article on "Implicit Sharing":http://doc.qt.io/qt-5/implicit-sharing.html for more info.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  In general the great advantage of C++ is references. Besides making the typing a bit easier it is the avoidance of memory leaks. Finally the const declaration of reference helps to avoid non-intended changes.
                  For someone switching from C to C++ that is not obvious and a real nuisance at start. However, after getting used you really appreciate those features. They save lots of debugging hours.
                  IMHO pointers shall be used when absolutely necessary, but not when they are also a way to do.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SysTech
                    wrote on last edited by
                    #9

                    Thanks all. I still have a lot to learn. It is great to have so many people here that are willing to set others straight on better ways to do things.

                    I do see that from my early C++ days things have definitely moved from straight pointers. So more reading, more work and hopefully I'll save myself hours of debugging and time!

                    All great info thanks!

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved