Qt: subclassing QStyledDelegate problems



  • I'm subclassing QStyledItemDelegate to give my table a custom editor widget. Triplet is the class whose instances I'm keeping in the table. There are several problems.

    1. By far the most interesting one is in table_editor.cpp where I'm connecting signals. The second signal I'm connecting comes from a Cancel button and it connects with QDialogButtonBox's reject slot, which does exist, according to the docs, but not according to my compiler! I'm using Qt Creator 3.2.0.

    2. When I click on a table cell, the editor appears as a separate dialog (what I want), but outside the main window, in the top left corner. Its position seems to mimic the QModelIndex: if I click a cell in the third column, the editor appears more to the right.

    Should I just drop the delegate and and go for setting up my table model so that its signal triggers the editor into being as a normal popup dialog when the cell is clicked?

    1. My editor doesn't seem to be changing the data in the actual table. I choose the values, click Ok and the editor closes, but the data in the cell is still the same.

    Here it all is:

    the delegate:

    table_delegate.h:
    @

    #include <QStyledItemDelegate>

    class table_delegate : public QStyledItemDelegate
    {
    Q_OBJECT

    public:
    table_delegate(QWidget *parent);

    void paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const;
    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;
    

    private slots:
    void commitAndCloseEditor();
    };

    @

    table_delegate.cpp:
    @#include "table_delegate.h"
    #include "table_editor.h"
    #include "triplet.h"
    #include "table_model.h"
    #include <QPainter>

    table_delegate::table_delegate(QWidget *parent):QStyledItemDelegate(parent)
    {

    }

    void table_delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {
    if (index.data().canConvert<triplet>()) {
    triplet t = qvariant_cast<triplet>(index.data());

        painter->drawText(option.rect, t.showTriplet());
    }
    

    }

    QWidget *table_delegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &option,
    const QModelIndex &index) const

    {
    if (index.data().canConvert<triplet>()) {
    table_editor *editor = new table_editor(parent);
    editor->setGeometry(200, 200, 200, 200);
    editor->n_state->addItems(table_model::get_state());
    editor->n_symbol->addItems(table_model::get_symbols());
    editor->move->addItems(table_model::get_move_options());

        connect(editor, SIGNAL(editingFinished()),
                this, SLOT(commitAndCloseEditor()));
        return editor;
    } else {
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
    

    }

    void table_delegate::setEditorData(QWidget *editor,
    const QModelIndex &index) const
    {
    if (index.data().canConvert<triplet>()) {
    triplet t = qvariant_cast<triplet>(index.data());
    table_editor *edit = qobject_cast<table_editor *>(editor);
    edit->setTriplet(t);
    } else {
    QStyledItemDelegate::setEditorData(editor, index);
    }
    }

    void table_delegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const
    {
    if (index.data().canConvert<triplet>()) {
    table_editor *edit = qobject_cast<table_editor *>(editor);
    model->setData(index, QVariant::fromValue(edit->getTriplet()));
    } else {
    QStyledItemDelegate::setModelData(editor, model, index);
    }
    }

    void table_delegate::commitAndCloseEditor()
    {
    table_editor *editor = qobject_cast<table_editor *>(sender());
    emit commitData(editor);
    emit closeEditor(editor);
    }
    @
    the editor

    table_editor.h:
    @
    #include <QPushButton>
    #include <QGroupBox>
    #include "triplet.h"

    class table_editor: public QWidget
    {
    Q_OBJECT

        public:
            table_editor(QWidget *parent);
            QMap<QString, signed int> shifts;
            QComboBox* n_state;
            QComboBox* n_symbol;
            QComboBox* move;
            void setTriplet(triplet a);
            triplet getTriplet();
    
        signals:
            void editingFinished();
    
        protected:
            void paintEvent(QPaintEvent* e) {
                QWidget::paintEvent(e);
            }
    
        private:
            triplet t;
        };
    

    @
    table_editor.cpp:
    @
    #include "table_editor.h"
    #include "table_model.h"
    #include "fshema_widget.h"
    #include <QFormLayout>
    #include <QGroupBox>
    #include <QDialogButtonBox>
    #include <QLineEdit>
    #include <QLabel>

    table_editor::table_editor(QWidget *parent)
    {

    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);
    
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
                                     | QDialogButtonBox::Cancel);
    
    
    
    layout->addRow(buttonBox);
    this->setLayout(layout);
    
    t.nextState=n_state->currentText().toInt();
    t.nextSymbol=n_symbol->currentText();
    t.shift=shifts.value(move->currentText());
    
    connect(buttonBox, SIGNAL(accepted()), this, SIGNAL(editingFinished()));
    connect(buttonBox, SIGNAL(rejected()), buttonBox, SLOT(reject()));
    

    }

    void table_editor::setTriplet(triplet a)
    {
    t.nextState=a.nextState;
    t.nextSymbol=a.nextSymbol;
    t.shift=a.shift;
    }

    triplet table_editor::getTriplet()
    {
    return t;
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.