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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:14:29+00:00 2026-05-11T19:14:29+00:00

I am a noob in using model/view paradigm in Qt and have the following

  • 0

I am a noob in using model/view paradigm in Qt and have the following problem: I have a tree-like structure, that must be visualized via Qt. I found out, that QAbstractTableModel is perfect for my needs, so I write the following:

class columnViewModel : public QAbstractTableModel {
   // some stuff...
};

Everything now works, but now I have to implement an “Observer” design pattern over the nodes of my tree. Whenever the node expands in the TreeView, I must add an Observer to the corresponding node. Whenever the node collapses, I must remove this Observer from the node. So, I write something, like this:

void onExpand( const QModelIndex & Index ... ) {
   Node* myNode = static_cast<Node*>(Index->internalPointer());
   Observer* foo = Observer::create();
   myNode->addObserver(foo);

   // ok up to here, but now where can I save this Observer? I must associate 
   // it with the Node, but I cannot change the Node class. Is there any way 
   // to save it within the index?
}

void onCollapse( const QModelIndex & Index ... ) {
   Node* myNode = static_cast<Node*>Index->internalPointer();
   Observer* foo = // well, what should I write here? Node does not have anything 
                   // like "getObserver()"! Can I extract an Observer from Index?

   myNode->remObserver( foo );
}

I don’t have the snippets right now, so the code may be not a valid Qt, but the problem seems clear. I can change neither Node nor Observer classes. I can have a inner list of Observers, but then I have to resolve, what Observer to remove from the specific node. Is there any way to save the Observer pointer within Index (some user data maybe), to resolve it quickly in onCollapse? Any ideas would be welcome…

  • 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-11T19:14:30+00:00Added an answer on May 11, 2026 at 7:14 pm

    Do the following:

    • Define a new role (similar to Qt::UserRole), let’s say ObserverRole.
    • Use QAbstractItemModel::setData to
      set the observer as data with
      observer role. The code sketch:

      this->model()->setData( ObserverRole, QVariant::fromValue( foo));

    • You might need to put in the cpp implementation file a declaration for metadata, something like

      Q__DECLARE __METATYPE ( Observer* );

    to allow QVariant to make the variant casts in a proper way.

    • You can get the observer for an index using QModelIndex::data with Observer role:

      index.data( ObserverRole);

    • In your model implementation, add support for returning data for Observer role, if any (as you probably did for Qt::UserRole or Qt::DisplayRole.

    Update on received comment:

    Normally, the QModelIndex::data gives the data for the viewer. The role specified when asking for data allows the customizer of the model to provide different data for different reasons (e.g. provide a string for display role -> the title of the item).

    If you don’t use this mechanism for getting the data, then you probably don’t need QTreeView. In this case, use QTreeWidget, where you can work directly with QTreeWidgetItems and to attach data to the item through setData method, or to subclass QTreeWidgetItem and add the data as member of that subclass.

    The views are usually used when you want to work with models.

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

Sidebar

Related Questions

Python noob using 2.7 on Windows. I'm working on making a hierarchy tree view
I'm a CSS noob. I have an image of an arrow that I'm using
Using MVC3 for the first time, so sorry for the noob-like question. I'm trying
I feel like a real noob asking this, but here's my problem: I want
I'm trying to pull data from a QR code via zxing using the following
In a Rails(3.2) app, I have a class method on a Model like this:
I am sorry but I am a complete noob on using maven. There is
I'm a Rails noob very interested in experimenting with mobile app development using trigger.io.
I'm a total noob at EE. We are using version 1.6.6 and I need
Noob question, apologies. I'm compiling Java in Windows Vista's command-line and have so many

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.