Creating temporary database, adding and loading Dicoms

I have a question regarding creating temporary database, I got the script from ‘ScriptRepository’ of creating temporary database and loading DICOMs to slicer " How to load DICOM files into the scene from a folder" . My issue is that when I populates the database using “importDicom” function, the database is still empty. I’m working on c++ loadable module in which I want to load DICOM files into temporary database and as loader is python based module, I have to process python script in c++. Here is what this code looks like:

PythonQt::init();
PythonQtObjectPtr context = PythonQt::self()->getMainModule();
context.addVariable(“folderName”, d->folderName);
context.evalScript(QString(“import os,sys,slicer,random\n”
“from pathlib import Path\n”
“dicomDataDir=folderName\n”
“directory=‘TEMP’\n”
“from DICOMLib import DICOMUtils\n”
“with DICOMUtils.TemporaryDICOMDatabase(directory) as db:\n”
" DICOMUtils.clearDatabase(db)\n"
" DICOMUtils.importDicom(dicomDataDir, db)\n"
" patientUIDs = db.patients()\n"
" for patientUID in patientUIDs:\n"
" DICOMUtils.loadPatientByUID(patientUID)"));

The above code is called on click of button and “folderName” variable has all the dicom files. When this evalScript is done, in separate function in c++, I’m opening the database using following code but it shows empty output for patientsUIDs:

ctkDICOMDatabase *data;
data->openDatabase("/tmp/SlicerCAT-user/TEMP/ctkDICOM.sql"); //This is where temporary database is
QStringList patients = data->patients();
qDebug() << "Patients UIDs" << patients;
qDebug() << "StudyList" << data->studiesForPatient(patients[0]);

My question is that does the temporary database is cleared automatically when “evalScript” is run and finishes. During evalScript, I printed the studyList of loaded Dicoms and it shows correct output.

Loading data into Slicer’s persistent DICOM database is even simpler: replace

with DICOMUtils.TemporaryDICOMDatabase() as db

by this:

db = slicer.dicomDatabase

Alternatively, you can use this code snippet to import:
https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_import_DICOM_files_into_the_application.27s_DICOM_database

So there is no definite way to make persistent temporary database. I think temporary is not a good word to describe what I’m looking for. I am looking for a separate database for my application at my given location directory, separate from slicer’s default database. I was thinking of creating temporary database and using ctkDicomIndexer to populate and ctkDicomDatabase to open it. Sorry for late response, I was just figuring somethings out

You can pick any database directory you want and tell Slicer to use it. If you want the user to have their own config, you just need to remember to set if back if to their setting after they use your module. The temporary dicom database utilities are handy because they do that cleanup for you automatically after a transient operation.

A post was split to a new topic: Finding a DICOM series based on series description