3

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

user3733164
  • 531
  • 4
  • 17
  • 5
    you might want to ask on english – maxbachmann May 28 '19 at 08:28
  • oopss...sorry, I dindn't realize I wasn't in the spanish section. I will translate just now. Thx – user3733164 May 28 '19 at 08:31
  • 2
    @user3733164 The selectionModel only exists (that is, it is different from nullptr) after establishing a model to the QTableView, in your case in the constructor even the QTableView does not have a model. A possible solution is that you pass the model to the constructor and establish it before making the connection. – eyllanesc May 28 '19 at 08:46
  • @eyllanesc, reading your answer I feel so stupid....thank you ;-) and you are right, I'll pass the model into the constructor before – user3733164 May 28 '19 at 08:51
  • 2
    Since this question is on hold, I'll answer it in short in this comment: `QAbstractItemView::setModel()` is virtual, so you can override it in your `MiTabla` class (I suspect it's derived from `QTableView`). There you first call `QTableView::setModel(model)` and then establish your connection with `selectionModel()`. – Martin Hennings May 28 '19 at 08:51
  • @MartinHennings, yes, it's derived from ```QTableView``` and you are right in your answer. Now it's work fine – user3733164 May 28 '19 at 08:58
  • 2
    @user3733164 I recommend you use the [new style syntax](https://wiki.qt.io/New_Signal_Slot_Syntax) – eyllanesc May 28 '19 at 09:00

0 Answers0