我对 QT 相当陌生,并且无法理解如何处理 QTableView
选择更改信号。我已经设置了一个带有 openGL 小部件和 QTableView
的窗口。我有一个正确填充 tableview 的数据模型类,因此我向该类添加了一个公共(public)插槽:
class APartsTableModel : public QAbstractTableModel
{
public:
AVehicleModel *vehicle;
explicit APartsTableModel(QObject *parent = 0);
//MVC functions
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &paret) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
public slots:
void selectionChangedSlot(const QItemSelection &newSelection,
const QItemSelection &oldSelection);
};
当我准备好用表格 View 显示窗口时,我像这样分配/初始化它:
//create the display view
AStarModelView *displayWindow = new AStarModelView(this,
starModel->vehicle);
//create the datamodel for the table view
APartsTableModel *dataModel = new APartsTableModel(displayWindow);
dataModel->vehicle = starModel->vehicle;
//create selection model for table view
QItemSelectionModel *selModel = new QItemSelectionModel(dataModel);
displayWindow->materialsTable->setSelectionModel(selModel);
//setup model and signal
displayWindow->materialsTable->setModel(dataModel);
connect(selModel,
SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
dataModel,
SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));
//show the view
displayWindow->show();
当我在 slot 函数的实现中设置断点时,我从未命中它。我也试过不分配新的
QItemSelectionModel
,但这也不起作用。我真的不确定我在这里做错了什么。 最佳答案
当您在 View 上调用 setModel() 时,您本地分配的 QItemSelectionModel 将被 View 创建的 QItemSelectionModel 替换。无论如何,您不必创建自己的选择模型。只需将您的连接更改为
connect(displayWindow->materialsTable->selectionModel(),
SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
dataModel,
SLOT(selectionChangedSlot(const QItemSelection&, const QItemSelection&)));