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. Slot never called
Forum Updated to NodeBB v4.3 + New Features

Slot never called

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 6.1k Views 4 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.
  • Chris KawaC Chris Kawa

    The only difference is the main function that i'm not using.

    And that's the problem.
    Since you put the downloader in some method it goes out of scope and is destroyed, ie.

    void someFunction() {
       Downloader d;       //d is created here
       d.doDownload();
    }                      //d is destroyed here
    

    The difference is that in the example downloader is created in the main() and right after doDownload() is called the appplication enters an event loop: return a.exec();, which blocks until the download is finished, so the downloader does not exit the scope.

    To fix this either put Downloader instance in a scope that is wide enough or create it on the heap and delete after the download is finished.

    H Offline
    H Offline
    Helson
    wrote on last edited by
    #3

    @Chris-Kawa

    Great explanation!

    But if I want use within this function?
    Create it on the heap is the solution?
    How can I do this?

    Thanks.

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

      How can you create an instance on the heap? Just as with any other c++ class: with the new keyword.

          Downloader* d = new Downloader();
          d->doDownload();
      

      but that is leaking memory, so you need to make sure the downloader is deleted when it finishes. You could, for example, emit a signal when it's done:

      class Downloader : public QObject
      {
         ...
      signals:
         void finished() const;
         ...
      

      Emit it in the replyFinished():

      void Downloader::replyFinished (QNetworkReply *reply)
      {
         ...
          reply->deleteLater();
          emit finished(); 
      }
      

      and then you can use it like this:

      void someFunction()
      {
          Downloader* d = new Downloader();
          d->doDownload();
          connect(d, &Downloader::finished, d, &Downloader::deleteLater);
      }
      
      H 1 Reply Last reply
      2
      • Chris KawaC Chris Kawa

        How can you create an instance on the heap? Just as with any other c++ class: with the new keyword.

            Downloader* d = new Downloader();
            d->doDownload();
        

        but that is leaking memory, so you need to make sure the downloader is deleted when it finishes. You could, for example, emit a signal when it's done:

        class Downloader : public QObject
        {
           ...
        signals:
           void finished() const;
           ...
        

        Emit it in the replyFinished():

        void Downloader::replyFinished (QNetworkReply *reply)
        {
           ...
            reply->deleteLater();
            emit finished(); 
        }
        

        and then you can use it like this:

        void someFunction()
        {
            Downloader* d = new Downloader();
            d->doDownload();
            connect(d, &Downloader::finished, d, &Downloader::deleteLater);
        }
        
        H Offline
        H Offline
        Helson
        wrote on last edited by
        #5

        @Chris-Kawa
        Yes! Now it is being called!

        Now i'm getting a compile error:

        C2248: 'Downloader::finished' : cannot access protected member declared in class 'Downloader'

        signals:
        void finished() const;

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #6

          Hi,
          please post the full source code.

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

            @Helson said:

            C2248: 'Downloader::finished' : cannot access protected member declared in class 'Downloader'

            Looks like you misplaced something. finished() should be a public method (a signal). Posting your code is a good idea as @Wieland suggested.

            kshegunovK 1 Reply Last reply
            0
            • H Offline
              H Offline
              Helson
              wrote on last edited by Helson
              #8

              Here is the code:

              http://pastebin.com/H9dVD3QZ

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #9

                Hi
                note:
                its Qt4

                1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  @Helson said:

                  C2248: 'Downloader::finished' : cannot access protected member declared in class 'Downloader'

                  Looks like you misplaced something. finished() should be a public method (a signal). Posting your code is a good idea as @Wieland suggested.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #10

                  @Chris-Kawa

                  Looks like you misplaced something. finished() should be a public method (a signal).

                  Chris, in Qt4 signals used to be protected, so it might be simply that he's using a bit older version than we assumed. ;)

                  Read and abide by the Qt Code of Conduct

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

                    awww, ok. Good catch guys.

                    @Helson so just replace

                    connect(d, &Downloader::finished, d, &Downloader::deleteLater);
                    

                    with

                    connect(d, SIGNAL(finished()), d, SLOT(deleteLater()));
                    

                    and think about upgrading to Qt5. Especially since you're following tutorials there's no point in learning obsolete stuff.

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @Chris-Kawa said:
                      connect(d, SIGNAL(finished()), d, SLOT(deleteLater()));

                      we did. still said it was protected ?!
                      so we moved to ctor in Download class

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

                        @mrjj

                        so we moved to ctor in Download class

                        Don't do that. It makes your class dependent on being created on the heap, which you can't guarantee. If someone creates it on the stack you will crash on double delete.

                        mrjjM 1 Reply Last reply
                        1
                        • Chris KawaC Chris Kawa

                          @mrjj

                          so we moved to ctor in Download class

                          Don't do that. It makes your class dependent on being created on the heap, which you can't guarantee. If someone creates it on the stack you will crash on double delete.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #14

                          @Chris-Kawa
                          Ah yes I see. Luckily we did not do it, ;)
                          we moved to dodownload member function and
                          used old syntax.

                          Im not used to Qt4 so the whole protected signals were
                          confusing.

                          kshegunovK 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @Chris-Kawa
                            Ah yes I see. Luckily we did not do it, ;)
                            we moved to dodownload member function and
                            used old syntax.

                            Im not used to Qt4 so the whole protected signals were
                            confusing.

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #15

                            Im not used to Qt4 so the whole protected signals were
                            confusing.

                            If you think about it, semantically it's more correct that signals are protected. A signal is raised from the object itself to notify others and in principle shouldn't be available for others to emit it! My suspicion is that they're now public because of the new way of connecting, which by my opinion is not such an improvement over the old one.

                            PS.
                            Is it me, or the forum is going nuts? I have the distinct feeling that I see Helson's posts as mrjj's?

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            2
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              hi, I talked with him in chat also, so I did post here.
                              Forums crash every 2 minutes for me though.

                              kshegunovK 1 Reply Last reply
                              1
                              • mrjjM mrjj

                                hi, I talked with him in chat also, so I did post here.
                                Forums crash every 2 minutes for me though.

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by
                                #17

                                @mrjj
                                Yes, but who are you? Are you indeed mrjj, or there's some bug in the forum and you're in fact Helson? :)

                                Read and abide by the Qt Code of Conduct

                                mrjjM 1 Reply Last reply
                                0
                                • kshegunovK kshegunov

                                  @mrjj
                                  Yes, but who are you? Are you indeed mrjj, or there's some bug in the forum and you're in fact Helson? :)

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by mrjj
                                  #18

                                  @kshegunov
                                  Heh. Im mrjj
                                  (i hope)

                                  kshegunovK 1 Reply Last reply
                                  1
                                  • mrjjM mrjj

                                    @kshegunov
                                    Heh. Im mrjj
                                    (i hope)

                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on last edited by
                                    #19

                                    @mrjj
                                    Ok, because I would have expected this:

                                    Ah yes I see. Luckily we did not do it, ;)
                                    we moved to dodownload member function and
                                    used old syntax.

                                    to be written by Helson. So it's my mistake, sorry for the misunderstanding. :)

                                    Read and abide by the Qt Code of Conduct

                                    mrjjM 1 Reply Last reply
                                    0
                                    • kshegunovK kshegunov

                                      @mrjj
                                      Ok, because I would have expected this:

                                      Ah yes I see. Luckily we did not do it, ;)
                                      we moved to dodownload member function and
                                      used old syntax.

                                      to be written by Helson. So it's my mistake, sorry for the misunderstanding. :)

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #20

                                      @kshegunov
                                      well You should have the credits as you solved it :)
                                      and sorry for writing a bit unclear :)

                                      kshegunovK 1 Reply Last reply
                                      1
                                      • mrjjM mrjj

                                        @kshegunov
                                        well You should have the credits as you solved it :)
                                        and sorry for writing a bit unclear :)

                                        kshegunovK Offline
                                        kshegunovK Offline
                                        kshegunov
                                        Moderators
                                        wrote on last edited by
                                        #21

                                        @mrjj
                                        Thanks for the credits, although I'm not sure how much I care about them, still I appreciate the sentiment. :)

                                        Read and abide by the Qt Code of Conduct

                                        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