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 pointer to hand over and get back in signal
Qt 6.11 is out! See what's new in the release blog

QString pointer to hand over and get back in signal

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 2.7k Views 1 Watching
  • 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.
  • J.HilkJ J.Hilk

    @Kutyus
    why don't you pass it as Reference ?

    public:
       Worker(QObject *parent = nullptr) : QObject(parent){
            QObject::connect(this, &Worker::modThisString, this, &Worker::modTheString);
            QString str ("foo");
            qDebug() << str;
            emit modThisString(str);
            qDebug() << str;
        }
    
    public slots:
    
        void modTheString(QString &s) { s = s+ "bar";}
    
    signals:
        void modThisString(QString &s);
    

    result in

    "foo"
    "foobar"

    that said, I don't think this is a good design choice.

    KutyusK Offline
    KutyusK Offline
    Kutyus
    wrote on last edited by
    #5

    @J.Hilk I passed the reference, but the signal parameter is integer type.
    I should convert it to char *, and back to QString in the slot?

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      What exactly are you trying to achieve by that ?

      Why use an int to pass a pointer ? The size won't necessarily match.

      KutyusK Offline
      KutyusK Offline
      Kutyus
      wrote on last edited by Kutyus
      #6

      @SGaist I found the solution, the (int)&str casted Qstring I get back the *((QString *)str) cast. The str type is QString.

      jsulmJ 1 Reply Last reply
      0
      • KutyusK Kutyus

        @SGaist I found the solution, the (int)&str casted Qstring I get back the *((QString *)str) cast. The str type is QString.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #7

        @Kutyus I would be careful as sizeof(void*) is not necessarily same as sizeof(int) :-)

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        KutyusK 1 Reply Last reply
        1
        • jsulmJ jsulm

          @Kutyus I would be careful as sizeof(void*) is not necessarily same as sizeof(int) :-)

          KutyusK Offline
          KutyusK Offline
          Kutyus
          wrote on last edited by
          #8

          @jsulm Sorry, I do not understand, QString is void pointer?

          jsulmJ 1 Reply Last reply
          0
          • KutyusK Kutyus

            @jsulm Sorry, I do not understand, QString is void pointer?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #9

            @Kutyus What I mean is: on some platforms a pointer (doesn't matter to what it points) can be bigger than an int. For example:

            • int can be 4 byte
            • and a pointer 8 byte (on a 64bit platform)

            You're trying to squeeze a pointer to a QString into an int. Whether this will work or not depends on platform.
            See https://en.cppreference.com/w/cpp/language/types
            64 bit systems:
            LLP64 or 4/4/8 (int and long are 32-bit, pointer is 64-bit)
            Win64 API
            LP64 or 4/8/8 (int is 32-bit, long and pointer are 64-bit)
            Unix and Unix-like systems (Linux, Mac OS X)

            So, Win64 API (Windows 64bit), MacOS and Linux 64bit this will NOT work.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            KutyusK 1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #10

              Can you explain what exactly you are trying to achieve using this int/pointer idea ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • jsulmJ jsulm

                @Kutyus What I mean is: on some platforms a pointer (doesn't matter to what it points) can be bigger than an int. For example:

                • int can be 4 byte
                • and a pointer 8 byte (on a 64bit platform)

                You're trying to squeeze a pointer to a QString into an int. Whether this will work or not depends on platform.
                See https://en.cppreference.com/w/cpp/language/types
                64 bit systems:
                LLP64 or 4/4/8 (int and long are 32-bit, pointer is 64-bit)
                Win64 API
                LP64 or 4/8/8 (int is 32-bit, long and pointer are 64-bit)
                Unix and Unix-like systems (Linux, Mac OS X)

                So, Win64 API (Windows 64bit), MacOS and Linux 64bit this will NOT work.

                KutyusK Offline
                KutyusK Offline
                Kutyus
                wrote on last edited by
                #11

                @jsulm Ahha, good perception, I modify the parameter type to long.

                jsulmJ 1 Reply Last reply
                0
                • KutyusK Kutyus

                  @jsulm Ahha, good perception, I modify the parameter type to long.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  @Kutyus If you can modify the type of the parameter why not changing it to QString? Or do you want to pass arbitrary types to it (in this case you should use void*)?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  KutyusK 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Kutyus If you can modify the type of the parameter why not changing it to QString? Or do you want to pass arbitrary types to it (in this case you should use void*)?

                    KutyusK Offline
                    KutyusK Offline
                    Kutyus
                    wrote on last edited by
                    #13

                    @jsulm All of the information is int type (or short) except the device name, it is string.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      Again: why do you do it like that ? You could pass the whole structure for example.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      KutyusK 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        Again: why do you do it like that ? You could pass the whole structure for example.

                        KutyusK Offline
                        KutyusK Offline
                        Kutyus
                        wrote on last edited by
                        #15

                        @SGaist Has given a message - message parameter - type communication, it is ready, it has to be adapted to this.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          You realise that you are setting a recipe for disaster here. If you are working in a multithreaded setup, you are going to try to access invalid memory because by the time your slot is called, the QString object you are pointing will likely already be destroyed.

                          You should rather fix the design rather than trying that kind of workaround.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          E 1 Reply Last reply
                          3
                          • SGaistS SGaist

                            You realise that you are setting a recipe for disaster here. If you are working in a multithreaded setup, you are going to try to access invalid memory because by the time your slot is called, the QString object you are pointing will likely already be destroyed.

                            You should rather fix the design rather than trying that kind of workaround.

                            E Offline
                            E Offline
                            etla
                            wrote on last edited by etla
                            #17

                            @SGaist Second that.

                            Casting data wildly is a good way of getting bugs that might be extremely hard to track down. I'd suggest that you instead store your private data in an external array and pass an index to the array, much safer than using a bigger hammer. Ie for example:

                            QString *data[n];

                            (With whatever size of n you'll need).

                            So you send the index to the right pointer in the call and avoid the whole mess of casting the pointer to an integer, which will be platform dependant.

                            Code in haste; debug in leisure.

                            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