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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:04:46+00:00 2026-06-09T19:04:46+00:00

Im very new in Qt programming i want to display data with Qt TableView

  • 0

Im very new in Qt programming i want to display data with Qt TableView which im getting from XML file.

I Cannot find any useful tutorial about how to create custom model and then bind it to TableView.

Please provide some help or if anybody have some good examples please share.

Thanks

  • 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-06-09T19:04:47+00:00Added an answer on June 9, 2026 at 7:04 pm

    The model-view approach in Qt is quite versatile. All models inherit from QAbstractItemModel. With this class you can create quite complex data layouts (lists, trees, tables etc.) but the effort for the implementation is in comparison quite high, too.

    A ready to use class would be QStandardItemModel. You can easily create a table model and add the items, which are instances of QStandardItem. You can use the following code to get started:

    #include <QtGui>
    
    QStandardItemModel* createModel(QObject* parent)
    {
        const int numRows = 10;
        const int numColumns = 10;
    
        QStandardItemModel* model = new QStandardItemModel(numRows, numColumns);
        for (int row = 0; row < numRows; ++row)
        {
            for (int column = 0; column < numColumns; ++column)
            {
                QString text = QString('A' + row) + QString::number(column + 1);
                QStandardItem* item = new QStandardItem(text);
                model->setItem(row, column, item);
            }
         }
    
        return model;
    }
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QMainWindow window;
        QTableView* view = new QTableView;
        view->setModel(createModel(view));
        window.setCentralWidget(view);
        window.show();
    
        return app.exec();
    }
    

    You see, it is really easy to use. However, a drawback is that you have to supply the data via a QStandardItem, which might be a waste of memory. For example, assume you have several 100MB of data, which you would like to display in a view. As you already have the data stored somewhere, it would be preferable to just adapt it such that it can be used in the view instead of creating a QStandardItem for every cell.

    This is where QAbstractTableModel comes into play. The following example creates a matrix
    with 250.000 entries. Instead of creating one QStandardItem for every matrix element,
    we sub-class QAbstractTableModel and implement the three pure virtual methods
    numRows(), numColumns() and data(), which return the number of rows, columns and
    the data to display.

    #include <QtGui>
    
    class MatrixModel : public QAbstractTableModel
    {
    public:
        MatrixModel(int numRows, int numColumns, double* data)
            : m_numRows(numRows),
              m_numColumns(numColumns),
              m_data(data)
        {
        }
    
        int rowCount(const QModelIndex& parent = QModelIndex()) const
        {
            return m_numRows;
        }
    
        int columnCount(const QModelIndex& parent = QModelIndex()) const
        {
            return m_numColumns;
        }
    
        QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
        {
            if (!index.isValid() || role != Qt::DisplayRole)
                return QVariant();
    
            // Return the data to which index points.
            return m_data[index.row() * m_numColumns + index.column()];
        }
    
    private:
        int m_numRows;
        int m_numColumns;
        double* m_data;
    };
    
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        // Create a matrix.
        const int numRows = 500;
        const int numColumns = 500;
        double matrix[numRows][numColumns];
        for (int i = 0; i < numRows; ++i)
            for (int j = 0; j < numColumns; ++j)
                matrix[i][j] = i + j;
    
        // Create a model which adapts the data (the matrix) to the view.
        MatrixModel model(numRows, numColumns, (double*)matrix);
    
        QMainWindow window;
        QTableView* view = new QTableView;
        view->setModel(&model);
        window.setCentralWidget(view);
        window.show();
    
        return app.exec();
    }
    

    As you can see, the model does not duplicate any data but just serves as an adapter. If you need even more flexibility, you can go to QAbstractItemModel and event implement
    the creation of the model indexes, which Qt uses to specify which model data to read or
    write.

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

Sidebar

Related Questions

I am very new to C++ programming and you will see why. I want
I am very new to programming and I can't seem to find a way
I'm very new to web programming and jQuery. I want to load images and
Hello I am very new to programming. I want to learn very fastly joomla
I am very new to programming. I want to be able to run this
I'm still very new to programming and I want to write the cleanest code
I am very new at programming. I have the following problem. I want to
I'm very new to programming and I would really appreciate any help! I am
Hey I am very new to Web Programming. I have been learning PHP from
I'm still very new at programming, and our local SSIS genius isn't here today

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.