需要有关 QTreeView 的 QAbstractItemModel 中的节点的示例吗?

发布于 2024-12-08 13:13:38 字数 803 浏览 0 评论 0原文

问题:我正在寻找有关为 QTreeView 创建模型(基于 QAbstractItemModel )的示例,但找不到 sane 代码。 Qt 示例基于QStandardModel,不是很有用且复杂,互联网示例是基于python的?!代码...其他信息无法给我正确的方向。所以,这就是我所拥有的:

  • std::map

    typedef std::map<公司数据,std::向量< ContractorData>,LessData<公司资料> >公司;

这是数据示例(CompanyName + ContractorsNames):

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

其中 * 表示 - 可扩展项目(父项)

等等我需要它用上面的数据创建QTreeView

有人可以帮忙吗?

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮筵 2024-12-15 13:13:38

因此,由于这里没有发布任何帖子,所以我在这里发布我自己的解决方案(也使用文本过滤):

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:类型 Companies 是其中

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

CompanyData, ContractorsData< /code> 是简单的结构...

祝你有美好的一天!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文