QT Draw Line Code
- User
QT Draw Line Code
- Assistant
In QT, drawing a line can be done using the QPainter class. This class provides functions to draw a variety of shapes, including lines. Here's an example of how you can create a custom QWidget subclass that draws a line:
#include <QWidget> #include <QPainter> class LineWidget : public QWidget { public: LineWidget(QWidget *parent = nullptr) : QWidget(parent) {} protected: void paintEvent(QPaintEvent *event) override { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); QPen pen(Qt::black, 2); painter.setPen(pen); painter.drawLine(QPointF(40.0, 40.0), QPointF(200.0, 200.0)); } };To use this custom widget in your application, you would add it to your main window or another layout, like this:
#include <QApplication> #include <QMainWindow> int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow mainWindow; LineWidget *lineWidget = new LineWidget(); mainWindow.setCentralWidget(lineWidget); mainWindow.show(); return app.exec(); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.