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. [Solved] objects in QMainWindow derived class destroyed

[Solved] objects in QMainWindow derived class destroyed

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 1.5k 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.
  • A Offline
    A Offline
    ashishbansal
    wrote on 27 Dec 2014, 16:31 last edited by
    #1

    Hi everyone,
    I am new to Qt and want to know, why all the objects created in the derived class of QMainWindow gets destroyed after running all the lines of the constructor??

    For example,
    I have class mainwindow and download:

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    Download dl;
    }@

    Object created dl automatically gets destroyed even though whole program keeps running.

    Thanks,
    Ashish Bansal

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 27 Dec 2014, 17:58 last edited by
      #2

      Hi, welcome to devnet.

      dl is a local variable. It gets destroyed at the end of a function just like "int foo" would be.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ashishbansal
        wrote on 27 Dec 2014, 18:26 last edited by
        #3

        [quote author="Chris Kawa" date="1419703138"]Hi, welcome to devnet.

        dl is a local variable. It gets destroyed at the end of a function just like "int foo" would be.[/quote]

        Okay so what if I want to keep it until I close the program and want to instantiate the class whenever user triggers the QAction ? Would I have to create my all objects in main.cpp or create them on heap and clean up manually ?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 27 Dec 2014, 18:50 last edited by
          #4

          Let me check if I understand it correctly: you want to instantiate a class once, when an action is triggered, and keep that instance until the app exits.
          What should happen if the action is triggered a second time? Should the trigger be ignored or a new instance created? If another instance should be created what should happen with the previous one? Destroyed or also kept till the app finishes?

          There are different ways to proceed depending on the above requirements.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ashishbansal
            wrote on 27 Dec 2014, 18:58 last edited by
            #5

            [quote author="Chris Kawa" date="1419706223"]Let me check if I understand it correctly: you want to instantiate a class once, when an action is triggered, and keep that instance until the app exits.
            What should happen if the action is triggered a second time? Should the trigger be ignored or a new instance created? If another instance should be created what should happen with the previous one? Destroyed or also kept till the app finishes?

            There are different ways to proceed depending on the above requirements.[/quote]

            Hey first of all thanks for quick reply!
            Let me explain what i wanna do. I am trying to create a download manager. I would pass the URL from which it would download the respective file. So, any no.s of instances can be created and all have to be kept until there respective tasks gets completed(i.e. download successfully completed).

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 27 Dec 2014, 19:12 last edited by
              #6

              That's a different story. If that's the case then one way is to just create a dynamic instance(hide it behind static or something) and in the downloader class call deleteLater(this) when you're done downloading (might want to emit some finished signal first of course).

              So something along these lines:
              @
              void MyClass::onSomeActionTriggered() {
              auto url = ...
              connect(Download::start(url), &Download::finished, [](SomeData data){
              //do something with the data
              }
              }

              Download* Download::start(url) { //this is a static method
              auto foo = new Download();
              foo->setUrl(url);
              foo->startDownload(); //assume this calls done() when it's done
              return foo;
              }

              void Download::done() {
              emit finished(theData);
              deleteLater(this);
              }
              @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ashishbansal
                wrote on 27 Dec 2014, 19:53 last edited by
                #7

                [quote author="Chris Kawa" date="1419707566"]That's a different story. If that's the case then one way is to just create a dynamic instance(hide it behind static or something) and in the downloader class call deleteLater(this) when you're done downloading (might want to emit some finished signal first of course).

                So something along these lines:
                @
                void MyClass::onSomeActionTriggered() {
                auto url = ...
                connect(Download::start(url), &Download::finished, [](SomeData data){
                //do something with the data
                }
                }

                Download* Download::start(url) { //this is a static method
                auto foo = new Download();
                foo->setUrl(url);
                foo->startDownload(); //assume this calls done() when it's done
                return foo;
                }

                void Download::done() {
                emit finished(theData);
                deleteLater(this);
                }
                @[/quote]

                Thanks, I got it!

                [Edit: new topic moved "here":http://qt-project.org/forums/viewthread/51471/ @Chris Kawa]

                1 Reply Last reply
                0

                4/7

                27 Dec 2014, 18:50

                • Login

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