How to use pqLoadDataReaction and pqDataRepresentation correctly?

Hi,
I’m implementing my first custom application using QT, a few objects will be loaded into Paraview using the code and I have to change some properties, like color, size, position, etc.

I loaded the object with this code, it worked and the properties changed, but might crash.

I do not understand how the representation object and the view object work. Should I maintain a view object to load all the things in it? And is there some wrong with my code(except the empty check)?

pqAutoApplyReaction::setAutoApply(true);

QVector<pqPipelineSource *> QSourcePointer = pqLoadDataReaction::loadFilesForSupportedTypes(filelist);
pqDataRepresentation* representation = this->QSourcePointer->getRepresentation(this->QSourcePointer->getViews().at(0));
vtkSMPropertyHelper(representation->getProxy(), "Opacity").Set(0.25);

pqShowHideAllReaction::act(pqShowHideAllReaction::ActionType::Show);

The second thing is that sometimes I get an empty view object after loading one data object, so I can’t change any property of it in the same way.

QString filePath = this->cylinderPath;
if(FileTools::fileExist(filePath)){
   QStringList filePathList;
   QList<QStringList> fileList;
   filePathList << filePath;
   //If add the filePath twice, the size of the view object will be 1.
   //filePathList << filePath;
   fileList << filePathList;
   this->QSourcePointer = pqLoadDataReaction::loadFilesForSupportedTypes(fileList);
   
   // empty view object
   QList<pqView *> views = QSourcePointer.at(0)->getViews();
   pqDataRepresentation* representation = QSourcePointer.at(0)->getRepresentation(views.at(0));
}

I dont see anything wrong with your code.

You may want to rely on the ActiveView/ActiveSource logic.

Thanks for your answer. Where can I get the ActiveView/ActiveSource logic information? only by reading source code? Is that possible to specify a view to load data in pqLoadDataReaction?

https://kitware.github.io/paraview-docs/latest/cxx/classpqActiveObjects.html

Is that possible to specify a view to load data in pqLoadDataReaction?

No, use vtkSMParaViewPipelineControllerWithRendering::Show to show a source in a view.

I have identified the cause of the crash. I tried to access index 0 of getViews(), but unfortunately, the views are currently empty. I have written a code to pause momentarily and ensure that the view is fully prepared before proceeding.

bool SphDockWidget::wait_view(){
    bool success = false;
    int count = 10;

    while (!success && count >= 0) {
        if(!this->QSourcePointer.empty()) {
            if (!this->QSourcePointer.at(0)->getViews().empty()) {
                success = true;
            }
        }

        if (!success) {
            QEventLoop loop;
            QTimer::singleShot(10, &loop, &QEventLoop::quit);

            loop.exec();
            count--;
        }
    }
    return success;
}