Drawing in QT
-
Hi All,
I'm trying to understand how to draw in QT. I have a graph which looks like this:
I need to draw this in qt and control slope of the trapezoid with a QSlider.
It will be really helpful if someone can show me this with a simple example.
Thanks in advance
-
Hi
Which part do you need help with ?
You can use QPainter and
https://doc.qt.io/qt-5/qtwidgets-painting-painterpaths-example.html
for the trapezoidIf i read here
https://stackoverflow.com/questions/59373961/draw-and-control-rectangle-in-qtIt seems you want the slider to adjust the 2 top points to be the same value according to a slider.
You can do this by updating the points for the path and call update()
-
Hi
very basic example. I draw on pixmap and show in label.
Its just to show as a concept. A custom control might be better.void MainWindow::on_verticalSlider_valueChanged(int value) { int h = ui->label->height(); int w = ui->label->width(); QPixmap pix(w, h); QPainter paint(&pix); pix.fill( Qt::white ); paint.setPen(QColor(0, 0, 0, 255)); QPainterPath polygonPath; polygonPath.moveTo(30.0, h); int topy = h - 100 - value; polygonPath.lineTo(60.0, topy); polygonPath.lineTo(160.0, topy ); polygonPath.lineTo(260.0, h); polygonPath.closeSubpath(); paint.drawPath(polygonPath); ui->label->setPixmap(pix); }
test project
https://www.dropbox.com/s/cctwcsadkhq5x2m/trapezoidTest.zip?dl=0 -
I need to show the Polygon path while the MainWindow is constructed with a default value and then it should change if I drag a Slider.
In the example, the figure is constructed underon_verticalSlider_valueChanged
slot itself , so I wont see anything until I move the Slider.If I move the polygon painting part under the MainWindow constructor, plot does not look right:
//this gives totally wrong figure MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); int h = ui->label->height(); int w = ui->label->width(); QPixmap pix(w, h); QPainter paint(&pix); pix.fill( Qt::white ); paint.setPen(QColor(0, 0, 0, 255)); polygonPath.moveTo(30.0, h); int topy = h - 100 ; polygonPath.lineTo(60.0, topy); polygonPath.lineTo(160.0, topy ); polygonPath.lineTo(260.0, h); polygonPath.closeSubpath(); paint.drawPath(polygonPath); ui->label->setPixmap(pix); }
basically i need to have a default figure with
int topy = h - 100
then if I change Sliderint topy = h - 100 - value
. What's the best way to implement this? -
How do I overwrite showEvent function, should I call showEvent in the constructor?
I tried this:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QWidget::showEvent(parent); on_verticalSlider_valueChanged(100); }
this gives error:
mainwindow.cpp:11:24: error: cannot initialize a parameter of type 'QShowEvent *' with an lvalue of type 'QWidget *' qwidget.h:646:40: note: passing argument to parameter 'event' here
-
@viniltc said in Drawing in QT:
How do I overwrite showEvent function
Like any other:
void MainWindow::showEvent(QShowEvent *event) { QWidget::showEvent(event); }
https://www.programiz.com/cpp-programming/function-overriding
-
@viniltc
Hi
No, overwrite means to add that function to your class so
its called instead of the base class.void MainWindow:showEvent(QShowEvent *event) { QMainWindow::showEvent(event); // call base so we dont fux things up on_verticalSlider_valueChanged(100); }