class GridWidget : public QWidget
{
public:
GridWidget(QWidget *parent = nullptr) : QWidget(parent) {}
protected:
void paintEvent(QPaintEvent *event) override;
};
void GridWidget::paintEvent(QPaintEvent *event) // 重写paintEvent
{
Q_UNUSED(event);
QPainter painter(this);
QPen pen(Qt::red,2);
painter.setPen(pen);
painter.drawLine(213, 0, 213, 480);
painter.drawLine(426, 0, 426, 480);
painter.drawLine(0, 160, 640, 160);
painter.drawLine(0, 320, 640, 320);
}
void MainWindow::on_DrawLine_clicked() // 绘制九宫格
{
GridWidget* grid = new GridWidget(Widget); // Widget
grid->resize(Widget->size());
grid->show();
grid->update();
}
void MainWindow::on_HideLine_clicked() // 去掉九宫格
{
if(grid != nullptr)
{
delete grid;
grid = nullptr;
}
}
Qt实现各种Widget上绘图
于 2024-05-17 18:00:24 首次发布