2016-08-01 24 views
1

我有一個快速的問題。我想根據點擊哪一列來攔截具有不同行爲的樹視圖上的點擊事件。我相信有一個信號可以通過模型指數......但是如何識別列?謝謝您的幫助。QT QTreeView點擊特定列上的事件

+1

我想我找到了答案.. index.column()正確嗎? –

回答

1

檢查QTreeView中使用的QItemSelectionModel以處理行o列中的選擇或單擊事件。使您的樹視圖可選,並使用其中一個默認信號。你有3個不同的信號處理click事件:

void currentChanged(const QModelIndex &current, const QModelIndex &previous) 
void currentColumnChanged(const QModelIndex &current, const QModelIndex &previous) 
void currentRowChanged(const QModelIndex &current, const QModelIndex &previous) 

手柄,定製插槽信號,並使用QModelIndex參數來獲取當前行和索引。例如:

void MainWindow::elementClicked(const QModelIndex& current, const QModelIndex& previous) { 
    const int row = current.row(); 
    const int column = current.column(); 
    qDebug() << "Clicked at " << row << column; 
} 
+0

謝謝。你是什​​麼意思: 檢查QItemSelectionModel中使用的QTreeView ? –

+0

我附加的信號是QItemSelectionModel類的信號。 QTreeView有一個默認的QItemSelectionModel,用於通知樹中選定/取消選擇的每個項目。因此請檢查:http://doc.qt.io/qt-5/qitemselectionmodel.html和http://doc.qt.io/qt-5.7/qtreeview-members.html – mohabouje