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

  • SEARCH
  • Home
  • 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 7633961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:02:15+00:00 2026-05-31T07:02:15+00:00

I need to implement a table in Qt that shows a Combo Box on

  • 0

I need to implement a table in Qt that shows a Combo Box on each row on a particular column.

So far based on this example:
http://doc.qt.nokia.com/4.7-snapshot/itemviews-spinboxdelegate.html
and on this question:
QStandardItem + QComboBox
I succesfully managed to create a QItemDelegate.

My problem is that everything works fine if I implement it from my main() function in main.cpp, but it does not work well if I insert the table in Qt Designer to use it then within a function of MainWindow class in mainwindow.cpp.

Could you please give me a clue? Thanks in advance!

Delegation on main.cpp (it works well, when I double click on the second column it shows a Combo Box):

QStandardItemModel model(4, 2);
QTableView tableView;
tableView.setModel(&model);
ComboBoxDelegate delegate;
tableView.setItemDelegateForColumn(1,&delegate);
tableView.horizontalHeader()->setStretchLastSection(true);

for (int row = 0 ; row < 4; ++row) {
    for (int col = 0; col < 2; ++col) {
        QModelIndex index = model.index(row, col, QModelIndex());
        model.setData(index, QVariant((row+1) * (col+1)));
    }
}
tableView.show();

Delegation on mainwindow.cpp alternative 1 (it shows an empty table)

QStandardItemModel model(4,2);
ui->tablePoint->setModel(&model);
ComboBoxDelegate delegate;
ui->tablePoint->setItemDelegateForColumn(1,&delegate);
ui->tablePoint->horizontalHeader()->setStretchLastSection(true);

for (int row = 0 ; row < 4; ++row) {
    for (int col = 0; col < 2; ++col) {
        QModelIndex index = model.index(row, col, QModelIndex());
        model.setData(index, QVariant((row+1) * (col+1)));
    }
}

Delegation on mainwindow.cpp alternative 2 (it shows the table but when I double click on the second column it does not show the Combo Box. Instead it shows the regular Spin Box):

QStandardItemModel* model = new QStandardItemModel(4,2);
ui->tablePoint->setModel(model);
ComboBoxDelegate delegate;
ui->tablePoint->setItemDelegateForColumn(1,&delegate);
ui->tablePoint->horizontalHeader()->setStretchLastSection(true);

for (int row = 0 ; row < 4; ++row) {
    for (int col = 0; col < 2; ++col) {
        QModelIndex index = model->index(row, col, QModelIndex());
        model->setData(index, QVariant((row+1) * (col+1)));
    }
}
  • 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-31T07:02:18+00:00Added an answer on May 31, 2026 at 7:02 am

    Assuming that in both mainwindow.cpp cases, the code you show is in the constructor, you do the classic “coming from java, python, perl or any language where you don’t give a damn about memory management” error to allocate your model and/or delegate on the stack rather than on the heap.

    In c++, stuff allocated on the stack (e.g. QStandardItemModel model(4,2);) is destroyed when returning from the function, while stuff allocated on the heap (e.g. QStandardItemModel* model = new QStandardItemModel(4,2); lives until you delete it).
    Case 1 works because stuff allocated on the stack in main() lives for the duration of the app.

    Bottom line: for case 3, do a ComboBoxDelegate* delegate = new ComboBoxDelegate(); and ui->tablePoint->setItemDelegateForColumn(1,delegate);

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

Sidebar

Related Questions

I need to implement a table in Qt that shows a QComboBox on each
Designing a simple HTML table that I am going to implement alternate-row coloring on...
I need to implement a dynamic div that pops up for each user. I
I need to implement a feature that would allow users to filter table data
I have an HTML table with several columns and I need to implement a
This is is my case: I upload an Excel file and each row is
I need implement simple search in small content table: id, name, description, content. Results
I need to implement the abcd list indexing in the below table view right
I need to store a customer 's currently selected payment info ,so that this
I've got a table from an existing LabView VI (ewww!) that I need to

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.