How to create a line between 2 points with QLabel?
-
wrote on 7 May 2019, 13:58 last edited by frnklu20 5 Jul 2019, 14:03
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 ? -
wrote on 7 May 2019, 14:05 last edited by
yes! instead of using QPainter, represent this line as a QLabel
-
Lifetime Qt Championwrote on 7 May 2019, 14:07 last edited by mrjj 5 Jul 2019, 14:11
@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. -
wrote on 7 May 2019, 14:20 last edited by
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 -
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 ?
-
wrote on 7 May 2019, 14:32 last edited by
-
It's not pretty yet, but that's what my program does
I add objects on the screen and draw lines between thenbut this line has to be treated as an object as well, when i click in any point of the line it should open a window with its data
Lifetime Qt Championwrote on 7 May 2019, 14:36 last edited by mrjj 5 Jul 2019, 14:37@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 ?
-
wrote on 10 May 2019, 19:38 last edited by
Exactly!
Graphics View Framework would help me a lot, i guess
Do you know some book or site that i can learn more about it?with some examples and everything
-
Did you check the Qt documentation and related examples ?
-
wrote on 13 May 2019, 19:31 last edited by
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 ? -
wrote on 13 May 2019, 19:57 last edited by
no, how can i do this?
-
Well, use designer as explained here.
1/15