2011-11-02 184 views

回答

3

既可以使用:

  • QAbstractItemView::selectionModel()返回的QItemSelectionModel實例的信號currentChanged(QModelIndex, QModelIndex),或
  • 導出視圖並重新定義函數QAbstractItemView::currentChanged(QModelIndex, QModelIndex)

PS:當參數爲const引用(所以不const獨自一人,或不能單獨&),你可以省略在SIGNALSLOT宏都const&

connect(tableView->selectionModel(), 
     SIGNAL(currentChanged(QModelIndex, QModelIndex)), 
     ... 
+0

我用第一種方法 - 像魅力一樣工作!謝謝! (this-> tableView-> selectionModel(),SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)),SLOT(SomethingChanged(const QModelIndex&,const QModelIndex)&))); –

0

你應該重新實現此method

virtual void selectionChanged (const QItemSelection & selected, const QItemSelection & deselected) 
+0

所以要做到這一點,我必須使用自定義v IEW?有沒有辦法做到這一點與默認的QTableView? –

0

似乎沒有要任何東西儘量精簡的QTableWidget的的currentCellChanged,但QTableView中確實繼承了幾件事情從QAbstractItemView您可能可以一起使用,特別是點擊,輸入,&按下的信號用於鼠標輸入,keyPressEvent用於鍵盤輸入。我能看到的唯一真正的問題是那些依賴於你的人也使用了模型,因爲它都基於當前有效的模型索引。

0

你可以也使用事件過濾器並重新實現eventFilter來管理索引更改,如下所示:

MyWidget::MyWidget(QWidget* parent) : QWidget(parent), ui(new Ui::uiClass) 
{ 
    ui->setupUi(this); 
    ui->tableView->installEvenFilter(this); 
    connect(ui->tableView, SIGNAL(activated(QModelIndex)), this,SLOT(manageNewIndex(QModelIndex))); 

} 



bool MyWidget::eventFilter(QObject* watched, QEvent* event) 
{ 
    bool retVal = true; 
    if(watched == ui->tableView && event->type() == QEvent::KeyPress) 
    { 
     QModelIndex index = ui->tableView->currentIndex(); 
     int row = index.row(); 
     int col = index.column(); 

     if(((QKeyEvent*)event)->key() == Qt::Key_Down) 
     { 
      index = index.sibling(row+1,col); 
     }else if(((QKeyEvent*)event)->key() == Qt::Key_Up) 
     { 
      index = index.sibling(row-1,col); 
     } 
     if(index.isValid()) 
     { 
      ui->tableView->setCurrentIndex(index); 
      manageNewIndex(index); 
     } 
     retVal = true; 
    } 
    else 
     retVal = QWidget::eventFilter(watched,event); 

    return retVal; 
} 
相關問題