C++ Qt:重新启动线程

C++ Qt:重新启动线程,c++,qt,C++,Qt,我正在使用Qt开发GUI,我在使用线程时遇到问题。我创建了一个带有两个按钮stream和stopstream的小型GUi。我的问题是停止流后无法重新启动它: 以下是代码的一部分: MainThread::MainThread(QWidget *parent):QWidget(parent){ bstream = new QPushButton("&stream"); bstopstream = new QPushButton("STOP stream"); bc

我正在使用Qt开发GUI,我在使用线程时遇到问题。我创建了一个带有两个按钮stream和stopstream的小型GUi。我的问题是停止流后无法重新启动它: 以下是代码的一部分:

MainThread::MainThread(QWidget *parent):QWidget(parent){

    bstream = new QPushButton("&stream");
    bstopstream = new QPushButton("STOP stream");
    bcapture = new QPushButton("capture a frame");
    Allbox = new QVBoxLayout(this);
    Allbox->addWidget(bstream);
    Allbox->addWidget(bcapture);
    Allbox->addWidget(bstopstream);

    connect(bstream,SIGNAL(clicked()),this, SLOT(startingstream()));
    connect(bcapture,SIGNAL(clicked()),this, SLOT(captureAFrame()));
    connect(bstopstream,SIGNAL(clicked()),this, SLOT(stopstreaming()));

    setLayout(Allbox);

} 

void MainThread::stopstreaming(){
    cv::destroyAllWindows();

    stream.terminate();
    stream.wait();
    stream.~Streaming();
}

void MainThread::startingstream(){

    if(stream.isRunning()) return;
    stream.start();

}
这将调用
对象的析构函数。您不应该手动调用它,从形式上讲,对象在此之后就死了,并且在此之后它可能会表现得“有趣”

例如,假设对象如下所示:

void stream::play() {
    buff_->start();
}

void stream::~stream() {
    delete buff_;
}
然后,行
buff\uu->start()
可能会做一些古怪的事情,本质上会产生未定义的行为

或者,如果它是这样写的(尽管您永远不需要在析构函数中手动将某个值设置为零;如前所述,对象在销毁后被假定为死亡):


然后它可能什么也不做。

对于类似的程序,我只是重新创建了线程以重新启动它。在我的例子中,作为流的是一个智能指针。文档中也不鼓励终止,因为它会导致内存泄漏和其他不良行为,如变量的部分更新……我不明白你的意思,所以除了终止线程,我会发信号让它退出。要从GUI线程重新启动线程,我创建了一个新的thread对象并启动它。@Engine:我的帖子有什么帮助吗?问题是我必须在QThread中使用它,所以我必须在run()方法中实现流式处理,我不明白buff\是什么意思。
void stream::play() {
    buff_->start();
}

void stream::~stream() {
    delete buff_;
}
void stream::play() {
    if (buff_) buff_->start();
}

void stream::~stream() {
    delete buff_;
    buff_ = 0;
}