[Solved] Problem with QLabel, QPixmap and PaintEvent



  • I have my own Widget called FrameWidget and in this widget i have a label in which i display QPixmap.
    The first problem is that at the begining the label is black and i don't know why.
    The second problem is when i call paintEvent function everything is drawed under my label ;/

    When i put in my code @QPainter painter(label);@ the compilator show me:
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::setPen: Painter not active
    QPainter::setBrush: Painter not active
    QPainter::drawRects: Painter not active

    Can someone help me with this?

    my widget:
    @
    FrameWidget::FrameWidget(QWidget *parent) :
    QWidget(parent)
    {
    layout = new QVBoxLayout;
    label = new QLabel;
    layout->setContentsMargins(10,10,10,10);
    layout->addWidget(label);

    QImage temp(640,480,QImage::Format_RGB32);
    frame = temp;       
    
    QPixmap obraz = QPixmap::fromImage(frame);
    label->setPixmap(obraz);
    
    setLayout(layout);
    

    }@

    @
    void FrameWidget::paintEvent(QPaintEvent *event)
    {
    QPainter painter(this);
    painter.setPen(QPen(Qt::black, 12, Qt::SolidLine, Qt::RoundCap));
    painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
    painter.drawRect(0,0, 300, 300);
    }@



  • That's not right to paint not on this in paintEvent of the object.
    If you would like to have painter of label it should be paintEvent of the label



  • can You give me an example how i can do this ?? I understand what You said, but i still don't know how to do this, i can't write sth like this:

    @
    void label::paintEvent(QPaintEvent *event)
    {
    QPainter painter(this);
    painter.setPen(QPen(Qt::black, 12, Qt::SolidLine, Qt::RoundCap));
    painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
    painter.drawRect(0,0, 300, 300);
    }@



  • You are painting in label, so you need to subclass it:
    class MyLabel : public QLabel { ... };
    You need instantiate correct object in FrameWidget:
    label = new MyLabel;
    And then surely implement MyLabel::paintEvent(...) where you do your own painting on this (which is now MyLabel instance - what you needed)



  • thanx a lot for Your help, i do this and it's work, but my problem is a little bit complicated, I want to have in this label an image from camera i have frames as a QPixmap, and I want to have four markers (2 horizontal and 2 vertical) on this QPixmap and i would like to move this markers.

    After I implemented function @void myLabel::paintEvent(QPaintEvent *e){...}@ the frames from my camera don't show in myLabel. Can you help me with this?



  • Do you paint these frames in paintEvent?
    If not then there is no one who paint it.

    Is it painted by superclass QLabel? - then inside of your paintEvent() you need to call QLabel::paintEvent before your paintings (they will go to top).



  • it's my function to show frames from camera in myLabel
    @
    void FrameWidget::putFrame(IplImage *cvFrame){

    switch(cvFrame->depth){
    case IPL_DEPTH_8U:
        switch (cvFrame->nChannels){
        case 3:
            frame = QImage((const uchar *)cvFrame->imageData, cvFrame->width, cvFrame->height, QImage::Format_RGB888).rgbSwapped();
        break;
    
        default:
        QMessageBox msgBox;
        msgBox.setText("Wrong number of channels");
        msgBox.exec();
        break;
        }
     break;
    
     default:
     QMessageBox msgBox;
     msgBox.setText("This type of image is not implemented");
     msgBox.exec();
     break;
     }
    label->setPixmap(QPixmap::fromImage(frame));  // its myLabel *label = new myLabel;
    

    }@



  • it's work, once again thank You, You help me a lot

    [quote author="yshurik" date="1306611173"]Do you paint these frames in paintEvent?
    If not then there is no one who paint it.

    Is it painted by superclass QLabel? - then inside of your paintEvent() you need to call QLabel::paintEvent before your paintings (they will go to top).[/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.