2012-04-01 6 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("+")); 
    } 
} 

これは別のslot - 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

信号をコンストラクタで1回だけスロットに接続する必要があります。

逆に、あなたがアイテムを変更evety時間

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

を呼び出します。したがって、connectと呼ばれるように、あなたのスロットは何度も呼び出されます。

+0

ああ、私はその行について完全に忘れてしまった。ほとんどすべてをコメントアウトしましたが、これがあります。 >。>そのような恥。大いに感謝する。 ;) –

関連する問題