QTableView::edit( const QModelIndex &index ) failed - qt

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.

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.

Related

QItemDelegate: painting an unwanted QCheckBox

I have an editable model, which inherits QAbstractTableModel. Also have a custom delegate to go with it. This is my first editable model, and I think I'm missing something. I'm pretty much following the examples found at Nokia. My model tells the delegate that the data is editable via flags(). When I do this, it draws a QSpinBox in the cell.
Underlying model is a simple std::map. The key is days, the value is a rate.
Generally, what is painted in any editable cell, is a QCheckBox, but is ghosted out, then the data. If I double-click on the value, I am shown the editor, which happens to be a custom widget based on QDoubleSpinbox.
Qt::ItemFlags my_model_t::flags( const QModelIndex& index ) const
{
if ( !index.isValid() ) {
return Qt::NoItemFlags;
}
if ( index.column() == col_rates ) {
return QAbstractItemModel::flags( index ) | Qt::ItemIsEditable;
}
return QAbstractItemModel::flags( index );
}
QVariant my_model_t::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() ) {
return QVariant();
}
if ( role == Qt::DisplayRole || Qt::EditRole ) {
if ( static_cast<int>( rates.size() ) <= index.row() ) {
return QVariant();
}
int day = vec[index.row()];
if ( index.column() == col_days ) {
return day;
} else if ( index.column() == col_rates ) {
std::map<int, double>::const_iterator it = rates.find( day );
if ( it != rates.end() ) {
return (*it).second;
}
}
}
return QVariant();
}
QWidget* my_delegate_t::createEditor( QWidget* parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& index ) const
{
gui_spinbox* editor = new gui_spinbox( parent );
if ( index.column() == col_rate ) {
const my_model_t* model = static_cast<my_model_t*>( index.model() );
}
return editor;
}
void my_delegate_t::setEditorData( QWidget* editor, const QModelIndex& index ) const
{
double value = index.model()->data( index, Qt::EditRole ).toDouble();
gui_spinbox* spin_box = static_cast<gui_spinbox*>( editor );
if ( spin_box ) {
spin_box->setValue( value );
}
}
void my_delegate_t::setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const
{
gui_spinbox* spin_box = static_cast<gui_spinbox*>( editor );
if ( spin_box ) {
double value = spin_box->value();
model->setData( index, value, Qt::EditRole );
}
}
void my_delegate_t::updateEditorGemoetry( QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& /*index*/ ) const
{
editor->setGeometry( option.rect );
}
I'm not pretty sure about your problem. Do you want to paint a QCheckBox?
In your createEditor method, you created an editor use QSpinbox but in the Title you said it was unwanted.
If you want to be shown an editor based on an QCheckBox when you double clicked an item you just create a checkBox in the createEditor method and use checkBox in setEditorData and setModelData.

Qt - QTreeView and custom model with checkbox columns

I wanted to have a tree view which shows the item name, the item description, and two related Boolean values in respective columns. I started by modifying the Editable Tree Mode example, so there's a TreeModel that keeps track of a group of TreeItems, each of which not only has a list of child TreeItems, but also a list of QVariants which stores a set of values that can later be displayed in columns in the QTreeView.
I managed to add two more columns for two Boolean values. I also searched through the net on how to add checkboxes for QTreeView and QAbstractItemModel. I managed to have the checkboxes on the two Boolean columns working okay, as well as the rest of the tree hierarchy. Yet all the items in each column renders a checkbox and a line of text now.
Here's the parts where I've modified from the example, mainly within TreeModel.
treemodel.cpp:
bool TreeModel::isBooleanColumn( const QModelIndex &index ) const
{
bool bRet = false;
if ( !index.isValid() )
{
}
else
{
bRet = ( index.column() == COLUMN_BOL1 ) || ( index.column() == COLUMN_ BOL2 );
}
return bRet;
}
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
if ( isBooleanColumn( index ) )
{
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
}
else
{
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
}
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::CheckStateRole )
return QVariant();
TreeItem *item = getItem(index);
if ( role == Qt::CheckStateRole && isBooleanColumn( index ) )
{
Qt::CheckState eChkState = ( item->data( index.column() ).toBool() ) ? Qt::Checked : Qt::Unchecked;
return eChkState;
}
return item->data(index.column());
}
bool TreeModel::setData(const QModelIndex &index, const QVariant &value,
int role)
{
if (role != Qt::EditRole && role != Qt::CheckStateRole )
return false;
TreeItem *item = getItem(index);
bool result;
if ( role == Qt::CheckStateRole && isBooleanColumn( index ) )
{
Qt::CheckState eChecked = static_cast< Qt::CheckState >( value.toInt() );
bool bNewValue = eChecked == Qt::Checked;
result = item->setData( index.column(), bNewValue );
}
else
{
result = item->setData(index.column(), value);
}
if (result)
emit dataChanged(index, index);
return result;
}
mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
…
QStringList headers;
headers << tr("Title") << tr("Description") << tr("Hide") << tr("Lock");
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel *model = new TreeModel(headers, file.readAll());
file.close();
…
}
The checkboxes under the non-Boolean columns don't respond to user input, and the text under the Boolean columns aren't editable. So functionality-wise there's nothing wrong, but it's still bothersome as far as UI goes.
I'm moving onto having QTreeWidget do the same thing. Meanwhile, I'm couldn't help but wonder if there's something else I'm missing here. I heard one solution is to have a custom delegate; is that the only option?
If there's anyone who can point out what else I need to do, or provide a similar example, I will greatly appreciate it.
I think the problem is in the Data method. You should return QVariant() When the role is CheckStateRole but the column is not boolean.
I had this problem. It occurred in TreeModel::parent() method due to passing child.column() value to createIndex() method. It should be 0 instead. So, instead of
createIndex(parentItem->childNumber(), child.column(), parentItem);
should be
createIndex(parentItem->childNumber(), 0, parentItem);
The reason this is happening is related to a "bug" in peoples implementation of the models data method.
In the example below, only column 2 should show a checkbox.
Problem code:
if role == Qt.CheckStateRole:
if index.column() == 2:
if item.checked:
return Qt.Checked
else:
return Qt.Unchecked
Correct code:
if role == Qt.CheckStateRole:
if index.column() == 2:
if item.checked:
return Qt.Checked
else:
return Qt.Unchecked
return None
In the problematic code, table cells that should not have a checkbox decorator were getting missed and processed by a catch all role handler farther down in the code.

Qtreeview, resizing a line when it's been selected

I'm using Qt 4.7.0, a Qtreeview with multiple columns.
What I want to do is "simple" : I want a line to increase its height, when it's selected.
Will delegates be enough to do this ?
I've been through some stuff with a QTableView :
m_pMyTableView->verticalHeader()->setResizeMode(QHeaderView::Interactive);
...
QSize AbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
It's working with this tableview, but I can't see how I'll do this on a QTreeview, since, for a start, it doesn't have any vertical headers...
Can someone enlight my path please ?
Along with setting uniformRowHeights off in in your QTreeView here is what I would try.
There are few ways to do this, I like to use Qt's signals/slots, as such we're going to change the height through a custom QAbstractItemModel on the QTreeView. This custom model will be connected to the signal selectionChanged from the QItemSelectionModel of your QTreeView. The example code/snippets work with single selection mode but you can easily alter it to handle multiple selected rows.
Step 1 - Create Custom Model with Selection slot
Create the custom model class that derives from QAbstractItemModel and make sure you create a slot such as:
Q_SLOTS:
void onSelectionChanged( const QItemSelection&, const QItemSelection& );
Inside your model class add the following snippets/methods.
void MyModelClass::onSelectionChanged( const QItemSelection& selected,
const QItemSelection& deselected )
{
if( !selected.empty() )
{
// Save the index within the class.
m_selectedIndex = selected.first();
Q_EMIT dataChanged( m_selectedIndex, m_selectedIndex );
}
}
QVariant MyModelClass::data( const QModelIndex& index, int role ) const
{
// Use the selected index received from the selection model.
if( m_selectedIndex.isValid() &&
index == m_selectedIndex &&
role == Qt::SizeHintRole )
{
// Return our custom size!
return QSize( 50, 50 );
}
...
}
Step 2 - Connect Selection Changes to Your Model
Inside the initialization of your QTreeView create your custom model and do the following:
MyTreeView::MyTreeView( QWidget* parent ) : QWidget( parent )
{
...
MyModelClass* model = new MyModelClass();
setModel( model );
setSelectionMode( QAbstractItemView::SingleSelection );
setSelectionBehavior( QAbstractItemView::SelectRows );
connect
(
selectionModel(),
SIGNAL( selectionChanged(const QItemSelection&, const QItemSelection&) ),
model,
SLOT( onSelectionChanged(const QItemSelection&, const QItemSelection&) )
);
}
I'm sure there are a few ways of doing this, i.e. handing the QItemSelectionModel directly to your QAbstractItemModel but again I prefer to use signals/slots and to save the selection in the model.
Hope this helps.

Determine if QTableView has an open editor

Is there any way to determine if a QTableView has an open editor in the current cell? I need to handle the following situation:
A user double-clicks a cell and edits the data, but leaves the cell in the "edit" state.
On another part of the UI, an action is taken that changes the selected row of the underlying model.
Back on my view, I want to determine if the newly selected row is the same as the open row. If not, I need to take an action. (Prompt the user? Commit automatically? Revert?)
I see how to get the current item, and can get the delegate on that item, but I don't see any isEditMode() property I was hoping to find.
Can someone point me in the right direction?
Just check whether the return value of
State QAbstractItemView::state () const
is
QTableView::EditingState
Connect to underlying model dataChanged signal
void QAbstractItemModel::dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight )
You can check if the cell where data has changed is the same than the currentIndex
QModelIndex QAbstractItemView::currentIndex () const
You cannot know if the current cell had an open editor straight, but can check if the view is in QAbstractItemView::EditingState
State QAbstractItemView::state () const
It should be enough to do what you want.
You can subclass QTableView in order to be able to access the state() function, which is unfortunately protected. However, I did not try that.
If you already have an QStyledItemDelegate subclass, you can use it to track whether an editor is currently open. However, you can't just use setEditorData/setModelData, because setModelData won't be called, when the user cancels editing. Instead, you can track the creation and destruction of the editor itself.
class MyItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
MyItemDelegate( QObject* parent = nullptr );
~MyItemDelegate();
QWidget* createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const;
void setEditorData( QWidget* editor, const QModelIndex& index ) const;
void setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const;
bool isEditorOpen() const { return *m_editorCount > 0; }
protected:
int* m_editorCount;
protected slots:
void onEditorDestroyed( QObject* obj );
};
Implementation:
MyItemDelegate::MyItemDelegate( QObject* parent ) :
QStyledItemDelegate( parent )
{
m_editorCount = new int;
*m_editorCount = 0;
}
MyItemDelegate::~MyItemDelegate()
{
delete m_editorCount;
}
QWidget* MyItemDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
// create an editor, can be changed as needed
QWidget* editor = QStyledItemDelegate::createEditor( parent, option, index );
connect( editor, SIGNAL(destroyed(QObject*)), SLOT(onEditorDestroyed(QObject*)));
printf( "editor %p created\n", (void*) editor );
(*m_editorCount)++;
return editor;
}
void MyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
...
}
void MyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
...
}
void MyItemDelegate::onEditorDestroyed( QObject* obj )
{
printf( "editor %p destroyed\n", (void*) obj );
(*m_editorCount)--;
}
On some occasions, e.g. when moving to the next item in the tree using the cursor keys, Qt will create the new editor first and then destroy the old one. Hence, m_editorCount must be an integer instead of a bool.
Unfortunately, createEditor() is a const function. Therefore, you cannot create an int-member. Instead, create a pointer to an int and use that.
Subclass your delegate so that it includes an accessor that tells you when it's editing:
void MyDelegate::setEditorData ( QWidget * editor, const QModelIndex & index ) const {
// _isEditing will have to be mutable because this method is const
_isEditing = true;
QStyledItemDelegate::setEditorData(editor, index);
}
void MyDelegate::setModelData ( QWidget * editor, QAbstractItemModel * model, const QModelIndex & index ) const {
QStyledItemDelegate::setModelData(editor, model, index);
_isEditing = false;
}
bool MyDelegate::isEditing() const { return _isEditing; }
Then you can just check the delegate to see what's going on. Alternatively and/or if you don't like the mutable, you can emit signals so you know what state the delegate is in.
If you know the index of the item being edited, you can call indexWidget() and attempt to cast it. If it's valid, you not only know you're editing, but you also have your editor widget handy.
EditWidget *editWidget = qobject_cast<EditWidget*>(tableView->indexWidget(tableView->currentIndex()));
if(editWidget)
{
//yep, ur editing bro
}
Here is an idea, its even helpful to get the edit/combo widget before the edit begins...
just emit a signal and consume it in the mainwindow... this is what I used one to get combo box in QTableWidget before editing...
first create a signal in ComoBoxItemDelegate...
signals:
void OnComboEdit(QComboBox* pCombo) const;
then emit the signal in the createEditor method...
QWidget* ComboBoxItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
// Create the combobox and populate it
QComboBox* cb = new QComboBox(parent);
emit OnComboEdit(cb);
return cb;
}
and in the MainWindow declare a function to receive the singal...
void MainWindow::OnComboEidt(QComboBox *pCB) const
{
qDebug() << "Combo Eidt Singal Received";
}
Then finally in the constructor of MainWindow connect it...
ComboBoxItemDelegate* cbid = new ComboBoxItemDelegate(ui->tableWidget);
connect(cbid, &ComboBoxItemDelegate::OnComboEdit, this, &MainWindow::OnComboEidt);
ui->tableWidget->setItemDelegateForColumn(0, cbid);

QTreeView memory consumption

I'm testing QTreeView functionality right now, and i was amazed by one thing. It seems that QTreeView memory consumption depends on items count O_O. This is highly unusual, since model-view containers of such type only keeps track for items being displayed, and rest of items are in the model. I have written a following code with a simple model that holds no data and just reports that it has 10 millions items. With MFC, Windows API or .NET tree / list with such model will take no memory, since it will display only 10-20 visible elements and will request model for more upon scrolling / expanding items. But with Qt, such simple model results in ~300Mb memory consumtion. Increasing number of items will increase memory consumption. Maybe anyone can hint me what i'm doing wrong? :)
#include <QtGui/QApplication>
#include <QTreeView>
#include <QAbstractItemModel>
class CModel : public QAbstractItemModel
{
public: QModelIndex index
(
int i_nRow,
int i_nCol,
const QModelIndex& i_oParent = QModelIndex()
) const
{
return createIndex( i_nRow, i_nCol, 0 );
}
public: QModelIndex parent
(
const QModelIndex& i_oInex
) const
{
return QModelIndex();
}
public: int rowCount
(
const QModelIndex& i_oParent = QModelIndex()
) const
{
return i_oParent.isValid() ? 0 : 1000 * 1000 * 10;
}
public: int columnCount
(
const QModelIndex& i_oParent = QModelIndex()
) const
{
return 1;
}
public: QVariant data
(
const QModelIndex& i_oIndex,
int i_nRole = Qt::DisplayRole
) const
{
return Qt::DisplayRole == i_nRole ? QVariant( "1" ) : QVariant();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeView oWnd;
CModel oModel;
oWnd.setUniformRowHeights( true );
oWnd.setModel( & oModel );
oWnd.show();
return a.exec();
}
If i replace QTreeView with QTableView in sample source, memory will not be consumed. So it seems that QListView and QTreeView are not intended to be used with very big amount of data and QTableView must be used instead.

Resources