简体   繁体   English

QT覆盖QWidget错误

[英]QT Overlay QWidget Error

I have following code for custom Qwidget 我有以下代码用于自定义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. 根据颜色值,我在画修改paintEvent。

Now I want to declare OverLay *m_Overlay globally. 现在,我要全局声明OverLay * m_Overlay。 So that if one instance of it is already created, I will delete it and create new one. 因此,如果已经创建了一个实例,则将其删除并创建一个新实例。

So in mainwindow.h, I have 所以在mainwindow.h中,我有

OverLay *m_Overlay; //After adding this line, application crashes on start

In mainwindow.cpp, 在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. 正如@ Taz742所建议的,我添加了Q_OBJECT宏。 Now am able to declare one instance of Overlay in mainwindow.h 现在可以在mainwindow.h中声明一个Overlay实例

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. 更新2这是我的mainwindow.h。 I am declaring m_Overlay as private. 我将m_Overlay声明为私有。 Also class is added in mainwindow.h 还在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);


};

try to change 尝试改变

OverLay *m_Overlay; 

to

extern OverLay *m_Overlay;

and please refer this . 请参考这个

Another Singleton solution : 另一个Singleton解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM