Control to hold an image and getting X,Y in that control
-
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
-
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.
-
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) -
So you want to do something like edge detection ?
-
@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?
-
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.
-
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
#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?
-
@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.