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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:13:16+00:00 2026-05-27T14:13:16+00:00

I am having trouble with showing items from a model defined in c++ in

  • 0

I am having trouble with showing items from a model defined in c++ in a SelectionDialog in qml. I’m trying to make an application for the Meego operating system.

If I display the items in a ListView, everything works as expected and the items are shown. However, if I try to show them in a SelectionDialog I get an empty list.

Here is some code:

languagemodel.h:

#ifndef LANGUAGEMODEL_H
#define LANGUAGEMODEL_H

#include <baza/language.h>
#include <QObject>
#include <QAbstractListModel>

class LanguageModel: public QAbstractListModel
{
    Q_OBJECT
public:
    enum LanguageRoles  {
        RoleLanguageName = Qt::DisplayRole,
        RoleLanguageCode = Qt::UserRole,
        RoleId = Qt::UserRole+1,
        RoleChosen = Qt::UserRole+2
    };

    LanguageModel(QObject *parent = 0);

    void addLanguge(const Language &language);
    void clear();

    int rowCount(const QModelIndex & parent = QModelIndex()) const;

    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;

private:
    QList<Language> m_languages;
};

#endif // LANGUAGEMODEL_H

languagemodel.cpp:

#include "languagemodel.h"
#include <QDebug>

LanguageModel::LanguageModel(QObject *parent)
    : QAbstractListModel(parent)
{
    QHash<int, QByteArray> roles;

    roles[RoleLanguageName]="name";
    roles[RoleLanguageCode]="code";
    roles[RoleId]="id";
    roles[RoleChosen]="chosen";

    setRoleNames(roles);
}

void LanguageModel::addLanguge(const Language &language){
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_languages << language;
    endInsertRows();
}

int LanguageModel::rowCount(const QModelIndex & parent) const  {
    return m_languages.count();
}

void LanguageModel::clear(){
    for (int i=0; i<m_languages.count(); i++)
    {
        beginRemoveRows(QModelIndex(), 0, 0);
        m_languages.removeAt(0);
        endRemoveRows();
    }
}

QVariant LanguageModel::data(const QModelIndex & index, int role) const  {
    if (index.row() < 0 || index.row() > m_languages.count())
        return QVariant();

    const Language &language = m_languages[index.row()];
    if (role == RoleLanguageName)
        return language.getName();
    else if (role == RoleLanguageCode)
        return language.getCode();
    else if (role == RoleChosen)
        return language.isChosen();
    else if (role == RoleId)
        return language.getId();

    return QVariant();
}

adding languages in done from handler.cpp like:

//(...)
for(unsigned int i = 0; i < listLanguages.size(); i++)
{
    Language language = listLanguages.at(i);

    modelLanguages.addLanguge(language);
}
//(...)
ctxt->setContextProperty("modelLanguages", &modelLanguages);

where
modelLanguages is a LanguageModel.

And I would like to display the list in Settings.qml:

//(...)
SelectionDialog {
    id: dialogLanguages

    titleText: qsTr("Select language")
    model: modelLanguages
    delegate: Text  { text: name}
}

When I open the dialogLanguages, I can only see the title “Select language” without any of the items from the model.

If I use this as a model, it works fine:

model: ListModel {
    ListElement { name: "ListElement #1" }
    ListElement { name: "ListElement #2" }
    ListElement { name: "ListElement #3" }
}

Also, as I have already mentioned, if I set modelLanguages as a model in a ListView, al the items are displayed correctly.

can anyone help and tell me what am I doing wrong here and what do I have to change to get it working.

Any help is appreciated!

Thank you!

  • 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-27T14:13:17+00:00Added an answer on May 27, 2026 at 2:13 pm

    OK, I managed to solve it! I found a hint for the soultion here in the comments: https://qt.gitorious.org/qt-components/qt-components/merge_requests/887#

    I needed to add a count property to my model inheriting QAbstractListModel.

    My LanguageModel.h now looks something like this:

    class LanguageModel: public QAbstractListModel
    {
        Q_OBJECT
    
        Q_PROPERTY(int count READ count NOTIFY countChanged)
    
        //(...)
    public:
        //(...)
        int count();
        //(...)
    signals:
        void countChanged(int newCount);
        //(...)
    

    I added the needed method in LanguageModel.cpp:

    //(...)
    int LanguageModel::count(){
        return m_languages.count();
    }
    //(...)
    

    I also emitted a signal when the count changes (when adding languages and clearing the model) with:

     emit countChanged(m_languages.count());
    

    And now it’s working. 🙂

    I hope this helps someone.

    Bye!

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

Sidebar

Related Questions

im trying to develop a social networking application and im having some trouble showing
I am having trouble initializing a constant array of constant strings. From week.h (showing
I am having trouble showing a row from a database which has a list
I'm having trouble seeing tables from one particular SQL Server 2008 database while trying
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
I'm having trouble showing and hiding a tag on my page. When the page
I'm having trouble showing the favicon of a subdomain (which redirects to a PHP
I am having trouble with MPMoviePLAYER not showing video just audio. It doesn't work

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.