Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Edit data from a custom QAbstractTableModel through a modal dialog

Edit data from a custom QAbstractTableModel through a modal dialog

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marinamarina
    wrote on last edited by
    #1

    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_OBJECT

        public:
            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&#40;&#41;;
    triplet *t = new triplet(&#41;;
    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?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved