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. Split picture of QLabel subclass in squares.
QtWS25 Last Chance

Split picture of QLabel subclass in squares.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 681 Views
  • 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.
  • D Offline
    D Offline
    deleted396
    wrote on last edited by
    #1

    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
    
    
    1 Reply Last reply
    0
    • KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by
      #2

      @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

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        D 1 Reply Last reply
        2
        • VRoninV VRonin

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

          D Offline
          D Offline
          deleted396
          wrote on last edited by
          #4

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

          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