Tag Archives: Qt Vtk wglMakeCurrent failed

[Solved] Qt Vtk wglMakeCurrent failed in MakeCurrent(), error: The handle is invalid.

Problem description

The qvtkwidget control is used in QT’s interface. When clicking the close button in the upper right corner of the window, an error is reported: wglmakecurrent failed in makecurrent(), error: the handle is invalid .

Solution:

By overriding the void closeevent (qcloseevent * event) function, manually delete all qvtkwidget type controls, and then close the window.

Example

The qvtkwidget type controls in the QT design interface are as follows:

mymainwindow. H

class MyMainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MyMainWindow(QWidget *parent = Q_NULLPTR);
protected:
	void closeEvent(QCloseEvent *event); 
private:
    Ui::MainWindow ui;
};

mymainwindow.cpp

MyMainWindow::MyMainWindow(QWidget *parent): QMainWindow(parent)
{
    ui.setupUi(this);
}

void MyMainWindow::closeEvent(QCloseEvent *event) {
	delete ui.qvtkWidget;
	delete ui.qvtkWidget_2;
	delete ui.qvtkWidget_3;
	delete ui.qvtkWidget_4;
	event->accept();
}

Postscript

I don’t know whether such hasty delete processing will cause other problems. I hope you can make more corrections.