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

标签 qt signals-slots qabstractitemmodel

我有一个 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。我认为这是因为它不是一个实际的项目,而只是一个指向一个项目的指针?如何获取插入的文本/元素?

最佳答案

由于父级对于顶级项目无效,另一个选择是让 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);
   }
}

关于qt - 帮助在Qt的rowInserted信号之后获取插入的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1202182/

相关文章:

c++ - 如何在 Qt 5 中将 New-Signal-Slot 语法声明为函数的参数

c++ - C/C++ 与 Python 之间交换值的应用程序设计

python - 继承QAbstrctitemModel、PyQt时使用Lists作为QModelIndex中的internalPointer

python - Qt:按下按钮之前运行脚本

javascript - 如何在没有 id 和 class 的情况下获取当前元素?

qt - 我可以从 Qt Creator 中的自定义构建步骤获取构建警告吗?

qt - Qt-发射时隙而不是信号

c++ - Qt:从 View 中删除模型

qt - 我可以在 QThread 中使用与 QAbstractTableModel 数据的交互吗?

c++ - Qt 原始类型,与 QTimer 和 C++ 竞赛