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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:57:33+00:00 2026-05-28T03:57:33+00:00

Shown below is the my custom table model. I am trying to use that

  • 0

Shown below is the my custom table model. I am trying to use that tablemodel together with a QTableView. If the method append of the table model is called I would expect the table view to update its contents. But it doesn’t and I don’t know why. If however I use that same table model together with a QListView, everything works fine, i.e. the list view does update its contents, when append of the table model gets called. Is there anything special I need to do in case of the QTableView?

class MyModel : public QAbstractTableModel
{
public:

    MyModel(QObject* parent=NULL) : QAbstractTableModel(parent) {}

    int rowCount(const QModelIndex &parent = QModelIndex()) const {
        return mData.size();
    }

    int columnCount(const QModelIndex &parent = QModelIndex()) const {
        return 2;
    }

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const {
        if (!index.isValid()) {
            return QVariant();
        }

        if (role == Qt::DisplayRole) {
            if (index.column()==0) {
                return QVariant(QString::fromStdString(getFirst(index.row())));
            }
            if (index.column()==1) {
                return QVariant(QString::fromStdString(getSecond(index.row())));
            }
        }

        return QVariant();
    }

    void append(std::string const& first, std::string const& second) {
        mData.push_back(std::make_pair(first, second));

        emit dataChanged(index(mData.size()-1, 0), index(mData.size()-1, 1));
    }

    std::string const& getFirst(int i) const {
        return mData[i].first;
    }

    std::string const& getSecond(int i) const {
        return mData[i].second;
    }

protected:

    std::vector<std::pair<std::string, std::string> > mData;
};
  • 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-28T03:57:34+00:00Added an answer on May 28, 2026 at 3:57 am

    As you’re inserting a new row instead of changing existing data, you should use beginInsertRows and endInsertRows instead:

    void append(std::string const& first, std::string const& second) {
        int row = mData.size();
        beginInsertRows( QModelIndex(), row, row );
    
        mData.push_back(std::make_pair(first, second));
    
        endInsertRows();
    }
    

    See if that helps.

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

Sidebar

Related Questions

I've a RichTextBox which I use as output from a custom TraceListener shown below:
How to create custom checkbox that looks like the one shown below. The checkboxes
I have made my custom In extension method as shown below: public static class
I have a ListView (shown below). I would like the cells to flash a
I have a table as shown below Q_ID DeptID EmployeeName City 100 100 testest
Given the following html table and script shown below I am having a problem
I have noticed that my geocoder is inconsistent in the code shown below because
Everything is working fine until I call the saveFile method (shown below) to write
I'm using a table view that uses a custom UITableViewCell subclass, NoteCell . The
I am trying to use a custom ExpandableListView with parent and child layout with

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.