I'm trying to give a specific behaviour to a QtableView . It would be that if I select a cell and I drag it with the mouse, only the cells in the same column will be selected.
For this purpose, and searching similar questions, I've tried it with a lambda function.
It works fine out of the constructor of the table (custom table), like that:
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
MiTabla *tabla = new MiTabla;
tabla->setModel(new MiModelo());
tabla->setItemDelegate(new MiDelegado());
selectionModel = tabla->selectionModel();
setCentralWidget(tabla);
QObject::connect(selectionModel,&QItemSelectionModel::selectionChanged,[=]()
{
foreach (const QModelIndex& item, selectionModel->selectedIndexes())
{
if (item.column()!=tabla->indiceActual().column())
{
selectionModel->select(item,QItemSelectionModel::Deselect);
}
}
});
}
But if I make the connection inside the constructor of the table, it throw me this error :
QObject::connect: invalid null parameter
MiTabla::MiTabla()
{
miSelectionModel = selectionModel();
QObject::connect(this,SIGNAL(pressed(QModelIndex)),this,SLOT(CambiarIndiceACtual(QModelIndex)));
QObject::connect(miSelectionModel,&QItemSelectionModel::selectionChanged,[=]()
{
foreach (const QModelIndex& item, miSelectionModel->selectedIndexes())
{
if (item.column()!=m_indiceActual.column())
{
miSelectionModel->select(item,QItemSelectionModel::Deselect);
}
}
});
}
My question is why doesn't work the connection inside the constructor of the table