我QVtkWidget
在我的主窗口中使用 a 。我在一个新线程中生成一个vtkRenderer
,完成后,我发送一个signal
连接主窗口的一个slot
以将渲染器发回并更新 QVtkWidget。
不幸的是,当我将渲染器添加到 vtkRenderWindow 时出现“访问冲突”异常。这是我的代码:
线程对象.h
class ThreadObject : public QObject
{
Q_OBJECT
public:
ThreadObject();
~ThreadObject();
signals:
void startShowPointSignal(QString filepath);
void endShowPointSignal(vtkRenderer* render);
public slots:
void showPoint(QString filepath);
};
线程对象.cpp
#include "threadobject.h"
ThreadObject::ThreadObject(){}
ThreadObject::~ThreadObject(){}
void ThreadObject::showPoint(QString filepath)
{
//qDebug() << filepath << QThread::currentThreadId();
std::ifstream filestream(filepath.toLocal8Bit());
std::string line;
int pointCounts = 0;
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetNumberOfComponents(3);
colors->SetName("Colors");
while (std::getline(filestream, line))
{
double x, y, z, r, g, b;
std::stringstream linestream;
linestream << line;
linestream >> x >> y >> z >> r >> g >> b;
points->InsertNextPoint(x, y, z);
colors->InsertNextTuple3(r, g, b);
}
filestream.close();
// Add the grid points to a polydata object
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);
polydata->GetPointData()->SetScalars(colors);
vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
glyphFilter->SetInputData(polydata);
glyphFilter->Update();
vtkSmartPointer<vtkCleanPolyData> cleaner = vtkSmartPointer<vtkCleanPolyData>::New();
cleaner->SetInputConnection(glyphFilter->GetOutputPort());
cleaner->Update();
// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
pointMapper->SetInputConnection(cleaner->GetOutputPort());
vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
pointActor->SetMapper(pointMapper);
// Create a renderer
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
// Add the actor to the scene
renderer->AddActor(pointActor);
renderer->ResetCamera();
renderer->SetBackground(0.1, 0.1, 0.1);
//send the renderer back to my qt window
emit endShowPointSignal(renderer);
//delete this;
}
主窗口.cpp
void MainWindow::test()
{
QThread* thr1 = new QThread();
ThreadObject* obj = new ThreadObject();
obj->moveToThread(thr1);
connect(obj, SIGNAL(startShowPointSignal(QString)), obj, SLOT(showPoint(QString)), Qt::QueuedConnection);
connect(obj, SIGNAL(endShowPointSignal(vtkRenderer*)), this, SLOT(updateVTK(vtkRenderer*)), Qt::QueuedConnection);
thr1->start();
obj->startShowPointSignal("D:/WorkSpace/VS Project/Plane/Datas/1.xyz");
}
void MainWindow::updateVTK(vtkRenderer* renderer)
{
double test[3];
renderer->GetBackground(test);
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer); // I GOT THE ACCESS VIOLATION THIS LINE!!!!!
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleTrackballCamera> istyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
renderWindowInteractor->SetInteractorStyle(istyle);
renderWindowInteractor->SetRenderWindow(renderWindow);
ui.qvtkWidget->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start();
}
我试图将所有代码放在主窗口中,而不是使用新线程,它很有趣。
那么当我使用QT线程时有什么问题吗?还是我没有注意到的其他东西?
我是QT新手,QT和VTK的版本分别是5.7和7。