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. Control to hold an image and getting X,Y in that control
Forum Updated to NodeBB v4.3 + New Features

Control to hold an image and getting X,Y in that control

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 522 Views 2 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.
  • E Offline
    E Offline
    EverydayDiesel
    wrote on last edited by EverydayDiesel
    #1

    Hello, Is there a control designated to hold an image in QT?

    I started using an QLabel but I am not sure this is the right control.

    This is basically how I draw the image
    ```
    QPixmap pm1(sFilePath);
    ui->label->setPixmap(pm1);
    ui->label->adjustSize();

    
    I need the ability to draw horizontal/verticle lines based on where the user clicks.
    
    Is QLabel the best control for this and how do I capture X, Y within that image?
    
    Thanks in advance
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It depends on what you want to do. With QLabel you already have the pixmap handling part and you just how to manage the painting of your lines.

      Please give more details about what that widget shall be used for.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        EverydayDiesel
        wrote on last edited by
        #3

        Hello and thank you for the reply.

        It will basically be chopping a larger image into multiple smaller images.

        I will need to evaluate the color the current pixel to determine if it is white (ish) it will be one of these values
        rgb(254, 254, 254)
        rgb(255, 255, 254)
        rgb(254, 255, 255)
        rgb(255, 254, 255)
        rgb(255, 254, 254)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So you want to do something like edge detection ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          E 1 Reply Last reply
          0
          • SGaistS SGaist

            So you want to do something like edge detection ?

            E Offline
            E Offline
            EverydayDiesel
            wrote on last edited by
            #5

            @SGaist yes.

            The user will click on a general area and it will scan the horizontal pixels until it finds a dark pixel.

            Is there a control that I can draw an image on, get the X,Y positions, and get the pixel RGB value based on a X,Y cordinate?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You can do that with every widget. Subclass the mouse related methods.

              Depending on what you want to do, you might want to consider using a library like OpenCV for your image processing.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • E Offline
                E Offline
                EverydayDiesel
                wrote on last edited by EverydayDiesel
                #7

                Please excuse my ignorance but I am new to QT (coming from wxwidgets)

                // MainWindow.h

                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H
                
                #include <QMainWindow>
                
                #include <string>
                
                using namespace std;
                
                QT_BEGIN_NAMESPACE
                namespace Ui { class MainWindow; }
                QT_END_NAMESPACE
                
                class MainWindow : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    MainWindow(QWidget *parent = nullptr);
                    ~MainWindow();
                
                private slots:
                
                private:
                    Ui::MainWindow *ui;
                };
                #endif // MAINWINDOW_H
                

                //MainWindow.cpp

                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                
                class MyExtendedQLabel : public QLabel, private Ui::MainWindow::MyExtendedQLabel
                {
                protected:
                    void mousePressEvent(QMouseEvent *event);
                };
                
                MainWindow::MainWindow(QWidget *parent)
                    : QMainWindow(parent)
                    , ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                }
                
                MainWindow::~MainWindow()
                {
                    delete ui;
                }
                
                void MainWindow::on_cmdLoadImage_clicked()
                {
                    QString sFilePath = "001.png";
                    QPixmap pm1(sFilePath);
                    ui->label->setPixmap(pm1);
                    ui->label->adjustSize();
                }
                

                I am a bit confused on how to implement this. How do I take the class MyExtendedQLabel and extract the X, Y coordinates to the main window? What connects MyExtendedQLabel to the QLabel (label) on the MainWindow?

                jsulmJ 1 Reply Last reply
                0
                • E EverydayDiesel

                  Please excuse my ignorance but I am new to QT (coming from wxwidgets)

                  // MainWindow.h

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QMainWindow>
                  
                  #include <string>
                  
                  using namespace std;
                  
                  QT_BEGIN_NAMESPACE
                  namespace Ui { class MainWindow; }
                  QT_END_NAMESPACE
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      MainWindow(QWidget *parent = nullptr);
                      ~MainWindow();
                  
                  private slots:
                  
                  private:
                      Ui::MainWindow *ui;
                  };
                  #endif // MAINWINDOW_H
                  

                  //MainWindow.cpp

                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  
                  class MyExtendedQLabel : public QLabel, private Ui::MainWindow::MyExtendedQLabel
                  {
                  protected:
                      void mousePressEvent(QMouseEvent *event);
                  };
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                      , ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                  }
                  
                  MainWindow::~MainWindow()
                  {
                      delete ui;
                  }
                  
                  void MainWindow::on_cmdLoadImage_clicked()
                  {
                      QString sFilePath = "001.png";
                      QPixmap pm1(sFilePath);
                      ui->label->setPixmap(pm1);
                      ui->label->adjustSize();
                  }
                  

                  I am a bit confused on how to implement this. How do I take the class MyExtendedQLabel and extract the X, Y coordinates to the main window? What connects MyExtendedQLabel to the QLabel (label) on the MainWindow?

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @EverydayDiesel said in Control to hold an image and getting X,Y in that control:

                  What connects MyExtendedQLabel to the QLabel (label) on the MainWindow?

                  Well you create an instance of MyExtendedQLabel and put it on your main window like any other widget. If you're using Qt Designer then first put a normal QLabel and then right-click on it and select "Promote to..." to promote QLabel to MyExtendedQLabel.

                  To get x/y coordinates use mouse events like @SGaist suggested: override https://doc.qt.io/qt-5/qwidget.html#mouseMoveEvent to get x/y while mouse is moving, https://doc.qt.io/qt-5/qwidget.html#mousePressEvent to get x/y when user presses mouse button.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2

                  • Login

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