1

I am trying to prevent user from entering the same data into my model, which is subclassed from QAbstractTableModel.

bool MyModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
    bool result = false;
    ...
    // Test if my model already has the same data
    result = findItem( value.toString() ) != -1;
    ...
    if ( result )
        emit( dataChanged( index, index );
    else
        emit ( dataInvalid( index ) );

    return result;
}

Now I should catch the signal and turn my table view (which type is QTableView) back to editing state:

void MyWindow::dataInvalid( const QModelIndex &index )
{
    myTableView->edit( index );
}

But when I run my application I got message in the console and QTableView does not turn to editing state:

edit: edit failed

What am I doing wrong? Thank you very much in advance.

pau
  • 352
  • 3
  • 14
  • I think it fails, because when you call edit() on your view, it is still in the edit mode and setData() function is not exited yet. – vahancho Nov 28 '13 at 14:41
  • Thank you very much, @vahancho! I added `Qt::QueuedConnection` when connecting to `dataInvalid` signal and it works! The problem was because `QTableView` was **still** in `QAbstractItemView::EditState` when I called `QTableView::edit( const QModelIndex &index )`. Thank you! – pau Nov 28 '13 at 15:10

1 Answers1

1

When calling

myTableView->edit( index )

my view is still in QAbstractItemView::EditState and that is the reason of failure. Solution is to add Qt::QueuedConnection when connecting to signal:

MyWindow::MyWindow()
{
    ...
    connect( myModel, SIGNAL( dataInvalid( QModelIndex ) ),
        this, SLOT( dataInvalid( QModelIndex ) ), Qt::QueuedConnection );
    ...
}

Now everything works fine.

pau
  • 352
  • 3
  • 14