I have a QStringListModel which works ok, but then I need to delete all the rows from it from QML.
I would expect removeRowsfunction from Qt documentation work like that, but I don't know how to use it property.
removeRows(int row, int count, const QModelIndex &parent = QModelIndex())
I tried to use it like this:
myModel.removeRows(1, 1)
But I am getting thie error:
qrc:/Logger.qml:63: TypeError: Property 'removeRows' of object QStringListModel(0x337350) is not a function
Can someone explain how to use removeRows correctly? Thanks.
removeRows() is not invokable from QML. the solution is to make it invokable by creating a new class and overriding that method:
class StringListModel: public QStringListModel{
Q_OBJECT
public:
Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()){
return QStringListModel::removeRows(row, count, parent);
}
};
In the following link there is an example.
I'm just getting started with QT, so please exercise a little patience...
I've an editable QTableWidget (actually a subclassing), and need to implement the following behavior.
When the user types a non acceptable value I would like:
1) to restore the original value;
2) to keep the focus in the cell and set it in edit mode.
I'm currently using the itemChanged SIGNAL, and a subclassing of QTableWidgetItem.
Which one is the best way to get what I need?
Any tip, suggestion or reference is really welcome.
If you think it as useful I can post some code.
Ciao
Alf.
I'm currently using the itemChanged SIGNAL...
You should subclass QStyledItemDelegate
class CustomTableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
CustomTableDelegate (QObject * parent = 0);
QWidget * createEditor (QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const;
bool editorEvent (QEvent *, QAbstractItemModel *, const QStyleOptionViewItem &, const QModelIndex &);
void setEditorData (QWidget *, const QModelIndex &) const;
void setModelData (QWidget *, QAbstractItemModel *, const QModelIndex &) const;
};
And implement validation inside setModelData.
To use custom delegate you need set it for your QTableWidget:
table->setItemDelegate (new CustomTableDelegate () );
Hi I can't figure out why my data() function is never called when populating QTableView
I subclassed QSqlQueryModel. The header is like:
class TicketModel : public QSqlQueryModel
{
Q_OBJECT
public:
explicit TicketModel(QObject *parent = 0);
QVariant data(const QModelIndex &index, int role);
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
};
In the main window I set my model to the table
TicketModel *model = new TicketModel();
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(model);
QSqlQuery *query = _tf->search(1);
model->setQuery(*query);
_ui->dashTable->setModel(proxyModel); // <<<<<<<<<<<<<< I setting model here too, didn't work
_ui->dashTable->setSortingEnabled(true);
_ui->dashTable->horizontalHeader()->setVisible(true);
_ui->dashTable->setSelectionBehavior(QAbstractItemView::SelectRows);
The TicketModel::headerData(...) is called but TicketModel::data(...) is never called when the table is created. Why? How can I get it to be called?
I hope I just overlooked something simple but I have been trying for a few hours to figure it out.
Thanks for the help!
You've got the signature wrong. You need a const.
QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const
I have a QTableView and a corresponding instance of a child of QAbtractTableModel.
I was surprised to see that if my table model instance emits a dataChanged naming a single cell, the Qt framework will then issue a large number of calls to my table model's ::data() member function. The row/column range of those calls appears to cover the entire range of what is on the screen + some extra.
This is more than I expected. I would have thought that a dataChanged() that names a single cell would only result in ::data() calls requesting data for that cell. After all, that's the only cell that my table model said was changed. But the Qt framework appears to be very gregarious and inquires about all the cells.
I clearly have a broken understanding of the intent of the dataChanged() signal.
Is there a way to tell the QTableView to update one cell and one cell only without all the extra chatter sent to my table model?
UPDATE: Including code sample
The example here is a header, source, and a chunk of code to create the table. For me, the table displays with 12 columns and 29 rows. After the "issueEmit" invocation at the end, ::data will be invoked 1044 times all because of a dataChanged() signal for a single cell.
// Declaration
#include <QAbstractTableModel>
class SimpleModel : public QAbstractTableModel
{
Q_OBJECT
private:
bool _post_emit;
public:
explicit SimpleModel(QObject *parent=0);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void issueEmit();
};
// Implementation
#include <stdlib.h>
#include <stdio.h>
#include "simplemodel.h"
SimpleModel::SimpleModel(QObject *parent) : QAbstractTableModel(parent), _post_emit(false) { }
int SimpleModel::rowCount(const QModelIndex &parent) const {
return 100;
}
int SimpleModel::columnCount(const QModelIndex &parent) const {
return 100;
}
QVariant SimpleModel::data(const QModelIndex &index, int role) const {
if (role==Qt::DisplayRole) {
if (_post_emit) {
static unsigned s_calls=0;
s_calls++;
printf("Data calls: %d\n",s_calls);
}
return ((rand()%10000)/1000.00);
}
return QVariant();
}
void SimpleModel::issueEmit() {
_post_emit=true;
emit dataChanged(createIndex(1,1),createIndex(1,1));
}
// Usage
QTableView *table=new QTableView;
table->setMinimumSize(1200,900);
SimpleModel *model=new SimpleModel;
table->setModel(model);
table->show();
model->issueEmit();
QVariant QStandardItem::data ( int role = Qt::UserRole + 1 ) const [virtual]
Returns the item's data for the given role, or an invalid QVariant if there is no data for the role.
The argument is really the interesting thing here. Each item in a model holds a number of QVariants, these QVariants maintain different information about the item.
These Variants are all assigned roles. Any time you emit that data has changed the model must redraw the item. To redraw the item it must look at many different pieces of data (small excerpt included below)
Roles describing appearance and meta data (with associated types):
Constant Value Description
Qt::FontRole 6 The font used for items rendered with the default delegate. (QFont)
Qt::TextAlignmentRole 7 The alignment of the text for items rendered with the default delegate. (Qt::AlignmentFlag)
Qt::BackgroundRole 8 The background brush used for items rendered with the default delegate. (QBrush)
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);