Edit data from a custom QAbstractTableModel through a modal dialog
-
I have a subclass of QDialog that I'd like to use as the table editor. I subclassed QAbstractTableModel so I can manipulate a QHash <int, triplet>. triplet is a custom class.
I tried subclassing QStyledItemDelegate as well, but I couldn't get it to actually set data. so I tried ading a slot to the table model that creates the dialog, gets the data and calls setData, with the slot being activated by a double click signal. I got the dialog to show, but the data wasn't being set.
I'm now trying to have setData call that function and now I can't even get the editor to show! please, advise. here's the editor header
@
#ifndef TABLE_EDITOR_H
#define TABLE_EDITOR_H
#include <QComboBox>
#include <QDialog>
#include <QPushButton>
#include <QGroupBox>
#include "triplet.h"
#include "table_model.h"class table_editor: public QDialog
{
Q_OBJECTpublic: table_editor(); QMap<QString, signed int> shifts; QComboBox* n_state; QComboBox* n_symbol; QComboBox* move; triplet setTriplet(); signals: void editingFinished(); protected: void paintEvent(QPaintEvent* e) { QWidget::paintEvent(e); } private: triplet t; };
#endif // TABLE_EDITOR_H
@
editor source
@
#include <QGroupBox>
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QLabel>table_editor::table_editor()
{
QFormLayout *layout = new QFormLayout();
n_state=new QComboBox();
n_symbol=new QComboBox();
move = new QComboBox();
layout->addRow(new QLabel("Iduće stanje: "), n_state);
layout->addRow(new QLabel("Izlazni simbol: "), n_symbol);
layout->addRow(new QLabel("Pomak: "), move);QPushButton *ok = new QPushButton("Prihvati"); QPushButton *cancel = new QPushButton("Odustani"); QDialogButtonBox *buttonBox = new QDialogButtonBox(); buttonBox->addButton(ok, QDialogButtonBox::AcceptRole); buttonBox->addButton(cancel, QDialogButtonBox::RejectRole); connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); layout->addRow(buttonBox); //layout->addWidget(buttonBox); this->setLayout(layout); connect(buttonBox, SIGNAL(accepted()), this, SIGNAL(editingFinished()));
}
triplet table_editor::setTriplet()
{
t.nextState=n_state->currentText().toInt();
t.nextSymbol=n_symbol->currentText();
t.shift=shifts.value(move->currentText());
return t;
}
@the slot, belonging to the table model
@
triplet table_model::activate_editor()
{
table_editor *edit= new table_editor();
edit->setModal(true);
edit->setGeometry(200, 200, 100, 100);
edit->n_state->addItems(table_model::get_state());
edit->n_symbol->addItems(table_model::get_symbols());
edit->move->addItems(table_model::get_move_options());connect(edit, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor())); edit->exec(); triplet *t = new triplet(); t->nextState=edit->n_state->currentText().toInt(); t->nextSymbol=edit->n_symbol->currentText(); t->shift=edit->shifts.value(edit->move->currentText()); return *t;
}
@
and how I'm calling it right now from setData
@
bool table_model::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
const int a=index.column()*100+index.row();
triplet t = activate_editor();
_table->insert(a, t);
emit dataChanged(index, index);
return true;
}
return false;
}
@I googled, tried, and failed. Could you help?