I have following code for custom Qwidget
class OverLay : public QWidget
{
public:
OverLay(int color=0, QWidget *parent = 0);
protected:
virtual void paintEvent(QPaintEvent *) override;
private:
int m_type;
};
Depending on the color value, I am drawing modifying paintEvent.
Now I want to declare OverLay *m_Overlay globally. So that if one instance of it is already created, I will delete it and create new one.
So in mainwindow.h, I have
OverLay *m_Overlay; //After adding this line, application crashes on start
In mainwindow.cpp,
m_Overlay = NULL;
and in function, am checking
if(m_overlay!=NULL){
delete m_overlay;
}
m_overlay = new OverLay(type,ui->slider->parentWidget());
m_overlay->setGeometry(ui->slider_video->geometry());
m_overlay->show();
Declaration itselfs gives error. May I please know what I am doing wrong?
Update
As @Taz742 suggested, I added Q_OBJECT macro. Now am able to declare one instance of Overlay in mainwindow.h
OverLay *m_Overlay;
However, application again crashes on startup if I declare one more instance.
OverLay *m_Overlay1,*m_Overlay2;
Update 2 Here is my mainwindow.h. I am declaring m_Overlay as private. Also class is added in mainwindow.h
class OverLay : public QWidget
{
Q_OBJECT
public:
OverLay(int color=0, QWidget *parent = 0);
~OverLay() {}
protected:
virtual void paintEvent(QPaintEvent *) override;
private:
int m_type;
};
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void pushbuttonclicked();
private:
Ui::MainWindow *ui;
OverLay *m_overlay;
void draw_overlay(int type);
};