2012-04-01 24 views
1

在我的應用程序中,我有列表視圖。選擇另一個項目,觸發事件:QtWebPage - loadFinished()多次調用

connect(listView->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(item_changed(const QModelIndex &, const QModelIndex &))); 

void MainWindow::item_changed(const QModelIndex & current, const QModelIndex & previous) 
{ 
    qDebug() << "\n" << "item_changed(), caller: " << sender()->objectName(); 
    if (current.isValid()) 
    { 
     /* 
      not so important code 
     */ 

     change_query(tokens.join("+")); 
    } 
} 

這調用另一個插槽 - change_query()。

void MainWindow::change_query(QString newquery) 
{ 
    qDebug() << "change_query(), caller: " << sender()->objectName(); 

    QUrl query (newquery); 

    frame->load(query); 

    connect(frame, SIGNAL(loadFinished(bool)), this, SLOT(loading_finished())); 
} 

最後,當頁面完全加載,它應該調用loading_finished()

void MainWindow::loading_finished() 
{ 
    qDebug() << "loading_finished(), caller: " << sender()->objectName(); 
} 

但是,令我驚訝的是,輸出是:

item_changed(), caller: "SelectionModel" 
change_query(), caller: "SelectionModel" 
loading_finished(), caller: "frame" 

item_changed(), caller: "SelectionModel" 
change_query(), caller: "SelectionModel" 
loading_finished(), caller: "frame" 
loading_finished(), caller: "frame" 

item_changed(), caller: "SelectionModel" 
change_query(), caller: "SelectionModel" 
loading_finished(), caller: "frame" 
loading_finished(), caller: "frame" 
loading_finished(), caller: "frame" 

item_changed(), caller: "SelectionModel" 
change_query(), caller: "SelectionModel" 
loading_finished(), caller: "frame" 
loading_finished(), caller: "frame" 
loading_finished(), caller: "frame" 
loading_finished(), caller: "frame" 

正如你所看到的,每當我改變選擇時,WebFrame的另一個實例(?)被創建並加載,或者每個循環加載頁面+1次。我花了2個小時查明問題所在,我什麼也沒看到。

回答

2

您應該只將信號連接到插槽一次,可能在構造函數中。

相反地,你叫

connect(frame, SIGNAL(loadFinished(bool)), this, SLOT(loading_finished())); 

evety一次更改項目。所以,你的插槽會被調用很多次,如你所說的connect

+0

哦,該死的,我完全忘記了那條線。幾乎所有的東西都註釋掉了,但是這個。 >>這樣的恥辱。萬分感謝。 ;) – 2012-04-01 17:32:24