用qt编写一段代码,要求如下:1、程序运行时默认画一个五角星,点击按钮1画一个矩形,点击按钮2画一个圆形。2、按钮需连接信号和槽函数,分别对应画矩形和圆形。3、每画一个图形时上一个图形将被覆盖。
时间: 2023-09-08 19:05:28 浏览: 120
### 回答1:
以下是 Qt 代码:#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QPainter>class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);protected:
void paintEvent(QPaintEvent *);private slots:
void drawRect();
void drawCircle();private:
QPushButton *rectButton;
QPushButton *circleButton;
};MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
rectButton = new QPushButton(tr("画矩形"), this);
circleButton = new QPushButton(tr("画圆形"), this); connect(rectButton, SIGNAL(clicked()), this, SLOT(drawRect()));
connect(circleButton, SIGNAL(clicked()), this, SLOT(drawCircle())); rectButton->setGeometry(10, 10, 70, 30);
circleButton->setGeometry(90, 10, 70, 30);
}void MyWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawEllipse(50, 50, 50, 50);
}void MyWidget::drawRect()
{
QPainter painter(this);
painter.drawRect(20, 20, 100, 50);
}void MyWidget::drawCircle()
{
QPainter painter(this);
painter.drawEllipse(20, 20, 100, 50);
}int main(int argc, char **argv)
{
QApplication app(argc, argv); MyWidget window;
window.show(); return app.exec();
}
### 回答2:
下面是一个使用Qt编写的满足要求的代码示例:
```cpp
#include <QtWidgets>
class DrawingWidget : public QWidget
{
public:
DrawingWidget(QWidget *parent = nullptr) : QWidget(parent), shape(Shape::Star) {}
enum class Shape { Star, Rectangle, Circle };
protected:
void paintEvent(QPaintEvent *) override
{
QPainter painter(this);
if (shape == Shape::Star)
{
drawStar(painter);
}
else if (shape == Shape::Rectangle)
{
drawRectangle(painter);
}
else if (shape == Shape::Circle)
{
drawCircle(painter);
}
}
private:
void drawStar(QPainter &painter)
{
QPolygonF starPolygon;
starPolygon << QPointF(90, 50) << QPointF(109, 67) << QPointF(127, 50) << QPointF(109, 32)
<< QPointF(127, 9) << QPointF(109, 25) << QPointF(90, 9) << QPointF(72, 25)
<< QPointF(55, 9) << QPointF(72, 32);
painter.drawPolygon(starPolygon);
}
void drawRectangle(QPainter &painter)
{
painter.drawRect(50, 50, 100, 50);
}
void drawCircle(QPainter &painter)
{
painter.drawEllipse(75, 50, 50, 50);
}
Shape shape;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
DrawingWidget *drawingWidget = new DrawingWidget(&window);
window.setCentralWidget(drawingWidget);
QPushButton *rectangleButton = new QPushButton("画矩形", &window);
QObject::connect(rectangleButton, &QPushButton::clicked, [&]() {
drawingWidget->shape = DrawingWidget::Shape::Rectangle;
drawingWidget->update();
});
QPushButton *circleButton = new QPushButton("画圆形", &window);
QObject::connect(circleButton, &QPushButton::clicked, [&]() {
drawingWidget->shape = DrawingWidget::Shape::Circle;
drawingWidget->update();
});
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(rectangleButton);
buttonLayout->addWidget(circleButton);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(drawingWidget);
mainLayout->addLayout(buttonLayout);
QWidget *centralWidget = new QWidget(&window);
centralWidget->setLayout(mainLayout);
window.setCentralWidget(centralWidget);
window.show();
return app.exec();
}
```
解释:
上述代码定义了一个继承自`QWidget`的`DrawingWidget`类,用于绘制不同形状的图形。在`paintEvent`函数中根据当前选择的形状绘制相应的图形。`Shape`是一个枚举类型,用于表示当前要绘制的形状。
在`main`函数中,创建了一个`QMainWindow`窗口,并将`DrawingWidget`作为中央部件。同时创建了两个按钮`rectangleButton`和`circleButton`,并分别与对应的槽函数连接,点击按钮后更新`DrawingWidget`的形状,并重新绘制。最后,创建了布局管理器,将`DrawingWidget`和按钮添加到布局中,并设置为窗口的中央部件。
运行程序,初始情况下会显示一个五角星。点击"画矩形"按钮后,会在窗口中央位置绘制一个矩形,原先的五角星会被覆盖。同理,点击"画圆形"按钮后,矩形会被覆盖,绘制一个圆形。每次只能绘制一个图形,上一个图形会被新的图形覆盖。
### 回答3:
使用Qt编写这段代码可以实现上述要求:
```cpp
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QPainter>
#include <QPen>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = nullptr) : QWidget(parent)
{
setFixedSize(400, 400); // 设置窗口大小
// 创建按钮1
QPushButton *button1 = new QPushButton("画矩形", this);
button1->setGeometry(10, 10, 80, 30);
// 创建按钮2
QPushButton *button2 = new QPushButton("画圆形", this);
button2->setGeometry(10, 50, 80, 30);
connect(button1, &QPushButton::clicked, this, &MyWidget::drawRectangle); // 连接按钮1的信号和槽函数
connect(button2, &QPushButton::clicked, this, &MyWidget::drawCircle); // 连接按钮2的信号和槽函数
}
protected:
void paintEvent(QPaintEvent *event) override
{
QPainter painter(this);
QPen pen(Qt::black, 2); // 使用黑色画笔,线宽为2
painter.setPen(pen);
// 画五角星
painter.drawLine(200, 50, 215, 90);
painter.drawLine(215, 90, 245, 90);
painter.drawLine(245, 90, 223, 115);
painter.drawLine(223, 115, 235, 155);
painter.drawLine(235, 155, 200, 130);
painter.drawLine(200, 130, 165, 155);
painter.drawLine(165, 155, 177, 115);
painter.drawLine(177, 115, 155, 90);
painter.drawLine(155, 90, 185, 90);
painter.drawLine(185, 90, 200, 50);
// 绘制当前图形
painter.drawRect(rectangle); // 绘制矩形
painter.drawEllipse(circle); // 绘制圆形
}
private slots:
void drawRectangle()
{
rectangle = QRect(100, 100, 100, 50);
update(); // 更新窗口,触发重绘
}
void drawCircle()
{
circle = QRect(250, 150, 100, 100);
update(); // 更新窗口,触发重绘
}
private:
QRect rectangle; // 矩形的位置和大小
QRect circle; // 圆形的位置和大小
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget widget;
widget.show();
return a.exec();
}
```
在这段代码中,我们创建了一个自定义的QWidget类MyWidget,重写了其paintEvent函数实现图形的绘制。MyWidget中含有两个按钮,分别对应画矩形和圆形。当点击按钮时,会触发对应的槽函数drawRectangle和drawCircle,分别更新矩形和圆形的位置和大小,并调用update函数触发重绘。绘制时,先画出默认的五角星,并根据矩形和圆形的位置和大小绘制出对应的图形。每次绘制图形都会覆盖上一个图形。
阅读全文