How to connect events from QAbstractItemDelegate widget?

  • Hi,

    I have a widget containing a QTreeView with a QStyledItemDelegate in column 0 creating a QSpinBox editor.
    I would like to display in a QLabel and in real time the value of the QSpinBox each time it changes (so when QSpinBox::valueChanged(int) signal is emitted, for example when the mouse wheel is used inside the QSpinBox or up/down buttons are clicked).

    Does someone know how to do such a connection between QStyledItemDelegate and the QLabel?

    Thanks in advance for your response.
    Best regards.

  • I guess, you could reimplement QStyledItemDelegate, add a slot to your derived class and connect this slot to the label after constructing it.

  • Easiest is to take control of the actual creation of the QSpinBox. For instance, by subclassing QStyledItemDelegate, and reimplementing the createEditor method like this:
    QWidget* MyDelegate::createEditor( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    //let the base class do the hard work
    QWidget editor = QStyledItemDelegate::createEditor(parent, option, index);

    QSpinBox* spinbox = qobject_cast<QSpinBox*>(editor);
    if (spinbox) {
    connect(spinbox, SIGNAL(valueChanged(int)), this, SLOT(onSpinboxValueChanged(int)));

    return editor;

  • Dear franku & Andre, thanks for your response.

    To Andre: "spinbox" is NULL using qobject_cast.
    But I finally wrote this code which works:
    QWidget * AngleDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
    QSpinBox *editor = new QSpinBox(parent);
    connect(editor, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));

    return editor;


    I resend the "QSpinBox::valueChanged()" signal outside my delegate and can then connect any QObject (like a QLabel) to the "AngleDelegate::valueChanged()" signal.

    Problem solved.
    Thanks again.

  • Hello there, pin..

    I have a similar problem and I can't figure out how to solve the connect thing, maybe you can give me a hand on it.

    I have a QTableView inherited and also a QStyledItemDelegate inherited so I can change them as needed.

    One of the columns has a widget (QSlider) that I paint using drawComplexControl from QStyle and I create the real QSlider to edit when the editor opens. In the other column I store the value of this slider.

    The problem is that I can't figure out how to keep the value updated in real-time, because now I can only update it when setModelData is called.

    I can't figure how to connect the signal valueChanged to a slot of mine that would then update the desired item in the table view (that would be in the same row but another column).

    I don't think any code is relevant because I have the same structure as you with createEditor but here it goes:

    Here I create the slider, the commented line is what would be my connect (but where should I use the slot or signal I'd call/emit?)
    [code]QWidget *HsSubunitConfigurationDelegate::createEditor(QWidget *parent, const StyleOptionViewItem &option, const QModelIndex &index) const{
    QSlider slider = new QSlider(Qt::Horizontal, parent);
    connect(slider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int))); */
    return slider;

    Here I set the data for both the item that hold the widget (I can't get from the value column I want to update because I decode the value in a string later on and I don't feel like decoding it back to a value) and then I set the value to the column I want to be updated in real-time:
    [code]void HsSubunitConfigurationDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
    QSlider slider = qobject_cast<QSlider>(editor);
    model->setData(index, slider->value(), Qt::UserRole+12);
    model->setData(model->index(index.row(), 3), slider->value(), Qt::DisplayRole);

    Sorry if I forgot to post any code or if I wasn't clear.. if you don't understand my problem I can try to ask it again in other words. Also sorry for any misspelling.

    Thanks in advance.

Log in to reply

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