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 6186347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:52:26+00:00 2026-05-24T01:52:26+00:00

I don’t have enough experience with Qt yet to make a good design choice.

  • 0

I don’t have enough experience with Qt yet to make a good design choice. Any help by experienced Qt programmers would be very appreciated.
I’m trying to find out which model to subclass, which view to use, what delegate subclassing / extending I should do…

My problem is similar to: I have these zones I would like to display, 1 per row:

class Zone{

    //inputs
    string country;  //edited with a QComboBox
    string city;     //edited with a QComboBox
    int ageMin;

    //stored result
    int nbInhabitantsOlderThanMin;
}

Here’s what I’d like to do, and the design choices each requirements makes me think of:

  • I would like to display a list of them (–> QListView )
  • But to display 1 item I need several columns (–> QTableView )
  • I would like a double click on a row to trigger editing in a custom widget, since nbInhabitantsOlderThanMin can not be edited, and choosing a country restricts the list of cities that can be chosen in the QComboBox (and vice versa in my real example) (–> I should probably use a QDataWidgetMapper (or subclass?) somewhere…)

    So whereas the edition of a row should happen in a widget, the display is simple / not custom, and subclassing a delegate (QStyledItemDelegate for instance) (I’m not so sure about this one) doesn’t seem to be the right way to have 1 custom widget with many child input widget to edit the 3 fields at the same time.
    I think the data to model would favor a model subclassing QAbstractListModel, but the display with many columns compatible with default delegate viewing favors a QAbstractTableModel..

So I don’t really know which design to go for. Any experienced help connecting the dots is very welcome 🙂

  • 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-24T01:52:27+00:00Added an answer on May 24, 2026 at 1:52 am

    QDataWidgetMapper is a slightly different thing. It is a way to display one item from a Model (ex. QStandardItemModel), using custom controls. You can read more about it here, with accompanying snapshots and an example of how to implement one.

    While it is certainly cool, I don’t think it is what you want here. Mostly because you specified that you want to view your items in a list format. However, you could display all your items in a simple list, double-click which would open a dialog using the QDataWidgetMapper. In which case all you would need to do with a QListView/QListWidget is implement the double-click event.

    Still, I personally don’t like the added burden of the extra window on a user. I prefer to use popups sparingly. But if you like that approach, then go ahead. This is another example of the QDataWidgetMapper which is pretty nice.

    My preferred approach is still to use the QTableView, and provide delegates for the columns that need specialized editing. Here is a great walk-through of all things Model/View. So if you decide to use the QListView or QTableView it will give you a great start. It also talks about how you can create delegates to edit fields however you want.

    So, how do you create a custom delegate? Basically, you just inherit from QItemDelegate. There are some examples in the link above, but I’ll highlight a few salient points.

    QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
         const QStyleOptionViewItem &/* option */,
         const QModelIndex &index) const
     {
         QComboBox *editor = new QComboBox (parent);
         //  Add items to the combobox here.
         //  You can use the QModelIndex passed above to access the model
         //  Add find out what country was selected, and therefore what cities
         //  need to be listed in the combobox
    
         return editor;
     }
    
    void ComboBoxDelegate::setEditorData(QWidget *editor,
                                         const QModelIndex &index) const
     {
         int value = index.model()->data(index, Qt::EditRole).toInt();
    
         QComboBox  *comboBox= static_cast<QComboBox *>(editor);
         int _SelectedItem = // Figure out which is the currently selected index;
         comboBox->setCurrentIndex(_SelectedItem);
     }
    
    void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                        const QModelIndex &index) const
     {
         QComboBox  *comboBox= static_cast<QComboBox *>(editor);
         comboBox->interpretText();
         int value = comboBox->currentIndex();
         //  Translate the current index to whatever you actually want to
         // set in your model.
    
         model->setData(index, value, Qt::EditRole);
    }
    

    Fill in the gaps that I left in my example and you have your Delegate.
    Now, how to use this in your QTableView:

    You can set a delegate for a particular column of your table as follows:

    setItemDelegateForColumn(_ColumnIndex, new ComboBoxDelegate(_YourTableModel));
    

    And, if you want to prevent certain columns from being editable:

    _YourTableModel->setColumnEditable(_ColumnIndex, false);
    

    Once you have your model set up, everything else should take care of itself.

    Hope that helps.

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

Sidebar

Related Questions

I don't have any experience in updating a Rails app and when I google
I don't have much PHP experience and I want to know how to best
Lenses don't seem to have any disadvantages while having significant advantages over standard Haskell:
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if anyone can help me with this or if it's even possible.
Don't be afraid to use any technical jargon or low-level explanations for things, please.
Don't they both have to convert to machine code at some point to execute
I don't know: if this works. if it's a good idea. what it is
Don't overthink this - there's a very commonly used term and I ... have

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.