level 3
zero9079
楼主
画一个矩形,但是不知道是不是坐标不对,把void PaintRect::setLine(double point1[], double point2[])函数中的第三行改成lineSource->SetPoint1(point2);的话,还能划出一条线,虽然坐标不对。我这个代码就什么都画不出来,我很是头疼啊,搞了好几天,发现不了问题,哪位大神帮忙看下呀,万分感谢![[泪]](/static/emoticons/u6cea.png)
void PaintRect::mousePressEvent(QMouseEvent* event)
{
if (NULL == event) return;
if (event->button() == Qt::RightButton)
{
status = 1;
x = event->pos().x();
y = event->pos().y();
}
else
{
QVTKWidget::mousePressEvent(event);
}
}
void PaintRect::mouseMoveEvent(QMouseEvent *event)
{
if (NULL == event) return;
if (1 == status)
{
/*renew;*/
width = event->pos().x() - x;
height = event->pos().y() - y;
//drawRect();
}
else
{
QVTKWidget::mouseMoveEvent(event);
}
}
void PaintRect::mouseReleaseEvent(QMouseEvent* event)
{
if (NULL == event) return;
if (event->button() == Qt::RightButton)
{
drawRect();
status = 0;
x = y = width = height = 0;
}
else
{
QVTKWidget::mouseReleaseEvent(event);
}
}
double PaintRect::getRect() const
{
return x, y, width, height;
}
void PaintRect::setRect(double x, double y, double width, double height)
{
this->x = x;
this->y = y;
this->width = width;
this->height = height;
}
void PaintRect::drawRect()
{
drawRect(x, y, width, height);
}
void PaintRect::drawRect(double x, double y, double width, double height)
{
//QMessageBox::information(this, "Note", "drawRect event", QMessageBox::Yes);
double leftup[3], leftdown[3], rightup[3], rightdown[3];
leftup[0] = x; leftup[1] = y; leftup[2] = 0;
leftdown[0] = x; leftdown[1] = y + height; leftdown[2] = 0;
rightup[0] = x + width; rightup[1] = y; rightup[2] = 0;
rightdown[0] = x + width; rightdown[1] = y + height; rightdown[2] = 0;
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
this->setLine(leftup, rightup);
this->setLine(rightup, rightdown);
this->setLine(rightdown, leftdown);
this->setLine(leftdown, leftup);
}
void PaintRect::setLine(double point1[], double point2[])
{
vtkSmartPointer<vtkLineSource> lineSource = vtkSmartPointer<vtkLineSource>::New();
lineSource->SetPoint1(point1);
lineSource->SetPoint2(point2);//改为lineSource->SetPoint1(point2);能画出四条线但坐标不对。
lineSource->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(lineSource->GetOutputPort());
actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetLineWidth(2);
actor->GetProperty()->SetColor(0.0, 1.0, 0.0);
this->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actor);
this->GetRenderWindow()->Render();
}
结果图如下, 改成lineSource->SetPoint1(point2)就是这样,而且坐标不对,不改就什么也出不来。

2016年11月11日 02点11分
1
void PaintRect::mousePressEvent(QMouseEvent* event)
{
if (NULL == event) return;
if (event->button() == Qt::RightButton)
{
status = 1;
x = event->pos().x();
y = event->pos().y();
}
else
{
QVTKWidget::mousePressEvent(event);
}
}
void PaintRect::mouseMoveEvent(QMouseEvent *event)
{
if (NULL == event) return;
if (1 == status)
{
/*renew;*/
width = event->pos().x() - x;
height = event->pos().y() - y;
//drawRect();
}
else
{
QVTKWidget::mouseMoveEvent(event);
}
}
void PaintRect::mouseReleaseEvent(QMouseEvent* event)
{
if (NULL == event) return;
if (event->button() == Qt::RightButton)
{
drawRect();
status = 0;
x = y = width = height = 0;
}
else
{
QVTKWidget::mouseReleaseEvent(event);
}
}
double PaintRect::getRect() const
{
return x, y, width, height;
}
void PaintRect::setRect(double x, double y, double width, double height)
{
this->x = x;
this->y = y;
this->width = width;
this->height = height;
}
void PaintRect::drawRect()
{
drawRect(x, y, width, height);
}
void PaintRect::drawRect(double x, double y, double width, double height)
{
//QMessageBox::information(this, "Note", "drawRect event", QMessageBox::Yes);
double leftup[3], leftdown[3], rightup[3], rightdown[3];
leftup[0] = x; leftup[1] = y; leftup[2] = 0;
leftdown[0] = x; leftdown[1] = y + height; leftdown[2] = 0;
rightup[0] = x + width; rightup[1] = y; rightup[2] = 0;
rightdown[0] = x + width; rightdown[1] = y + height; rightdown[2] = 0;
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
this->setLine(leftup, rightup);
this->setLine(rightup, rightdown);
this->setLine(rightdown, leftdown);
this->setLine(leftdown, leftup);
}
void PaintRect::setLine(double point1[], double point2[])
{
vtkSmartPointer<vtkLineSource> lineSource = vtkSmartPointer<vtkLineSource>::New();
lineSource->SetPoint1(point1);
lineSource->SetPoint2(point2);//改为lineSource->SetPoint1(point2);能画出四条线但坐标不对。
lineSource->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(lineSource->GetOutputPort());
actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetLineWidth(2);
actor->GetProperty()->SetColor(0.0, 1.0, 0.0);
this->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actor);
this->GetRenderWindow()->Render();
}
结果图如下, 改成lineSource->SetPoint1(point2)就是这样,而且坐标不对,不改就什么也出不来。
