利用spinbox,checkbox, radiobutton, slider 和 dial控件实现一个简单的绘图程序 用鼠标按下拖动来画图。 画笔颜色由3个slider决定rgb,线条宽度由spin控件决定, 画刷颜色由3个dial控件决定rgb。Qtcreator和c++实现
时间: 2024-03-18 10:38:32 浏览: 74
好的,这是一个比较复杂的问题,需要用到多个控件和一些绘图的知识,我会尽力回答。
首先,我们需要在 Qt Creator 中创建一个新的 Qt Widgets Application 项目,并添加以下控件:
1. QSpinBox:用于设置线条宽度。
2. QCheckBox:用于选择是否填充图形。
3. QRadioButton:用于选择绘图工具,例如画笔、直线、矩形、椭圆等。
4. QSlider:用于设置画笔颜色的 RGB 值。
5. QDial:用于设置画刷颜色的 RGB 值。
接下来,我们需要在主窗口的绘图区域中实现绘图功能。可以通过在主窗口类中重写 paintEvent() 函数来实现。在这个函数中,我们可以根据用户选择的绘图工具和参数来绘制图形。
下面是一个简单的实现示例代码:
```cpp
#include <QtWidgets>
class DrawingBoard : public QWidget
{
public:
DrawingBoard(QWidget *parent = nullptr)
: QWidget(parent)
{
setFixedSize(500, 500);
}
protected:
void paintEvent(QPaintEvent *event) override
{
QPainter painter(this);
if (m_drawing) {
painter.setPen(QPen(m_penColor, m_penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.setBrush(m_brushColor);
switch (m_tool) {
case Tool::Pencil:
painter.drawLine(m_lastPos, m_currentPos);
break;
case Tool::Line:
painter.drawLine(m_startPos, m_endPos);
break;
case Tool::Rectangle:
painter.drawRect(QRect(m_startPos, m_endPos));
break;
case Tool::Ellipse:
painter.drawEllipse(QRect(m_startPos, m_endPos));
break;
}
}
}
void mousePressEvent(QMouseEvent *event) override
{
m_drawing = true;
m_lastPos = event->pos();
m_startPos = m_lastPos;
}
void mouseMoveEvent(QMouseEvent *event) override
{
m_currentPos = event->pos();
m_endPos = m_currentPos;
update();
m_lastPos = m_currentPos;
}
void mouseReleaseEvent(QMouseEvent *event) override
{
m_drawing = false;
m_endPos = event->pos();
update();
}
private:
enum class Tool {
Pencil,
Line,
Rectangle,
Ellipse
};
Tool m_tool = Tool::Pencil;
bool m_drawing = false;
QPoint m_lastPos;
QPoint m_startPos;
QPoint m_currentPos;
QPoint m_endPos;
QColor m_penColor = Qt::black;
int m_penWidth = 1;
QColor m_brushColor = Qt::transparent;
};
class MainWindow : public QMainWindow
{
public:
MainWindow(QWidget *parent = nullptr)
: QMainWindow(parent)
{
setCentralWidget(&m_drawingBoard);
QDockWidget *dock = new QDockWidget(tr("Settings"), this);
addDockWidget(Qt::RightDockWidgetArea, dock);
QWidget *widget = new QWidget(dock);
dock->setWidget(widget);
QVBoxLayout *layout = new QVBoxLayout(widget);
widget->setLayout(layout);
// Line width
QLabel *label = new QLabel(tr("Line width:"), this);
layout->addWidget(label);
QSpinBox *spinBox = new QSpinBox(this);
spinBox->setRange(1, 10);
spinBox->setValue(1);
layout->addWidget(spinBox);
connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged), [&](int value) {
m_drawingBoard.setPenWidth(value);
});
// Fill shape
QCheckBox *checkBox = new QCheckBox(tr("Fill shape"), this);
layout->addWidget(checkBox);
connect(checkBox, &QCheckBox::toggled, [&](bool checked) {
m_drawingBoard.setFillShape(checked);
});
// Tools
label = new QLabel(tr("Tools:"), this);
layout->addWidget(label);
QButtonGroup *buttonGroup = new QButtonGroup(this);
QRadioButton *radioButton = new QRadioButton(tr("Pencil"), this);
layout->addWidget(radioButton);
buttonGroup->addButton(radioButton, static_cast<int>(Tool::Pencil));
radioButton = new QRadioButton(tr("Line"), this);
layout->addWidget(radioButton);
buttonGroup->addButton(radioButton, static_cast<int>(Tool::Line));
radioButton = new QRadioButton(tr("Rectangle"), this);
layout->addWidget(radioButton);
buttonGroup->addButton(radioButton, static_cast<int>(Tool::Rectangle));
radioButton = new QRadioButton(tr("Ellipse"), this);
layout->addWidget(radioButton);
buttonGroup->addButton(radioButton, static_cast<int>(Tool::Ellipse));
connect(buttonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked), [&](int id) {
m_drawingBoard.setTool(static_cast<Tool>(id));
});
// Pen color
label = new QLabel(tr("Pen color:"), this);
layout->addWidget(label);
QSlider *slider = new QSlider(Qt::Horizontal, this);
slider->setMinimum(0);
slider->setMaximum(255);
slider->setValue(0);
layout->addWidget(slider);
connect(slider, &QSlider::valueChanged, [&](int value) {
m_penColor.setRed(value);
m_drawingBoard.setPenColor(m_penColor);
});
slider = new QSlider(Qt::Horizontal, this);
slider->setMinimum(0);
slider->setMaximum(255);
slider->setValue(0);
layout->addWidget(slider);
connect(slider, &QSlider::valueChanged, [&](int value) {
m_penColor.setGreen(value);
m_drawingBoard.setPenColor(m_penColor);
});
slider = new QSlider(Qt::Horizontal, this);
slider->setMinimum(0);
slider->setMaximum(255);
slider->setValue(0);
layout->addWidget(slider);
connect(slider, &QSlider::valueChanged, [&](int value) {
m_penColor.setBlue(value);
m_drawingBoard.setPenColor(m_penColor);
});
// Brush color
label = new QLabel(tr("Brush color:"), this);
layout->addWidget(label);
QDial *dial = new QDial(this);
dial->setNotchesVisible(true);
layout->addWidget(dial);
connect(dial, &QDial::valueChanged, [&](int value) {
m_brushColor.setRed(value);
m_drawingBoard.setBrushColor(m_brushColor);
});
dial = new QDial(this);
dial->setNotchesVisible(true);
layout->addWidget(dial);
connect(dial, &QDial::valueChanged, [&](int value) {
m_brushColor.setGreen(value);
m_drawingBoard.setBrushColor(m_brushColor);
});
dial = new QDial(this);
dial->setNotchesVisible(true);
layout->addWidget(dial);
connect(dial, &QDial::valueChanged, [&](int value) {
m_brushColor.setBlue(value);
m_drawingBoard.setBrushColor(m_brushColor);
});
}
private:
DrawingBoard m_drawingBoard;
QColor m_penColor = Qt::black;
QColor m_brushColor = Qt::transparent;
void keyPressEvent(QKeyEvent *event) override
{
if (event->key() == Qt::Key_Escape) {
QApplication::quit();
}
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
```
在这个示例代码中,我们创建了一个名为 DrawingBoard 的自定义 QWidget 子类,用于绘制图形。该类实现了鼠标按下、拖动和释放事件的处理函数,并根据用户选择的工具和参数来绘制不同的图形。另外,该类还提供了一些公共函数,用于设置画笔颜色、线条宽度和画刷颜色。
在 MainWindow 类中,我们创建了一个 DockWidget,包含了一些控件,用于设置画笔和画刷的参数,以及选择绘图工具。我们还重写了 keyPressEvent() 函数,以便用户按下 Esc 键时退出应用程序。
运行该应用程序后,用户可以通过选择不同的工具、设置不同的参数和移动鼠标来绘制图形。在绘图时,用户可以实时调整画笔和画刷的参数,以达到想要的效果。
阅读全文