所以,我终于发现这个奇怪的问题,我没有找到答案。qtimer在创建构造函数或静态时崩溃
我创建了一个小GUI,在一个单独的窗口中启动应用程序,然后使用qtimer轮询此应用程序的状态。
process_timer = new QTimer(this);
connect(process_timer, SIGNAL(timeout()), this, SLOT(checkFlashProcess()));
process_timer->start(100);
所以这个工作。但我宁可不创建一个新的计时器每一次,所以我放置在GUI的构造process_timer的创建:
Flasher::Flasher(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Flasher)
{
ui->setupUi(this);
process_timer = new QTimer(this);
}
现在,这导致崩溃和输出: 的QObject ::连接:不能连接(空)::超时()到闪蒸器:: checkFlashProcess()
同样为这样:
Flasher::Flasher(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Flasher),
process_timer(new QTimer)
{...
QTimer * process_timer在应用报头定义。
我也试图定义process_timer非动态:
header.h:
QTimer process_timer;
code.cpp
void Flasher::on_flashButton_clicked()
{
(...)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process_pid);
if(hProcess)
{
qDebug() << "Got handle for process!";
connect(&process_timer, SIGNAL(timeout()), this, SLOT(checkFlashProcess()));
process_timer.start(30);
}
这也导致崩溃。
回调:
void Flasher::checkFlashProcess()
{
qDebug() << "Got handle for process!";
}
但是这是为什么呢?我猜计时器不会在构造函数中创建,但在构造函数中创建对象应该不是问题吧?而且为什么静态版本也会崩溃,这会是同一个问题吗?
'connect'调用在哪里,什么时候调用相对于'Flasher'对象实例的构造? – alexisdm
连接在gui上的按钮的槽中调用。 'void Flasher :: on_flashButton_clicked() { ... hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,process_pid); if(hProcess) qDebug()<<“获得进程句柄!”; process_timer = new QTimer(this); connect(process_timer,SIGNAL(timeout()),this,SLOT(checkFlashProcess())); process_timer-> start(30); }' –
你是否100%肯定'process_timer'在创建和调用'connect()'之间的某处没有设置为null?您是否在调试器中停止了connect()调用并调查了它? – Googie