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. Connecting a slot to a QSharedPointer (QQuickItem::grabToImage)

Connecting a slot to a QSharedPointer (QQuickItem::grabToImage)

Scheduled Pinned Locked Moved General and Desktop
qsharedpointerqquickitemqquickitemgrabrsignal & slot
4 Posts 2 Posters 4.0k 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.
  • B Offline
    B Offline
    bundickt
    wrote on 5 Aug 2015, 17:04 last edited by bundickt 8 Jun 2015, 16:39
    #1

    What I'm doing:
    I am trying to use the QQuickItem::grabToImage (http://doc.qt.io/qt-5/qquickitem.html#grabToImage) method to convert a Qt Quick Item to an image and in the documentation it says the QQuickItemGrabResult will emit the ready signal when grab has been completed. The issue I'm having is the QQuickItem::grabToImage returns a QSharedPointer and I can't find a "proper" way to connect a slot to a signal of a QSharedPointer. I know you can use QSharedPointer.data() to get the raw pointer, but why even return a QSharedPointer?

    What I need to know:
    What is the "proper" way to connect a slot to a signal of a QSharedPointer?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 5 Aug 2015, 20:53 last edited by
      #2

      Hi,

      You connect the object that is contained with the QSharedPointer not the QSharedPointer itself

      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
      1
      • B Offline
        B Offline
        bundickt
        wrote on 6 Aug 2015, 16:39 last edited by
        #3

        Thanks for the response.

        If you're suppose to use the object that is contained then why even have it be a QSharedPointer in the first place. I see a couple flaws with the design:

        1. QQuickItem::grabToImage returns a QSharedPointer<QQuickItemGrabResult>, but in order to know when the QQuickItemGrabResult is ready you need to listen for a signal and QSharedPointers doesn't support signals.
        2. Connecting for a ready signal after making the request may result in never receiving the ready signal. This will happen if the request is really quick. I have tried using this design patter for creating asynchronous sql queries and I did miss the signal sometimes.

        Proposed Change:
        Have the class making the request create a QQuickItemGrabResult pointer itself, set all the dependencies, connect to all of its signals and then start the request. The requester class should also be in charge of managing the memory of the pointer it created.

        QQuickItemGragResult *result = new QQuickItemGragResult(item); //item is the QQuickItem to create an image out of
        connect(result, SIGNAL(ready()), this, SLOT(onReady()));
        result->start();

        ...

        void onReady() {
        QObject *sender = sender();
        ...
        sender->deleteLater();
        }

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 6 Aug 2015, 20:49 last edited by
          #4

          It's just a matter of calling:

          connect(grabResult.data(), &QQuickItemGrabResult::ready, this, &MyClass::mySlot);

          I haven't checked the current implementation but in any case it's a costly operation so using a shared object might also be a non-negligeable resource usage improvement.

          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
          0

          4/4

          6 Aug 2015, 20:49

          • Login

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