0

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);


};
User123456
  • 13
  • 4

1 Answers1

0

try to change

OverLay *m_Overlay; 

to

extern OverLay *m_Overlay;

and please refer this.

Another Singleton solution:

Header:

class OverLay : public QWidget
{
public:
    OverLay(const int &color, QWidget *parent = 0);

    static OverLay* createInstance(const int &color, QWidget *parent = 0);
protected:
     virtual void paintEvent(QPaintEvent *) override;

private:
     std::vector<int> m_points;
     int m_total_frames;
     int m_type;

     static OverLay* m_instance;
};

Part of cpp:

OverLay *OverLay::createInstance(const int& color, QWidget *parent)
{
    if(m_instance != NULL)
        m_instance->deleteLater();
    m_instance = new OverLay(color, parent);
    return m_instance;
}

OverLay *OverLay::m_instance = NULL;

How to get the global instance:

#include "overlay.h"
...
OverLay* overlay = OverLay::createInstance(123);
JustWe
  • 4,250
  • 3
  • 39
  • 90
  • 1
    Then overlay should not be destroyed. It should be hided. – Dmitry Sazonov Mar 26 '18 at 09:20
  • Hi, I just want to repaint the qwidget depending on color parameters. I can delete it or simply redraw again. I am facing problem only in declaration in mainwindow.h – User123456 Mar 26 '18 at 15:16
  • Please see updated question. Everything is working perfectly fine with just one instance of Overlay. I can also create as many instances of Overlay in mainwindow.cpp. But, faces problem while declaring globally in mainwindow.h – User123456 Mar 26 '18 at 15:53
  • @User123456 did you try the code? if you want to make a global instance, you should use the `extern` keyword or `Singleton`. if you need recreate the instance just delete it first before `new`. – JustWe Mar 27 '18 at 00:12
  • @Jiu, thanks for the solution. However, the problem was somewhere else in the program. – User123456 Mar 31 '18 at 09:01