[vtkusers] Example VTK with Qt
agatte
agatakrason at gmail.com
Fri Aug 17 04:39:34 EDT 2012
agatteHi All ;)
I have a question concerning example vtk with qt.
I am trying to extend and modify example RenderWindowUISingleInheritance
from VTK WIKI
I want to load file with mesh (*.vtp) , display mesh and save mesh in
file(as *.vtp).
I have alredy display mesh.
I have created 2 push buttons open and save.
But I have a problem with read mesh from vtp file and save.
Could anyone help me please ?
I have principally problem with open/load file and save it.
I attach my code :
#include "ui_SimpleViewUI.h"
#include "SimpleViewUI.h"
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkPolyData.h>
#include <vtkRenderWindow.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLPolyDataWriter.h>
#include "vtkSmartPointer.h"
#include <QString>
#include <QFileDialog>
#include <QMessageBox>
#include <QFile>
// Constructor
SimpleView::SimpleView()
{
this->ui = new Ui_SimpleView;
this->ui->setupUi(this);
//std::string filename = "outputMeshXML.vtp";
//vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
//reader->SetFileName(filename.c_str());
//reader->Update();
vtkXMLPolyDataReader* reader = vtkXMLPolyDataReader::New();
vtkPolyData* polydata = vtkPolyData::New();
polydata = reader->GetOutput();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInput(polydata);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// VTK Renderer
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
// VTK/Qt wedded
this->ui->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
// Set up action signals and slots
connect(this->ui->actionExit, SIGNAL(triggered()), this,
SLOT(slotExit()));
connect(this->ui->openButton, SIGNAL(clicked()), this, SLOT(openFile()));
connect(this->ui->saveButton, SIGNAL(clicked()), this, SLOT(saveFile()));
};
void SimpleView::slotExit()
{
qApp->exit();
}
void SimpleView::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open file with model/mesh"), "",
tr("files (*.vtp);;All Files (*)"));
if (fileName.isEmpty())
return;
else {
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_5);
const char* filename = fileName.toStdString().c_str();
vtkXMLPolyDataReader* reader = vtkXMLPolyDataReader::New();
reader->SetFileName(filename);
reader->Update();
vtkPolyData* polyadata = reader->GetOutput();
//reader->Delete();
}
}
void SimpleView::saveFile()
{
// std::cout <<" save file "<<std::endl;
QString fileName = QFileDialog::getSaveFileName(this, tr("Save
File"),
"Save mesh/ model in .vtp file ",
tr("Images/models (*.vtp)"));
if (fileName.isEmpty())
return;
else {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("Unable to open
file"),
file.errorString());
return;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_5);
const char* filename2 = fileName.toStdString().c_str();
vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New();
writer->SetInput(polydata);
writer->SetFileName(filename2);
writer->Write();
//writer->Delete();
//polydata->Delete();
}
}
SimpleView::~SimpleView()
{
}
--
View this message in context: http://vtk.1045678.n5.nabble.com/Example-VTK-with-Qt-tp5715374.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list