如何销毁QProcess当它完成和类比contaed槽 [英] How to destroy QProcess when it finishes and the class than contaed the slot

查看:543
本文介绍了如何销毁QProcess当它完成和类比contaed槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类的成员函数中启动了一个 QProcess ,但是该对象(下面的Pdf对象)很快会在创建进程后被销毁。我显然希望槽函数可用,并在QProcess完成时调用。我通过 QProcess 指针,所以当它完成,它会毁了它,以及。但实际上它不会真正被销毁,当它完成。

  void PDf :: createPDF()
{
PdfSymlink * pdfSymlink = new PdfSymlink(fileName,linkName,myProcess);

connect(myProcess,SIGNAL(finished(int)),pdfSymlink,SLOT(createPdfLink(int)));

myProcess-> start(sh,args); // args现在定义为代码
}

这是一个重复的代码,调用许多次我想要QProcess在它完成时被销毁,同样pdfSymlink也应该销毁。



请注意,我的插槽确实会被调用,它会执行这项工作,但是我想确保我清理此对象后。

解决方案

好吧,你似乎跟踪过程对象。您可以像这样实现 createPdfLink(int)

  void createPdfLink int status)
{
if(status!= 0)
{
//错误处理
}
else
{
//正常代码
}

myProcess-> deleteLater();
deleteLater();
}

假定 myProcess PdfSymlink 对象中还存储为 myProcess 。它使得进程被Qt删除,一旦它允许被Qt删除(主要是为了防止随机崩溃),并导致类本身删除时允许。



额外:



作为更干净的代码,最好包含 connect

  PdfSymlink的构造函数中的<$ c> :: PdfSymlink(QString fileName,QString linkName,QProcess * myProcess)
{
this-> myProcess = myProcess;
connect(myProcess,SIGNAL(finished(int)),SLOT(createPdfLink(int)));

//更多初始化代码
}

增加了保持代码相关性的好处,并且使得receiver参数可选作为重载的非静态成员函数 connect(sender,signal,method) p>

I start a QProcess in member function of a class but that object (Pdf object below) will soon get destroyed after creating the process. I obviously want the slot function to be available and called when QProcess finishes. I pass QProcess pointer so when it finishes, it will destroy it as well. But actually it doesn't really is destroyed when it finishes.

void PDf::createPDF()
{
PdfSymlink * pdfSymlink = new PdfSymlink( fileName, linkName, myProcess );

connect(myProcess, SIGNAL(finished(int)), pdfSymlink, SLOT(createPdfLink(int)) );

myProcess->start("sh",args); // args is defined now shown in code
} 

This is a repetitive code which is called many many timesI want the QProcess to get destroyed when it finishes and likewise pdfSymlink should be destroyed as well. How can I do that?

Note my slot does get called and it does the job but I want to make sure I clean up after this object.

解决方案

Well, you seem to keep track of the process object. You could implement createPdfLink(int) like this:

void createPdfLink(int status)
{
    if(status != 0)
    {
       // Do error handling
    }
    else
    {
       // regular code
    }

    myProcess->deleteLater();
    deleteLater();
}

assuming myProcess is stored as myProcess inside the PdfSymlink object as well. It causes the process to be deleted by Qt as soon as it is allowed to be deleted by Qt (mainly to prevent random crashes) and causes the class itself to delete when allowed.

Extra:

As a more clean code, it might be better to include the connect function inside the constructor of the PdfSymlink class:

PdfSymlink::PdfSymlink(QString fileName, QString linkName, QProcess * myProcess)
{
    this->myProcess = myProcess;
    connect(myProcess, SIGNAL(finished(int)), SLOT(createPdfLink(int)));

    // More initialization code
}

This has the added benefit of keeping code relevant, and makes the "receiver" argument optional as the overloaded non-static member-function connect(sender,signal,method) is used.

这篇关于如何销毁QProcess当它完成和类比contaed槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆