Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Problem with QLabel, QPixmap and PaintEvent
Qt 6.11 is out! See what's new in the release blog

[Solved] Problem with QLabel, QPixmap and PaintEvent

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 15.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kamilus
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yshurik
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kamilus
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yshurik
          wrote on last edited by
          #4

          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)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kamilus
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              yshurik
              wrote on last edited by
              #6

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

              1 Reply Last reply
              1
              • K Offline
                K Offline
                kamilus
                wrote on last edited by
                #7

                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;
                

                }@

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kamilus
                  wrote on last edited by
                  #8

                  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]

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved