How to create a line between 2 points with QLabel?
-
With the program that i'm developing i can draw a line between 2 points with QPainter, but now i need to do some stuff with this line that it has to be a label
But how can i do it?
For example:QLabel *child = new QLabel(this); child->setPixmap(QPixmap(url_linhal));##it is an image that i made on paint that is all black, to act like a line made from QPainter QRect rect(10,20,100,20); child->setGeometry(rect); child->show();
but there is a problem, with this code appears a rect between 2 points not a line, so how can i do this?
i know that it is a crazy ideia, and i wanna know if there is a way to do it
-
Hi
Just to understand.
You draw a line with QPainter and you want to show this line as pixmap on
a QLabel ? -
@frnklu20
Well QLabel can show a pixmap. It can not show line by itself.
So the idea to draw on pixmap and show in label is fine enough.
you can do that directlyint h =100; int w = 100; QPixmap pix(w, h); QPainter paint(&pix); pix.fill( Qt::white ); paint.setPen(QColor(0, 0, 0, 255)); paint.drawLine(xxx) ui->label->setPixmap(pix);
But what are you actually doing?
Maybe a custom widget would work far better. -
I guess a custom widget will be better, but i don't have any ideia to do that
I want is to deal with the line as an object and to be clickable, i want to click in any part of the line and my program recognizes it.
What i thought was to use a CustomLabel class that it emits a signal when it is clicked and create an obect of this class between 2 points.
So the way you said before, i create a line inside my QLabel but it becomes to big for being a rect -
@frnklu20
Well the image has to be as big as the points
like 0,0 and 100,100 , the pixmap must be 100x100
But if you want to click on it, an image is not so good as it
has no idea where line is. so it needs the point anyway.Even with a custom widget, it must still be as big as the span between the points.
So what should happen when you click the line at any part of it ?
-
@frnklu20
well you need to store the 2 points for each line and
then use math to find out if the user hits a line on mousePressed.
How is the lines stored now ? ( the points you use to draw it )This explains it well
http://www.jeffreythompson.org/collision-detection/line-point.php -
Hi,
Since you seem to want to interact and have things working dynamically, did you consider the Graphics View Framework ?
-
Did you check the Qt documentation and related examples ?
-
Yes!
But i'm having a problem.I saw in a video a guy using the the graphic view framework in a QDialog
That's the code:
dialog.h
class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = nullptr); ~Dialog(); private: Ui::Dialog *ui; QGraphicsScene *scene; QGraphicsEllipseItem *ellipse; QGraphicsRectItem *rectangle; };
dialog.cpp
QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); scene= new QGraphicsScene(this); ui->graphicsView->setScene(scene);////the problem is here! QBrush redBrush(Qt::red); QPen blackpen(Qt::black); ellipse=scene->addEllipse(10,10,100,100,blackpen,redBrush); }
It says that no member named 'graphicView' in Ui::Dialog, so how can i fix it?
i want to use the application in qwidget or qdialog class -
Did you add a QGraphicsView widget name
graphicsView
through designer to your dialog ?