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