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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:52:25+00:00 2026-05-16T04:52:25+00:00

i got a godgiven list of xyz (the code says int, just an example)

  • 0

i got a godgiven list of xyz (the code says int, just an example) glued into a QList (to big to move anywhere). How can I create a Model View for that? I allready read the Qt doc which tells me, I have to reimplement data, index, parent, rowCount, columnCount functions. But the preprocessor/compiler cries for more reimplemented functions? I allready read a chapter in my Qt Book but it did not help either. Here my hacked away code:

class XModel : public QAbstractListModel
{
Q_OBJECT
public:
    explicit XModel(QList<int> *valuelist, QObject *parent = 0);
    virtual ~XModel();
    int rowCount(const QModelIndex &) const;
    int columnCount(const QModelIndex &) const;
    QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex()) const;
    QModelIndex parent(const QModelIndex &index) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
private:
    QList<int>* blah;
signals:

public slots:

};



XModel::XModel(QList<int> *valuelist, QObject *parent) :
    QAbstractListModel(parent),
    blah(valuelist)
{

}




XModel::~XModel()
{

}



int XModel::rowCount(const QModelIndex &) const
{
    return blah->size();
}

int XModel::columnCount(const QModelIndex &) const
{
    return 1;
}


QModelIndex XModel::index(int row, int column, const QModelIndex &parent) const
{
    return createIndex(row, column, (void)&(blah->at(row)));
}



QModelIndex XModel::parent(const QModelIndex &index) const
{
    return createIndex(index->row(), index->column(), NULL); 
}




QVariant XModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
    return QVariant(blah->at(index.row()));
}

Do I even have to use QAbstractItemModel or does QAbstractListModel work the exact same way? How do I give the Model the source of the actual data? Is this only within data function? Please tell me what I am doing wrong, I do not see it and give advice on howto do it properly, (good) howtos welcome.

This is fixed, but…

EDIT:

Widget::Widget(QWidget *parent)
    : QWidget(parent),
    valuelist(),
    xm(&valuelist) //xm = XModel
{
    valuelist.append(1);
    valuelist.append(2);
    valuelist.append(3);
    valuelist.append(4);
    valuelist.append(5);
    valuelist.append(6);
    valuelist.append(7);
    valuelist.append(8);
    valuelist.append(9);

    view = new QListView(this);
    view->setModel(&xm);
    //how to force the XModel to reread the QList`?
    view->show();
}
  • 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-16T04:52:26+00:00Added an answer on May 16, 2026 at 4:52 am

    Add to and remove data from XModel and have XModel modify the underlying list (reference?) for you:

    Widget::Widget(QWidget *parent)
        : QWidget(parent),
        valuelist(),
        xm(&valuelist) //xm = XModel
    {
        xm.append(1);
        xm.append(2);
        xm.append(3);
        xm.append(4);
        xm.append(5);
        xm.append(6);
        xm.append(7);
        xm.append(8);
        xm.append(9);
    
        view = new QListView(this);
        view->setModel(&xm);
    
        xm.append(10); // should call beginInsertRows, append to valuelist, and call endInsertRows.
    
        Q_ASSERT(valuelist.contains(10));
    
        view->show();
    }
    

    Otherwise, you could perhaps create a mix of QObject and QList that can emit signals to notify XModel of changes, but I think the first approach is better.

    Edit:

    Here is a silly example. It will create a list-backed model that will append more elements to the list every second:

    #include <QtGui/QApplication>
    #include <QtGui/QListView>
    #include <QtCore/QAbstractListModel>
    #include <QtCore/QTimer>
    
    class ListBackedModel : public QAbstractListModel
    {
        Q_OBJECT
        QList<int>* m_list;
    
    public:
    
        ListBackedModel(QList<int>* list, QObject* parent = 0)
            : QAbstractListModel(parent)
            , m_list(list)
        {}
    
        ~ListBackedModel()
        {}
    
        int rowCount(const QModelIndex &parent = QModelIndex()) const
        {
            Q_UNUSED(parent);
            return m_list->size();
        }
    
        QVariant data(const QModelIndex &index, int role) const
        {
            if (index.row() >= rowCount()) { return QVariant(); }
            if (index.row() < 0) { return QVariant(); }
    
            int element = m_list->at(index.row());
    
            if (Qt::DisplayRole == role) {
                return QString::number(element);
            }
    
            if (Qt::ToolTipRole == role) {
                return tr("%1 is element #%2").arg(element).arg(index.row() + 1);
            }
    
            return QVariant();
        }
    
        void append(int element)
        {
            /*
                First is the new index of the first element that will be inserted.
                Last is the new index of the last element that will be inserted.
                Since we're appending only one element at the end of the list, the
                index of the first and last elements is the same, and is equal to
                the current size of the list.
            */
            int first = m_list->size();
            int last = first;
    
            beginInsertRows(QModelIndex(), first, last);
            m_list->append(element);
            endInsertRows();
        }
    
        void startAddingMoreElements()
        {
            QTimer::singleShot(1000, this, SLOT(addMoreElements()));
        }
    
    private slots:
    
        void addMoreElements()
        {
            append(qrand() % 100);
            startAddingMoreElements();
        }
    };
    
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QList<int> list;
        list << 1 << 10 << 34 << 111;
        ListBackedModel model(&list);
    
        QListView listView;
        listView.setModel(&model);
        listView.show();
    
        model.startAddingMoreElements();
    
        return a.exec();
    }
    
    #include "main.moc"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
Got a list saved by using .data(). How is it possible to find exact
Got a real simple question but can't figure out the solution. Got following code.
Got into confusion I have to do book shelf for tablet. Which holds books
Got this code in C#: using System; public class Listener{ public static void Main(){
Got a big problem here which killed my whole day... I cant fix it
Got this code, that only works when alert (markers.length); is uncommented. When this javascript
Got a mobile site with a jquery mobile menu (nested-list). Every item in the
got code looking like this: // This is com.n.common.Networking.downloadBmp( ) function // ... byte[]
Got a question regarding SQL and ColdFusion: I can't write SQL code properly, so

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.