如何在 Qt5 的 QImage 上画一个点

How to draw a point on top of a QImage for Qt5

我是 Qt 的新手,我完全被困在这一点上。我唯一的选择是求助于专家。我设计了一个 GUI,可以将图像加载到 QMainWindow 上的 Qlabel 中。然后我想在图像本身上绘制多个点。但是,似乎正在发生的事情是在图像后面绘制点,我不知道如何在图像顶部绘制点。

我的代码:

main.cpp

int main(int argc, char *argv[])
{
   QApplication a(argc, argv);
   MainWindow w;
   w.show();

   return a.exec();
} 

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMouseEvent>

//Declaring the external Variables
extern QString name;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:

void on_actionOpen_triggered();
void on_actionDraw_2_triggered();
void on_actionCreate_2_triggered();
void mousePressEvent(QMouseEvent* event);
void paintEvent(QPaintEvent* e);

private:
Ui::MainWindow *ui;


};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "createdialog.h"
#include <QMouseEvent>
#include <qpainter.h>
#include <QFileDialog>
#include <QLabel>
#include <Qimage>
#include <QPaintDevice>
#include <QDebug>
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/system/config.hpp>
#include "iostream"

using namespace std;

QString fileName;
QString name;
int drawFlag=0;
int paintFlag=0;
int xCord = 0;      //mouse click x location
int yCord = 0;      //mouse click y loaction

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
 ui->setupUi(this);
}

MainWindow::~MainWindow()
{
  delete ui;
}

void MainWindow::on_actionOpen_triggered()
{
//Opening a dialog box to search for the images
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                   "C:/", tr("Images (*.png *.xpm *.jpg)"));

//Pulling just the file name out of the file path
    std::string filename = fileName.toStdString();
    boost::filesystem::path p(filename);
    name = QString::fromStdString(p.stem().string());

//Checking to see if the file name is empty
if (!fileName.isEmpty()) {
    QImage img(fileName);
    int width = img.width();
    int height = img.height();
    MainWindow::showMaximized();
    ui->label->setGeometry(0,0,width,height);       //Setting the label to the size of the image
    ui->label->setPixmap(QPixmap::fromImage(img));  //Inserting the image into the label

}
    else{
       ui->label->setText("Error");
}

}

void MainWindow::mousePressEvent(QMouseEvent* event)
 {
  //If the draw flag is set record mouse press events
  if(drawFlag==1){
    if(event->button() == Qt::LeftButton){
        xCord = event->x();
        yCord = event->y();
        std::cout << "x-location: " << xCord << endl;
        std::cout << "y-location: " << yCord << endl;
        paintFlag=1;
        update();

    }
  }
}
void MainWindow::paintEvent(QPaintEvent* e)
{
  QMainWindow::paintEvent(e);
  //When the paint flag is set then paint
  if(paintFlag==1){
    QPainter painter(this);
    QPen paintpen(Qt::red);
    paintpen.setWidth(10);
    QPoint p1;
    p1.setX(xCord);
    p1.setY(yCord);
    painter.setPen(paintpen);
    painter.drawPoint(p1);
  }
}

void MainWindow::on_actionDraw_2_triggered()
{
drawFlag=1;

}

非常感谢任何建议。谢谢!

主要 window 在其子窗口小部件绘制之前绘制。绘制主体window其实就是绘制灰色背景

这意味着您的 MainWindow::paintEvent() 调用基础 class 实现 (QMainWindow::paintEvent()) 来绘制灰色背景,然后在灰色背景之上绘制一个点。在 ``paintEvent()` returns 之后,Qt 会在其上绘制所有子部件,包括标签,从而绘制在灰色背景和您的点上。

可能的解决方案:

  1. 在调用 QLabel::setPixmap() 之前,在 QImage 上绘制点 - QPainter 也可以在 QImage 上绘制,而不仅仅是在 QWidget.

QImage image("path/to/image/img.png");
QPainter painter(&image);
QPen pen;
pen.setWidth(20);
pen.setColor(Qt::red);
painter.setPen(pen);
painter.drawPoint(5,5);
painter.end();
label->setPixmap(QPixmap::fromImage(image));

  1. Subclass QLabel(将其命名为 LabelWithPointOnTop)并在您的 subclass 中覆盖 paintEvent(),然后放置您的 subclass 的实例而不是 QLabel 到您的布局中。