帮助在 Qt 的 rowInserted 信号后获取插入的数据

发布于 2024-07-29 18:43:33 字数 766 浏览 4 评论 0原文

我有一个 onText 方法,它连接到 QAbstractItemModel 的 rowsInserted SIGNAL,这样我就可以在插入新行时收到通知:

QObject::connect(model, SIGNAL(rowsInserted ( const QModelIndex & , int , int  )  ),
                        client_,SLOT(onText( const QModelIndex & , int , int  )) )

该信号工作正常,因为我在插入行时收到通知。 这是 onText 方法:

void FTClientWidget::onText( const QModelIndex & parent, int start, int end ) 
{
    Proxy::write("notified!");

    if(!parent.isValid())
        Proxy::write("NOT VALID!");
    else
        Proxy::write("VALID");

     QAbstractItemModel* m = parent.model();


}

但我似乎无法从插入的项目中获取字符串。 传递的 QModelIndex“parent”无效,“m”QAbstractItemModel 为 NULL。 我认为这是因为它不是一个实际的项目,而只是一个指向一个项目的指针? 如何获取插入的文本/元素?

I have a onText method that connects to a QAbstractItemModel's rowsInserted SIGNAL so I can be notified when new rows have been inserted:

QObject::connect(model, SIGNAL(rowsInserted ( const QModelIndex & , int , int  )  ),
                        client_,SLOT(onText( const QModelIndex & , int , int  )) )

The signal works fine, since I am notified when rows are inserted. Here is the onText method:

void FTClientWidget::onText( const QModelIndex & parent, int start, int end ) 
{
    Proxy::write("notified!");

    if(!parent.isValid())
        Proxy::write("NOT VALID!");
    else
        Proxy::write("VALID");

     QAbstractItemModel* m = parent.model();


}

But I can't seem to be able to get the string from the inserted items. The QModelIndex "parent" passed is NOT VALID, and "m" QAbstractItemModel is NULL. I think its because it's not a actual item, but just a pointer to one? How do I get a hold of the inserted text/elements?

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

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

发布评论

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

评论(2

失退 2024-08-05 18:43:34

由于父项对于顶级项目无效,另一个选择是让 FTClientWidget 访问模型(如果它不违反您的预期设计),然后 FTClientWidget 可以直接在模型本身上使用开始和结束参数:

void FTClientWidget::onText( const QModelIndex & parent, int start, int end ) 
{
   //Set our intended row/column indexes 
   int row = start;
   int column = 0;

   //Ensure the row/column indexes are valid for a top-level item
   if (model_->hasIndex(row,column))
   {
      //Create an index to the top-level item using our 
      //previously set model_ pointer
      QModelIndex index = model_->index(row,column);

      //Retrieve the data for the top-level item
      QVariant data = model_->data(index);
   }
}

As the parent will be invalid for top level items, another option would be to give FTClientWidget access to the model (if it doesn't violate your intended design), and then FTClientWidget can use the start and end arguments directly on the model itself:

void FTClientWidget::onText( const QModelIndex & parent, int start, int end ) 
{
   //Set our intended row/column indexes 
   int row = start;
   int column = 0;

   //Ensure the row/column indexes are valid for a top-level item
   if (model_->hasIndex(row,column))
   {
      //Create an index to the top-level item using our 
      //previously set model_ pointer
      QModelIndex index = model_->index(row,column);

      //Retrieve the data for the top-level item
      QVariant data = model_->data(index);
   }
}
烟酉 2024-08-05 18:43:34

父级对于顶级项目始终无效,因此您可以预期它是无效的。 Qt 文档有一个很好的解释父母是如何工作的。 start 是插入子项的第一行,end 是插入子项的最后一行。

因此,您可以通过如下方式访问它:

int column = 0;

// access the first child
QModelIndex firstChild = parent.child(first, column);
QModelIndex lastChild = parent.child(end, column);

// get the data out of the first child
QVariant data = firstChild.data(Qt::DisplayRole);

或者,如果您愿意,您可以使用索引来检索可以从中访问它的模型。

The parent will always be invalid for the top level items, so you can expect it to be invalid. The Qt documentation has a good explanation of exactly how the parent works. start is the first row at which a child has been inserted, and end is the last row at which a child was inserted.

Thus, you can access it with something like the following:

int column = 0;

// access the first child
QModelIndex firstChild = parent.child(first, column);
QModelIndex lastChild = parent.child(end, column);

// get the data out of the first child
QVariant data = firstChild.data(Qt::DisplayRole);

Or, if you want, you can use the index to retrieve the model from which you can access it.

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