Important: Please read the Qt Code of Conduct -

Split picture of QLabel subclass in squares.

  • Hello! I have a subclass of QLabel that i implemented to use the MouseEvents. Now, i am showing a picture in that label but i need to split the pictures in how much squares users wants. After that, i will need to write in the square a number. For example, if i click in one of that squares, in the first click i will write '1' in that square, with the second click, i will write '2' in that square..

    But i don' know how i can do that, i put my code here! thanks!


    #include "interfazmatrizexperto.h"
    #include "ui_interfazmatrizexperto.h"
    using namespace std;
    interfazMatrizExperto::interfazMatrizExperto(const QString rutaImagenFondo, QWidget *parent) :
        ui(new Ui::interfazMatrizExperto)
        //Imagen de fondo
        QPixmap bkgnd("../InterfazKinect/img/foto_index_sinTXT.png");
        bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
        QPalette palette;
        palette.setBrush(QPalette::Background, bkgnd);
        labelImage = new subQLabel(0);
        labelImage->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        QPixmap pix(rutaImagenFondo);
        int w = ui->labelImage->width();
        int h = ui->labelImage->height();
        delete ui;
    void interfazMatrizExperto::mousePressEvent(QMouseEvent *event)
    void interfazMatrizExperto::resizeEvent(QResizeEvent *evt)
        QPixmap bkgnd("../InterfazKinect/img/foto_index_sinTXT.png");//Load pic
        bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio);//set scale of pic to match the window
        QPalette p = palette(); //copy current, not create new
        p.setBrush(QPalette::Background, bkgnd);//set the pic to the background
        setPalette(p);//show the background pic
        QWidget::resizeEvent(evt); //call base implementation
    void subQLabel::mousePressEvent(QMouseEvent *event)
        cout << "Ha pulsado" << endl;
        int x = event->x();
        int y = event->y();
        cout << "punto x: " << x << " punto y: " << y << endl;
    subQLabel::subQLabel(QWidget* parent) : QLabel(parent)

    file .h

    #include <QWidget>
    #include <QMouseEvent>
    #include <QImage>
    #include <QLabel>
    #include <QScrollArea>
    #include <iostream>
    #include <QPixmap>
    #include <QVBoxLayout>
    class subQLabel;
    namespace Ui {
    class interfazMatrizExperto;
    class interfazMatrizExperto : public QWidget
        explicit interfazMatrizExperto(const QString rutaImagenFondo,QWidget *parent = 0);
        void mousePressEvent(QMouseEvent *event) override;
        void resizeEvent(QResizeEvent *evt);
        Ui::interfazMatrizExperto *ui;
        subQLabel *labelImage;
    class subQLabel : public QLabel
        subQLabel(QWidget* parent);
        void mousePressEvent(QMouseEvent *event) override;

  • @AdrianCruz

    Maybe you can reimplement the paintEvent of SubLabel and get the pixelmap of label (if exists) to create cutted pixmaps using copy function of QPixmap class.
    Increment a integer variable for each time someone clicked in the label and calculate how many cuts are necessary and draw a text of the number of this cutted pixmap.
    Finally, you can redraw the image of background using these cutted pixmaps with orderned numbers to draw the new background pixmap.

  • QLabel really isn't the right tool for the job. Feels like you want the Graphics View Framework

  • @VRonin How can i do that? Because i have never used QGraphicWiew and seem hard to do :S

Log in to reply