Solved QString pointer to hand over and get back in signal
-
@jsulm Sorry, I do not understand, QString is void pointer?
-
@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.
-
Can you explain what exactly you are trying to achieve using this int/pointer idea ?
-
@jsulm Ahha, good perception, I modify the parameter type to long.
-
@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*)?
-
@jsulm All of the information is int type (or short) except the device name, it is string.
-
Again: why do you do it like that ? You could pass the whole structure for example.
-
@SGaist Has given a message - message parameter - type communication, it is ready, it has to be adapted to this.
-
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.
-
@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.