Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Can i use reference in signal slots

Can i use reference in signal slots

Scheduled Pinned Locked Moved QML and Qt Quick
14 Posts 4 Posters 4.4k 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.
  • P Offline
    P Offline
    praneeth007
    wrote on last edited by
    #1

    @
    myfirst::myfirst(QObject *parent) :
    QObject(parent)
    {
    connect(this,SIGNAL(set(QString)),this,SLOT(seting(QString),Qt::QueuedConnection);
    connect(this,SIGNAL(set(QString)),this,SLOT(seting2(QString)),Qt::QueuedConnection);

    }

    void myfirst::seting(const QString &slot1)
    {
    qDebug()<<&slot1;
    }

    void myfirst::seting2( const QString &slot2)
    {
    qDebug()<<&slot2;
    }

    void myfirst::hjk(QString & j) //this is mu public method just emitting signal
    {
    emit set(j);
    }
    @
    If i am not use reference then both printing same addresses

    if i use reference then printing different addresses.

    what is the difference

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Is the address important to you? The actual string data will be the same in both slots.

      Is your signal marked as const, does it pass const QString reference?

      (Z(:^

      1 Reply Last reply
      0
      • P Offline
        P Offline
        praneeth007
        wrote on last edited by
        #3

        If I am not put const then those slots are not connecting.

        And second thing I am thinking about memory management. Please tell me .. If you know

        And thanks for replay....

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          I don't understand your answer, pleae rephrase.

          QString will manage the memory for you, it is implicitly shared (copy on write). You do not need to worry about this at all, it just works. QStrings should be passed by const references whenever possible (const QString &something), including signals, slots, functions.

          (Z(:^

          1 Reply Last reply
          0
          • P Offline
            P Offline
            praneeth007
            wrote on last edited by
            #5

            It is creating two temporary memory addresses .
            So it will causes the time complexity problem.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Well, you are using QueuedConnections, so don't be surprised.

              I still fail to see any problem here, possibly I am too dumb for this. Or maybe it's the weekend-brain ;-)

              (Z(:^

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

                Hi,

                If you use a queued connection, Qt will copy your signal arguments. You don't need to worry about memory management (unless your argument is a pointer).

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

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  praneeth007
                  wrote on last edited by
                  #8

                  So is it good or bad using references in signal /slot

                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    [quote author="praneeth007" date="1405928051"]So is it good or bad using references in signal /slot [/quote]

                    In case of most of non-QObject classes: it is good and recommended practice.

                    (Z(:^

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #10

                      Just use const refernces. Qt makes it work :-)

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        praneeth007
                        wrote on last edited by
                        #11

                        why const compulsory

                        1 Reply Last reply
                        0
                        • sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #12

                          [quote author="praneeth007" date="1406010712"]why const compulsory [/quote]

                          Without it, you would keep modifying the original string.

                          (Z(:^

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            praneeth007
                            wrote on last edited by
                            #13

                            I want to modify those

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

                              [quote author="praneeth007" date="1406016204"]I want to modify those[/quote]You can't do that through signals and slots.

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

                              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