QThread:线程仍在运行时被销毁,QMutex被销毁
QThread: Destroyed while thread is still running and QMutex destroyed
我正试图将多个线程添加到我的Qt应用程序中,但就在它执行这个线程时,程序崩溃了,我得到了的错误
QThread:线程仍在运行时被销毁
QMutex:销毁锁定的互斥
我理解错误消息,只是不知道如何修复它。我的代码如下。
标题
class Worker : public QObject
{
Q_OBJECT
private slots:
void onTimeout()
{
qDebug()<<"Worker::onTimeout get called from?: "<<QThread::currentThreadId();
}
};
class Thread : public QThread
{
Q_OBJECT
private:
void run()
{
qDebug()<<"From work thread: "<<currentThreadId();
QTimer timer;
Worker worker;
connect(&timer, SIGNAL(timeout()), &worker, SLOT(onTimeout()));
timer.start(1000);
exec();
}
};
登录.cpp
void Login::on_pushButton_clicked()
{
QSqlQuery query;
QString Username = ui->Username_lineEdit->text();
QString Password = ui->Password_lineEdit->text();
query.prepare("SELECT * FROM Program_account WHERE Login = '"+ Username +"' AND Password = '"+ Password +"'");
if(!query.exec())
{
qDebug() << "SQL QUERY Login:" << query.executedQuery();
qDebug() << "SQL ERROR Login:" << query.lastError();
}
else if(!query.first())
{
tries++;
int x = 10 - tries;
ui->label->setText("Incorrect Username or Password " + QString::number(x) + " tries until timeout");
}
else
{
QSqlQuery Account_Type_Query("SELECT Account_Type FROM Program_account WHERE Login = '"+ Username +"' AND Password = '"+ Password +"'");
while(Account_Type_Query.next())
{
Account_Type = Account_Type_Query.value(0).toInt();
}
tries = 0;
static Home *home = new Home;
home->show();
close();
}
if(tries == 10)
{
Thread t;
t.start();
ui->label->setText("Password entered wrong too many times, entered 10 minute cooldown period");
ui->pushButton->hide();
QTimer::singleShot(600000, ui->pushButton, SLOT(show()));
tries = 0;
ui->label->setText("");
}
我能解决这个问题的正确方法是什么。我们非常感谢所有的帮助。
谢谢
更新:尝试类线程:公共QThread{Q_OBJECT
private:
void run()
{
while(QThread::wait())
{
qDebug()<<"From work thread: "<<currentThreadId();
QTimer timer;
Worker worker;
connect(&timer, SIGNAL(timeout()), &worker, SLOT(onTimeout()));
timer.start(1000);
exec();
}
QThread::quit();
}
};
但是仍然接收到相同的错误
从QThread继承是这里的一个问题,我怀疑Worker对象没有您认为的线程相关性,并且正在主线程上运行。
崩溃的原因是您在堆栈上创建了线程实例
if(tries == 10)
{
Thread t; // NOTE THIS IS ON THE STACK
t.start();
ui->label->setText("Password entered wrong too many times, entered 10 minute cooldown period");
ui->pushButton->hide();
QTimer::singleShot(600000, ui->pushButton, SLOT(show()));
tries = 0;
ui->label->setText("");
}
线程实例超出范围时将被销毁。
无论如何,我强烈建议遵循@Thomas的建议,坚持Maya使用线程的方式,而不需要继承QThread。
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 我在c++代码中生成了一个运行时#3异常
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 建议在运行时将带有类实例的列表从c++导入qml
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 如何在GTK程序运行时禁用屏幕保护程序/电源管理/屏幕消隐
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 读取文件时运行时的未知行为
- 函数在Windows或Linux上运行时表现不同
- 在声明中合并两个常量"std::set"(不是在运行时)
- AWS Lambda C++运行时权限被拒绝
- 通过switch和static_cast访问多态对象的运行时类型
- QThread:线程仍在运行时被销毁,QMutex被销毁