Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QLabel上的绘图点出错_C++_Qt_Qlabel - Fatal编程技术网

C++ QLabel上的绘图点出错

C++ QLabel上的绘图点出错,c++,qt,qlabel,C++,Qt,Qlabel,我试图在Qt中使用QLabel,如下所示: paintscene.h: class PaintScene : public QWidget { Q_OBJECT public: PaintScene(QWidget* parent = NULL); QVector<QLabel*> _layers; QColor _color; int _width; void mousePressEvent(QMouseEvent* event)

我试图在
Qt
中使用
QLabel
,如下所示:

paintscene.h:

class PaintScene : public QWidget
{
    Q_OBJECT
public:
    PaintScene(QWidget* parent = NULL);

    QVector<QLabel*> _layers;
    QColor _color;
    int _width;

    void mousePressEvent(QMouseEvent* event);

private slots:
    void updateWidth();
};
之所以需要这个列表,是因为我想实现层的工作(
QLabel
-一个单独的层)

然而,我得到一个错误,程序终止。错误发生在行
QImage tmp=\u layers.back()->pixmap()->toImage()

是什么让这一切发生的?如何解决这个问题?对于层来说,可能要使用不同的东西,而不是与for
QLabel::pixmap()不同的
QLabel

。。。所以当你这样做的时候:

QImage tmp = _layers.back()->pixmap()->toImage();
pixmap()
返回NULL(因为QLabel上从未设置过任何QPixmap),然后您尝试取消对该NULL指针的引用以调用其上的
toImage()
,从而导致崩溃

为了避免崩溃,不要尝试从空QPixmap指针创建QImage

我怀疑您想调用
grab()
而不是
pixmap()
-
grab()
将为您创建一个包含QLabel视觉外观的QPixmap。然而,一个更好的方法是完全避免混淆
qpixmap
;相反,创建自己的
QLabel
类的子类,并重写其
paintEvent(QPaintEvent*)
方法,首先调用
QLabel::paintEvent(e)
,然后使用QPainter绘制附加点。这将更容易实现,在运行时也更高效。

关于错误的原因是正确的,没有QPixmap这将是空的,在我的回答中,我将展示一个可能的解决方案

void PaintScene::mousePressEvent(QMouseEvent *event)
{
    QLabel *label = _layers.back();
    const QPixmap *pix= label->pixmap();
    QPixmap pixmap;
    if(pix)
        pixmap =  *pix;
    else{
        pixmap = QPixmap(label->size());
        pixmap.fill(Qt::transparent);
    }
    QPainter painter(&pixmap);
    QPen paintpen(_color);
    paintpen.setWidth(_width);
    painter.setPen(paintpen);
    painter.drawPoint(event->pos());
    painter.end();
    label->setPixmap(pixmap);
}

QImage tmp = _layers.back()->pixmap()->toImage();
void PaintScene::mousePressEvent(QMouseEvent *event)
{
    QLabel *label = _layers.back();
    const QPixmap *pix= label->pixmap();
    QPixmap pixmap;
    if(pix)
        pixmap =  *pix;
    else{
        pixmap = QPixmap(label->size());
        pixmap.fill(Qt::transparent);
    }
    QPainter painter(&pixmap);
    QPen paintpen(_color);
    paintpen.setWidth(_width);
    painter.setPen(paintpen);
    painter.drawPoint(event->pos());
    painter.end();
    label->setPixmap(pixmap);
}