我对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;
}
非常感谢任何建议。谢谢!
答案 0 :(得分:4)
在绘制子窗口小部件之前绘制主窗口。绘制主窗口实际上只是绘制灰色背景。
这意味着您的MainWindow::paintEvent()
调用基类实现(QMainWindow::paintEvent()
)来绘制灰色背景,然后在灰色背景上绘制一个点。在``paintEvent()`返回之后,Qt会在其上绘制所有子窗口小部件,包括标签,从而绘制灰色背景和点。
潜在的解决方案:
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));
QLabel
(将其命名为LabelWithPointOnTop)并覆盖子类中的paintEvent()
,然后将子类的实例而不是QLabel
放入布局中。