我想單擊單元格或使用箭頭鍵導航到QTableView的新單元格時發出信號。當我使用QTableWidget時,我使用了currentCellChanged信號。但是,我在QTableView中看不到類似的信號 - 有沒有類似的東西?QTableView中類似於QTableWidget :: currentCellChanged的東西?
感謝,
大衛
我想單擊單元格或使用箭頭鍵導航到QTableView的新單元格時發出信號。當我使用QTableWidget時,我使用了currentCellChanged信號。但是,我在QTableView中看不到類似的信號 - 有沒有類似的東西?QTableView中類似於QTableWidget :: currentCellChanged的東西?
感謝,
大衛
既可以使用:
QAbstractItemView::selectionModel()
返回的QItemSelectionModel
實例的信號currentChanged(QModelIndex, QModelIndex)
,或QAbstractItemView::currentChanged(QModelIndex, QModelIndex)
。PS:當參數爲const引用(所以不const
獨自一人,或不能單獨&
),你可以省略在SIGNAL
和SLOT
宏都const
和&
:
connect(tableView->selectionModel(),
SIGNAL(currentChanged(QModelIndex, QModelIndex)),
...
你應該重新實現此method:
virtual void selectionChanged (const QItemSelection & selected, const QItemSelection & deselected)
所以要做到這一點,我必須使用自定義v IEW?有沒有辦法做到這一點與默認的QTableView? –
似乎沒有要任何東西儘量精簡的QTableWidget的的currentCellChanged,但QTableView中確實繼承了幾件事情從QAbstractItemView您可能可以一起使用,特別是點擊,輸入,&按下的信號用於鼠標輸入,keyPressEvent用於鍵盤輸入。我能看到的唯一真正的問題是那些依賴於你的人也使用了模型,因爲它都基於當前有效的模型索引。
你可以也使用事件過濾器並重新實現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;
}
我用第一種方法 - 像魅力一樣工作!謝謝! (this-> tableView-> selectionModel(),SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)),SLOT(SomethingChanged(const QModelIndex&,const QModelIndex)&))); –