Добрый день!
Пытаюсь сделать модальное окно с бесконечным QProgressBar. Но при вызове bar->setValue() получаю exceprion. Может кто подсказать, что я делаю не так?
Header
#pragma once
#ifndef NONCLOSEWINDOW_H
#define NONCLOSEWINDOW_H
class UpdateThread: public QThread
{
Q_OBJECT
public:
UpdateThread(){
terminated = false;
}
void run(){
while(!terminated){
emit updateSignal();
Sleep(10);
}
}
void setTerminated(bool terminated){
this->terminated = terminated;
}
private:
bool terminated;
signals:
void updateSignal();
};
class NonCloseWindow : public QDialog
{
Q_OBJECT
public:
NonCloseWindow(QString text = "", bool closeAlowed = true);
~NonCloseWindow();
void setText(QString s){lbl->setText(s);}
protected:
virtual void reject();
virtual void closeEvent(QCloseEvent* e);
virtual void showEvent(QShowEvent* event);
private:
QLabel* lbl;
QProgressBar* bar;
bool closeAlowed;
int value;
UpdateThread* thread;
QMutex* mutex;
private slots:
void setValue();
};
#endif // NONCLOSEWINDOW_H
Source
#include "nonclosewindow.h"
NonCloseWindow::NonCloseWindow(QString text, bool pcloseAlowed)
: QDialog(),closeAlowed(pcloseAlowed)
{
value = 0;
mutex = new QMutex();
QVBoxLayout* vb = new QVBoxLayout();
lbl = new QLabel(text);
vb->addWidget(lbl);
bar = new QProgressBar(this);
bar->setMinimum(0);
bar->setMaximum(100);
lbl->setAlignment (Qt::AlignCenter);
vb->addWidget(bar);
setWindowFlags(Qt::FramelessWindowHint);
setWindowModality(Qt::ApplicationModal);
setFixedSize(400,100);
this->setLayout(vb);
}
void NonCloseWindow::setValue(){
if(bar->value() >= bar->maximum() ) bar->reset();
bar->setValue(bar->value()+1);
}
void NonCloseWindow::showEvent(QShowEvent* event){
value = 0;
connect(thread,SIGNAL(updateSignal()),this,SLOT(setValue()),Qt::DirectConnection);
thread->setTerminated(false);
thread->start();
}
NonCloseWindow::~NonCloseWindow()
{
delete lbl;
}
void NonCloseWindow::reject()
{
}
void NonCloseWindow::closeEvent(QCloseEvent* e)
{
disconnect(thread,SIGNAL(updateSignal()),this,SLOT(setValue()));
thread->setTerminated(true);
thread->quit();
}