Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to move a draw (Qpainter) to Qlabel ?



  • hello all, want to move a painter to a specific QLabel in the window . I tried with moving the QPainter to QPixmap (my_pix) and use: ui->label_pic->setPixmap(my_pix); but while writing QPainter painter(myPix) I get "not matching constructor".Any help ? (few days ago i started learning QT so please give me a simple answer)
    void thirdwindow::paintEvent(QPaintEvent*)
    {
    // Initialization
    unsigned int numCellX = 8, numCellY = 8;
    QRect wRect = rect();
    unsigned int cellSizeX = wRect.width() / numCellX;
    unsigned int cellSizeY = wRect.height() / numCellY;
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

        // Draw the background. The whole widget is drawed.
        painter.setBrush(QColor(0,0,0)); //black
        painter.drawRect(wRect);
    
        // Draw the cells which are of the other color
        // Note: the grid may not fit in the whole rect, because the size of the widget
        // should be a multiple of the size of the cells. If not, a black line at the right
        // and at the bottom may appear.
        painter.setBrush(QColor(255,255,255)); //white
        for(unsigned int j = 0; j < numCellY; j++)
            for(unsigned int i = j % 2; i < numCellX; i+=2)
               painter.drawRect(i * cellSizeX, j * cellSizeY, cellSizeX, cellSizeY);
    

    }
    and this is the drawing :
    7651f7dd-d310-43b4-91be-b3e97285580f-image.png


  • Lifetime Qt Champion

    Hi
    I think you just missed a &

        int h = ui->label->height();
        int w = ui->label->width();
    
        QPixmap pix(w, h); // give pixmap some size
        QPainter paint(&pix); // assign painter to it. note the &
        pix.fill( Qt::white );
    

    ps. looking good with the board !



  • @mrjj thank you for answering .
    here is the code:
    void thirdwindow::paintEvent(QPaintEvent*)
    {
    // Initialization
    int h = ui->label_pic->height();
    int w = ui->label_pic->width();

        QPixmap pix(w, h); // give pixmap some size
        QPainter painter(&pix); // assign painter to it. note the &
    
        unsigned int numCellX = 8, numCellY = 8;
        QRect wRect = rect();
        unsigned int cellSizeX = wRect.width() / numCellX;
        unsigned int cellSizeY = wRect.height() / numCellY;
        //QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
    
        // Draw the background. The whole widget is drawed.
        //painter.setBrush(QColor(0,0,0)); //black
        pix.fill( Qt::black);
    
    
        painter.drawRect(wRect);
    
        // Draw the cells which are of the other color
        // Note: the grid may not fit in the whole rect, because the size of the widget
        // should be a multiple of the size of the cells. If not, a black line at the right
        // and at the bottom may appear.
        //painter.setBrush(QColor(255,255,255));
        pix.fill( Qt::white );//white
        for(unsigned int j = 0; j < numCellY; j++)
            for(unsigned int i = j % 2; i < numCellX; i+=2)
               painter.drawRect(i * cellSizeX, j * cellSizeY, cellSizeX, cellSizeY);
    

    }

    and the result :
    qt.gui.icc: fromIccProfile: failed minimal tag size sanity and an empty window , i think it misses a little detail coz the code compiles.
    ps: after two days i finally get it !


  • Lifetime Qt Champion

    Hi
    Why are you painting to a pixmap inside paintEvent ?
    You are allowed to that outside PaintEvent when to a pixmap.
    as the only time where you can use QPainter outside paintEvent.

    Also you dont seem to anything with the pixmap after ?
    like ui->somelabel->setPixmap(pix);

    Do you want what paintEvent draws in some other window or why do you want to paint
    to pixmap anyway ?
    (since you have paintEvent in thirdwindow already)



  • @mrjj i add this function outside the painEvent :
    void thirdwindow::drawlabel(){
    int h = ui->label_pic->height();
    int w = ui->label_pic->width();

       QPixmap pix(w, h); // give pixmap some size
       QPainter painter(&pix); // assign painter to it. note the &
       pix.fill( Qt::white );
    

    }

    ==> nothing has changed (I get the first board ) but when I add it to the constructor , I get totally the oposite of what I want :
    66606495-29d6-441e-a30c-dceff1b16b31-image.png

    ==> This is why I want to draw with pixmap (to use setPixmap after in a specific label)


  • Lifetime Qt Champion

    Hi
    Im not sure i know what you are trying.
    Are you showing all your code as
    drawlabel does nothing with the pixmap ??

    So what are you trying ?
    You have thirdwindow with paint event that draws the board.

    Then you have drawlabel that should to what ?



  • yes,actually the drawlabel did nothing to the code so i add the code to the constructor of this window but i get the oposite of what i want, here is the code:
    thirdwindow::thirdwindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::thirdwindow)
    {
    ui->setupUi(this);
    int h = ui->label_pic->height();
    int w = ui->label_pic->width();

    QPixmap pix(w, h); // give pixmap some size
    QPainter painter(&pix); // assign painter to it. note the &
    pix.fill(Qt::white);
    ui->label_pic->setPixmap(pix);
    

    }


  • Lifetime Qt Champion

    @mayyy
    hi
    But that gives you exactly what picture shows.
    A big white rectangle the size of the label.

    What did you expect it to show ?



  • @mrjj instead of a white rectangle I want the chess board and the rest is empty (to add some extensions for the game )


  • Lifetime Qt Champion

    @mayyy
    Ahh. well you can still do that but you need the code from paint Event in drawLabel
    then and remove code in paintevent so it dont draw in background.

    void thirdwindow::makeBoard()
    {
    // Initialization
        int h = ui->label_pic->height();
        int w = ui->label_pic->width();
    
        QPixmap pix(w, h); // give pixmap some size
        QPainter painter(&pix); // assign painter to it. note the &
    
        unsigned int numCellX = 8, numCellY = 8;
        QRect wRect = rect();
        unsigned int cellSizeX = wRect.width() / numCellX;
        unsigned int cellSizeY = wRect.height() / numCellY;
        //QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
    
        // Draw the background. The whole widget is drawed.
        //painter.setBrush(QColor(0,0,0)); //black
        pix.fill( Qt::black);
    
        painter.drawRect(wRect);
    
        // Draw the cells which are of the other color
        // Note: the grid may not fit in the whole rect, because the size of the widget
        // should be a multiple of the size of the cells. If not, a black line at the right
        // and at the bottom may appear.
        //painter.setBrush(QColor(255,255,255));
        pix.fill( Qt::white );//white
        for (unsigned int j = 0; j < numCellY; j++)
            for (unsigned int i = j % 2; i < numCellX; i += 2)
                painter.drawRect(i * cellSizeX, j * cellSizeY, cellSizeX, cellSizeY);
        
      ui->label_pic->setPixmap(pix);  // set to label
    }
    
    

    and in constructor just call
    makeBoard();



  • @mrjj thank u but , this is what I get : the board is not drawn correctly
    1fa2c14e-ea63-40b2-bcb8-b23025e2ab59-image.png


  • Lifetime Qt Champion

    Hi
    Just small bug
    we say
    QRect wRect = rect(); but that gives us rect of the window
    so we need to say
    QRect wRect = ui->label_pic-rect();

    so we use the labels rect for calculations.



  • @mrjj This one is the closet to the goal ! ,but there is no black rect()
    8600443b-1592-48ab-87a1-cdc14455e94b-image.png


  • Lifetime Qt Champion

    @mayyy
    You call pix.fill and not
    painter.setBrush

    void MainWindow::makeBoard()
    {
    // Initialization
        int h = ui->label_pic->height();
        int w = ui->label_pic->width();
    
        QPixmap pix(w, h); // give pixmap some size
        QPainter painter(&pix); // assign painter to it. note the &
    
        unsigned int numCellX = 8, numCellY = 8;
        QRect wRect =  ui->label_pic->rect();
        unsigned int cellSizeX = wRect.width() / numCellX;
        unsigned int cellSizeY = wRect.height() / numCellY;
        //QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
    
        // Draw the background. The whole widget is drawed.
        //painter.setBrush(QColor(0,0,0)); //black
        pix.fill( Qt::black);
    
        painter.drawRect(wRect);
    
        // Draw the cells which are of the other color
        // Note: the grid may not fit in the whole rect, because the size of the widget
        // should be a multiple of the size of the cells. If not, a black line at the right
        // and at the bottom may appear.
    
        painter.setBrush(QColor(255,255,255)); // CHANGED
      // pix.fill( Qt::white );//white  THIS IS WRONG 
        for (unsigned int j = 0; j < numCellY; j++)
            for (unsigned int i = j % 2; i < numCellX; i += 2)
                painter.drawRect(i * cellSizeX, j * cellSizeY, cellSizeX, cellSizeY);
    
        ui->label_pic->setPixmap(pix);
    
    }
    

    alt text



  • @mrjj This is perfect ! thank u so much for ur help.


Log in to reply