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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:39:52+00:00 2026-05-26T21:39:52+00:00

Problem: I’m looking example about creating model ( based on QAbstractItemModel ) to QTreeView

  • 0

Problem: I’m looking example about creating model ( based on QAbstractItemModel ) to QTreeView, but can’t find sane codes. Qt examples are based on QStandardModel, which is not very useful and complex, Internet examples are based on python?! codes… Other information can’t gave me the right directions to go. So, here is what I have:

  • std::map

    typedef std::map< CompanyData, std::vector< ContractorData >, LessData< CompanyData > > Companies;

Here it’s data example (CompanyName + ContractorsNames):

[Microsoft]*
   [Bill Gates]
   [Steve Balmer]
   [...]
[Apple]*
   [Steve Jobs - R.I.P.]
   [Wozniak]
[OtherStuff]*
...

where * means – expandable item (parent)

And all that I need it to create QTreeView with this data above!

Can any one help?

Many 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-05-26T21:39:53+00:00Added an answer on May 26, 2026 at 9:39 pm

    So, as no posts was done here, I post here my own solution (also used text filtering):

    void ContractorsFilter::onCustomFilterChanged( const QString& text )
    {
         try
         {
              struct MatchFilter
              {
                   // data
                   QString        filter_;
                   Companies&     filtered_recipients_;
    
                   // methods
                   MatchFilter( const QString& _filter, Companies& _recipients )
                        : filter_( _filter )
                        , filtered_recipients_( _recipients )
                   {
                        filtered_recipients_.clear();
                   }
    
                   void operator()( const Companies::value_type& val ) const
                   {
                        bool isFound = false;
                        std::vector< ContractorData >::const_iterator con_i( val.second.begin() ), con_e( val.second.end() );
                        for( ; con_i != con_e; ++con_i )
                        {
                             const QString contractorName = (*con_i).name;
                             if( contractorName.contains( filter_, Qt::CaseInsensitive ) )
                             {
                                  filtered_recipients_[ val.first ].push_back( (*con_i) );
                                  isFound = true;
                             }
                        }
    
                        const QString companyName = val.first.name;
                        if( companyName.contains( filter_, Qt::CaseInsensitive ) )
                        {
                             filtered_recipients_[ val.first ].push_back( ContractorData() );
                        }
                   }
              };
    
              struct FillView
              {
                   // data
                   QFont     boldFont;
                   QBrush    whiteBrush;
    
                   QStandardItemModel* model_;
    
                   // methods
                   FillView( QStandardItemModel* model )
                        : model_( model )
                   {
                        model_->clear();
                   }
    
                   void operator ()( const Companies::value_type& val ) const
                   {
                        struct AppendContractors 
                        {
                             // data
                             QStandardItem* parent_;
    
                             // methods
                             AppendContractors( QStandardItem* _parent = 0 )
                                  : parent_( _parent )
                             {}
    
                             bool isEmpty( const ContractorData& contractor ) const
                             {
                                  return contractor.id.isEmpty();
                             }
    
                             void operator()( const std::vector< ContractorData >::value_type& contractor ) const
                             {
                                  if( !isEmpty( contractor ) )
                                  {
                                       QStandardItem *item = 0;
    
                                       QList< QStandardItem* > line;
                                       line << ( item = new QStandardItem( QIcon( ACCOUNT_ITEM_ICON ), contractor.name ) );
                                       item->setSizeHint( QSize( 0, 25 ) );
    
                                       parent_->appendRow( line );
                                  }
                             }
                        };
    
                        QStandardItem *parentItem = model_->invisibleRootItem();
    
                        // добавляем новую компанию + контрагента
                        QList< QStandardItem* > line;
                        line << ( parentItem = new QStandardItem( QIcon( COMPANY_ITEM_ICON ), val.first.name ) );
                        parentItem->setSizeHint( QSize( 0, 25 ) );
    
                        model_->appendRow( line );
    
                        std::for_each( val.second.begin(), val.second.end(), AppendContractors( parentItem ) );
                   }
              };
    
              // удаляем символ(ы), которые не фильтруются
              // формируется новая таблица, которая и будет использоваться моделью для отображения
              std::for_each( data_.begin(), data_.end(),
                   MatchFilter( text, filter_data_ ) );
    
              // вывод отфильтрованных контрагентов
              std::for_each( filter_data_.begin(), filter_data_.end(),
                   FillView( model_ ) );
    
              ui_.treeView->expandAll();
         }
         catch( const std::exception& e )
         {
              Core::errLog( "ContractorsFilter::onCustomFilterChanged", e.what() );
              throw;
         }
    }
    

    PS: type Companies is a

    typedef std::map< CompanyData, ContractorsData, LessData< CompanyData > > Companies;
    

    where CompanyData, ContractorsData are simple structures…

    Have a nice day!

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

Sidebar

Related Questions

Problem: I can set the exposureMode property of AVCaptureDevice, but it does not stay
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem: Given a list of strings, find the substring which, if subtracted from the
Problem I have timestamped data, which I need to search based on the timestamp
Problem! I Have the following input (rules) from a flat file (talking about numeric
Problem: Find people whose birthdays are tomorrow (table a), who havent got a record
Problem: Trying to create a Mix that is applied to the AVPlayerItem, but it
PROBLEM SOLVED. Special thanks to Andrew Noyes for explaining its not a class but
Problem: to combine PATHs with filenames, such that I can easily source many files.
Problem: I'm trying to load a stylesheet in Java but I get an error

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.