Want to display customWidget in Qlistview , but its not rendering correctly QT c++

Viewed 36

I'm trying to display all dicom images found in a directory in a Qlistwidget . I am able to add the dicom to list but the customwidget is not rendering . I am new to qt so pls help me

The code that renders the list

void MainWindow::drawDICOMSeries(std::string folderDICOM) {

    currentFolder = QString::fromStdString(folderDICOM);
    QDir directory(currentFolder);
    QFileInfoList images = directory.entryInfoList(QStringList() << "*.dcm" <<"*.DCM",QDir::Files);
    foreach(QFileInfo filename, images) {

      qDebug() << filename.filePath() << "\n";

      auto item = new QListWidgetItem();

      auto widget = new DicomWidget(this);

      widget->setImage(filename.filePath() , filename.fileName() );

      item->setSizeHint(widget->sizeHint());

      ui->listWidget->addItem(item);
      ui->listWidget->setSizeIncrement(200, 200);
      ui->listWidget->setItemWidget(item, widget);
    }

    QFileSystemModel *dirModel = new QFileSystemModel(); //Create new model
    dirModel->setRootPath(currentFolder); //Set model path

    ui->treeView->setModel(dirModel); //Add model to QTreeView
    QModelIndex idx = dirModel->index(currentFolder); //Set the root item
    ui->treeView->setRootIndex(idx);
    currentFileDI = dirModel->filePath(idx);
    reader->SetDirectoryName(folderDICOM.c_str());
    reader->Update();
    connect(ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged,
            this, &MainWindow::currentChanged);
}

the custom widget .ui image

enter image description here

the output

enter image description here

UI code

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>DicomWidget</class>
 <widget class="QWidget" name="DicomWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>273</width>
    <height>163</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QSplitter" name="splitter">
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
     <widget class="QVTKWidget" name="diWidget" native="true"/>
     <widget class="QLabel" name="diLabel">
      <property name="text">
       <string>imageLabel</string>
      </property>
     </widget>
    </widget>
   </item>
  </layout>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QVTKWidget</class>
   <extends>QWidget</extends>
   <header location="global">qvtkwidget.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

the dicomwidget.cpp file

#include "dicomwidget.h"
#include "ui_dicomwidget.h"
#include <QDebug>

DicomWidget::DicomWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DicomWidget)
{
    ui->setupUi(this);
}

void DicomWidget::setImage(const QString &text , const QString label) {
   // ui->diWidget->setImage(text);
    std::string fileDICOM = text.toUtf8().constData();
      ui->diLabel->setText(label);
    qDebug()<<"My path"<< text;
    vtkSmartPointer<vtkDICOMImageReader> reader =
        vtkSmartPointer<vtkDICOMImageReader>::New();
      reader->SetFileName(fileDICOM.c_str());
      reader->Update();

      vtkSmartPointer<vtkImageViewer2> imageViewer =
        vtkSmartPointer<vtkImageViewer2>::New();
      imageViewer->SetInputConnection(reader->GetOutputPort());
      imageViewer->SetRenderWindow(ui->diWidget->GetRenderWindow());
      imageViewer->Render();
}


DicomWidget::~DicomWidget()
{
    delete ui;
}
0 Answers
Related