所以,我終於發現這個奇怪的問題,我沒有找到答案。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