Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6341565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:03:43+00:00 2026-05-24T20:03:43+00:00

I’m trying to use a delegate to customize the way data from a model

  • 0

I’m trying to use a delegate to customize the way data from a model is displayed when using a QDataWidgetMapper.

I have two different versions of a widget, one is view-only (the data is displayed in QLabels) and the other is used to edit the data (the data is displayed in appropriate editors).

The latter one works perfectly with the delegate, everything is fine.

As you may have guessed the problem arises with the first one… When mapping the sections of my model to QLabels using the QDataWidgetMapper, the delegate is never called and the data is displayed correctly for the sections with regular data (strings, ints,…) but no data is displayed for the sections of my model with a custom data type (a kind of list) which I would like to format as a string using the delegate.

I’ve already performed this operation successfully when the same data is displayed in a QTableView (the method paint() of the delegate is called when the data is displayed).

After having looked at it a little bit closer, I’ve been able to see that, when using QLabels to display the data, the delegate is never called though I’ve explicitly associated a delegate to the QDataWidgetMapper using its method setItemDelegate().

So in synthesis, assume a class CustomItemDelegate which inherits QStyledItemDelegate with virtual methods:

void CustomItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    qDebug() << "DELEGATE: PAINT" << index.column();
    QStyledItemDelegate::paint(painter, option, index);
}

void CustomItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    qDebug() << "DELEGATE: SET EDITOR DATA" << index.column();
    QStyledItemDelegate::setEditorData(editor, index);
}

and a widget with following in it:

QDataWidgetMapper* mapper = new QDataWidgetMapper();
CustomItemDelegate* delegate = new CustomItemDelegate();
mapper->setModel(model);
mapper->setItemDelegate(delegate);

mapper->addMapping(editorWidget, 1);
mapper->addMapping(label, 2, "text");

mapper->toFirst();

QTableView* view = new QTableView();
CustomItemDelegate* delegate2 = new CustomItemDelegate();
view->setModel(model);
view->setItemDelegate(delegate2);

the code outputs:

DELEGATE: SET EDITOR DATA 1
// NOTHING ?!

DELEGATE: PAINT 1
DELEGATE: PAINT 2

and as a result I got

  • my editorWidget with the correct data in it (whatever data type the section contains: regular or custom, as long as the editor handles the type of course),
  • my label only displays the data if the section contains a regular type of the data as the delegate is not called
  • my view would display everything fine as the delegate is called for each section

So my questions are:

  • why isn’t the delegate called when the mapped widget is a QLabel?
  • in this case, how come the data is even displayed when the data type is regular? Magic?

Thanks very much and I apologize in advance if the answer is obvious (but even then, thank you for pointing it out :P),

ixM

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:03:44+00:00Added an answer on May 24, 2026 at 8:03 pm

    This is the code from QT that populates widgets

    void QDataWidgetMapperPrivate::populate(WidgetMapper &m)
    {
        if (m.widget.isNull())
            return;
    
        m.currentIndex = indexAt(m.section);
        if (m.property.isEmpty())
            delegate->setEditorData(m.widget, m.currentIndex);
        else
            m.widget->setProperty(m.property, m.currentIndex.data(Qt::EditRole));
    }
    

    In the first case when you do not specify a property delegate is used whereas in the second case the data is set to widget directly by passing your delegate.

    I don’t know why it was designed this way but this is how it works currently !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.