[英]MouseEvent coordinates on Qlabel with Image
so i dont know how to add a mouseevent ( if i click the label with image selected to add x,y coordinates of the mouse on the tableWidget and the coordinates to be drawed on the image ( green/red dots)所以我不知道如何添加鼠标事件(如果我单击带有所选图像的标签以在 tableWidget 上添加鼠标的 x、y 坐标以及要在图像上绘制的坐标(绿/红点)
for now i only can open a ascii file with coordinates and image.现在我只能打开一个带有坐标和图像的 ascii 文件。 i need your help with mouseevent and draw the points on the image我需要你的鼠标事件帮助并在图像上绘制点
void design::on_loadtext1_clicked() {
ui->merge->setText("merge");
QString filename=QFileDialog::getOpenFileName(
this,
tr("Open File"),
"C://",
"Text File(*.txt)"
);
QFile file(filename);
if(!file.open(QIODevice::ReadOnly)) { QMessageBox::information(nullptr,"Info",file.errorString());
}
QTextStream in(&file);
double x = 0.0;
double y = 0.0;
// double xn = 532;
int row=0;
QString line;
ui->tableWidget->setRowCount(30);
ui->tableWidget->setColumnCount(2);
while(!in.atEnd()) {
line = in.readLine();
QStringList s = line.split(" ");
x = s.at(0).toDouble();
y = s.at(1).toDouble();
ui->tableWidget->setItem(row, 0, new TableItem(tr("%1").arg(x)));
ui->tableWidget->setItem(row, 1, new TableItem(tr("%1").arg(y)));
row++;
} }
If you want to draw an ellipse on QLabel at mouse click position, this code should get you started:如果你想在鼠标点击位置在 QLabel 上绘制一个椭圆,这个代码应该让你开始:
First, as proposed, subclass QLabel
:首先,按照提议,子类QLabel
:
.h file .h文件
#include <QLabel>
#include <QPainter>
#include <QMouseEvent>
class MyLabel : public QLabel
{
public:
MyLabel(QWidget* parent=nullptr);
void mousePressEvent(QMouseEvent * e);
private:
QPixmap *pix;
};
Then create pixmap
in constructor and implement drawing on mouseClickEvent
:然后在构造函数中创建pixmap
并在mouseClickEvent
上实现绘图:
.cpp file .cpp文件
MyLabel::MyLabel(QWidget *parent) :
QLabel(parent)
{
pix = new QPixmap(200,200);
pix->fill(Qt::white);
setPixmap(*pix);
}
void MyLabel::mousePressEvent(QMouseEvent *e)
{
int xPos, yPos;
if(e->button() == Qt::LeftButton)
{
xPos = e->pos().x();
yPos = e->pos().y();
}
QPainter painter(pix);
painter.setPen(Qt::black);
painter.drawEllipse(xPos, yPos, 3, 3);
setPixmap(*pix);
QLabel::mousePressEvent(e);
}
Then just create an instance of MyLabel
in MainWindow
:然后只需在MainWindow
创建一个MyLabel
实例:
label = new MyLabel(this);
label->setGeometry(0,0,200,200);
Don't forget declaration in mainwindow.h
:不要忘记mainwindow.h
声明:
MyLabel *label;
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.