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!

    file.cpp

    #include "interfazmatrizexperto.h"
    #include "ui_interfazmatrizexperto.h"
    using namespace std;
    
    interfazMatrizExperto::interfazMatrizExperto(const QString rutaImagenFondo, QWidget *parent) :
        QWidget(parent),
        ui(new Ui::interfazMatrizExperto)
    {
        ui->setupUi(this);
    
        //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);
        this->setPalette(palette);
    
        labelImage = new subQLabel(0);
        labelImage->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        QPixmap pix(rutaImagenFondo);
        int w = ui->labelImage->width();
        int h = ui->labelImage->height();
        ui->labelImage->setPixmap(pix.scaled(w,h,Qt::KeepAspectRatio));
    
    }
    
    interfazMatrizExperto::~interfazMatrizExperto()
    {
        delete ui;
    }
    
    void interfazMatrizExperto::mousePressEvent(QMouseEvent *event)
    {
    
        QWidget::mousePressEvent(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)
    {
    
    }
    
    subQLabel::~subQLabel()
    {
    
    
    }
    
    

    file .h

    /#ifndef INTERFAZMATRIZEXPERTO_H
    #define INTERFAZMATRIZEXPERTO_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
    {
        Q_OBJECT
    
    public:
        explicit interfazMatrizExperto(const QString rutaImagenFondo,QWidget *parent = 0);
        ~interfazMatrizExperto();
    protected:
        void mousePressEvent(QMouseEvent *event) override;
        void resizeEvent(QResizeEvent *evt);
    private:
        Ui::interfazMatrizExperto *ui;
        subQLabel *labelImage;
    };
    
    class subQLabel : public QLabel
    {
        Q_OBJECT
    
    public:
        subQLabel(QWidget* parent);
        ~subQLabel();
        void mousePressEvent(QMouseEvent *event) override;
    };
    
    #endif // INTERFAZMATRIZEXPERTO_H
    
    


  • @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.

    http://doc.qt.io/qt-5/qpixmap.html#copy



  • 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
 

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