I have a question puzzles me for a long time. The class FormResliceViewer derives from QVTKOpenGLNativeWidget. vtkResliceImageViewer is used, with SetRenderWindow() by the FormResliceViewer object. Image reslice can be rendered well in the Viewer. Actually, there are 3 image reslice viewers. I defined them in a for loop. I choose one of the viewers to describe my question.
class FormResliceViewer : public QVTKOpenGLNativeWidget
{
public:
FormResliceViewer(QWidget* parent=nullptr);
void setImageFileName(QString fileName);
void setBorderColor(QColor color);
protected:
void paintGL()override;
// bool renderVTK()override;
// void paintEvent(QPaintEvent * e)override;
// some other members
......
}
Code in header:
vtkSmartPointer m_resliceImageViewers[3];
FormResliceViewer* m_formResliceViewers[3];
vtkSmartPointer m_imageRenderWindows[3];
Code in main initial function:
for (int i=0; i<3; ++i)
{
m_imageRenderWindows[i] = vtkGenericOpenGLRenderWindow::New();
m_resliceImageViewers[i] = vtkSmartPointer<vtkResliceImageViewer>::New();
m_resliceImageViewers[i]->SetRenderWindow(m_imageRenderWindows[i]);
m_formResliceViewers[i] = new FormResliceViewer();
m_formResliceViewers[i]->SetRenderWindow(m_imageRenderWindows[i]);
m_formResliceViewers[i]->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
m_resliceImageViewers[i]->SetupInteractor(m_imageRenderWindows[i]->GetInteractor());
vtkResliceCursorLineRepresentation *rep = vtkResliceCursorLineRepresentation::SafeDownCast(
m_resliceImageViewers[i]->GetResliceCursorWidget()->GetRepresentation());
rep->GetResliceCursorActor()->GetCursorAlgorithm()->SetReslicePlaneNormal(i);
int color[3] = {0, 0, 0};
color[i] = 255;
QColor qColor(color[0],color[1],color[2]);
m_formResliceViewers[i]->setBorderColor(qColor);
// m_resliceImageViewers[i]->SetResliceCursor(m_resliceImageViewers[0]->GetResliceCursor());
}
Now I want to add green border(2D content) to the Viewer. I override the function paintGL().
void FormResliceViewer::paintGL()
{
QPainter painter(this);
painter.begin(this);
QPen pen;
pen.setColor(m_borderColor);
pen.setWidth(2);
painter.setPen(pen);
painter.drawRect(0, 0, width(), height());
QVTKOpenGLNativeWidget::paintGL();
}
But I can NOT see any border. If I comment the last line (QVTKOpenGLNativeWidget::paintGL();). I can see the green border appear, but the image reslice disappear. That’s not the result I want.
void FormResliceViewer::paintGL()
{
QPainter painter(this);
painter.begin(this);
QPen pen;
pen.setColor(m_borderColor);
pen.setWidth(2);
painter.setPen(pen);
painter.drawRect(0, 0, width(), height());
// QVTKOpenGLNativeWidget::paintGL();
}
I try to override function renderVTK() and paintEvent() with the same code above, but all fail. I guess there must be something I have ignore that to keep the image reslice and the border exitence. Or native Qt 2D content drawing API is not supported in VTK? I would appreciate it if someone could give me some demo code or advice!
P.S. My development environment is VTK 8.2.0 in ubuntu 18.04.