Geometry.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
main.cpp
#include "widget.h"
#include
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
class Widget : public QWidget {
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
void updateLabel();
protected:
void moveEvent(QMoveEvent *);
void resizeEvent(QResizeEvent *);
private:
QLabel *xLabel;
QLabel *xValueLabel;
QLabel *yLabel;
QLabel *yValueLabel;
QLabel *posLabel;
QLabel *posValueLabel;
QLabel *frmLabel;
QLabel *frmValueLabel;
QLabel *geoLabel;
QLabel *geoValueLabel;
QLabel *widthLabel;
QLabel *widthValueLabel;
QLabel *heightLabel;
QLabel *heightValueLabel;
QLabel *rectLabel;
QLabel *rectValueLabel;
QLabel *sizeLabel;
QLabel *sizeValueLabel;
QGridLayout *mainLayout;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include
#include
Widget::Widget(QWidget *parent):QWidget(parent) {
setWindowFlags(Qt::WindowCloseButtonHint);
xLabel = new QLabel(tr("x():"));
xValueLabel = new QLabel;
yLabel = new QLabel(tr("y():"));
yValueLabel = new QLabel;
posLabel = new QLabel(tr("pos():"));
posValueLabel = new QLabel;
frmLabel = new QLabel(tr("frameGeometry():"));
frmValueLabel = new QLabel;
geoLabel = new QLabel(tr("geometry():"));
geoValueLabel = new QLabel;
widthLabel = new QLabel(tr("width():"));
widthValueLabel = new QLabel;
heightLabel = new QLabel(tr("height():"));
heightValueLabel = new QLabel;
rectLabel = new QLabel(tr("rect():"));
rectValueLabel = new QLabel;
sizeLabel = new QLabel(tr("size():"));
sizeValueLabel = new QLabel;
mainLayout = new QGridLayout(this);
mainLayout->addWidget(xLabel, 0, 0);
mainLayout->addWidget(xValueLabel, 0, 1);
mainLayout->addWidget(yLabel, 1, 0);
mainLayout->addWidget(yValueLabel, 1, 1);
mainLayout->addWidget(posLabel, 2, 0);
mainLayout->addWidget(posValueLabel, 2, 1);
mainLayout->addWidget(frmLabel, 3, 0);
mainLayout->addWidget(frmValueLabel, 3, 1);
mainLayout->addWidget(geoLabel, 4, 0);
mainLayout->addWidget(geoValueLabel, 4, 1);
mainLayout->addWidget(widthLabel, 5, 0);
mainLayout->addWidget(widthValueLabel, 5, 1);
mainLayout->addWidget(heightLabel, 6, 0);
mainLayout->addWidget(heightValueLabel, 6, 1);
mainLayout->addWidget(rectLabel, 7, 0);
mainLayout->addWidget(rectValueLabel, 7, 1);
mainLayout->addWidget(sizeLabel, 8, 0);
mainLayout->addWidget(sizeValueLabel, 8, 1);
}
void Widget::updateLabel(){
xValueLabel->setText(QString::number(x()));
yValueLabel->setText(QString::number(y()));
posValueLabel->setText(QString("(%1, %2)")
.arg(pos().x()).arg(pos().y()));
frmValueLabel->setText(QString("(%1, %2, %3, %4)")
.arg(frameGeometry().x()).arg(frameGeometry().y())
.arg(frameGeometry().width()).arg(frameGeometry().height()));
geoValueLabel->setText(QString("(%1, %2, %3, %4)")
.arg(geometry().x()).arg(geometry().y())
.arg(geometry().width()).arg(geometry().height()));
widthValueLabel->setText(QString::number(width()));
heightValueLabel->setText(QString::number(height()));
rectValueLabel->setText(QString("(%1, %2, %3, %4)")
.arg(rect().x()).arg(rect().y())
.arg(rect().width()).arg(rect().height()));
sizeValueLabel->setText(QString("(%1, %2)")
.arg(size().width()).arg(size().height()));
}
void Widget::moveEvent(QMoveEvent *) {
updateLabel();
}
void Widget::resizeEvent(QResizeEvent *){
updateLabel();
}
Paint.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
SOURCES += \
main.cpp \
paintarea.cpp \
widget.cpp
HEADERS += \
paintarea.h \
widget.h
main.cpp
#include "widget.h"
#include
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include "paintarea.h"
#include
#include
#include
#include
#include
#include
#include
class Widget : public QWidget {
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
private slots:
void on_penColorBtn_clicked();
void on_brushColorBtn_clicked();
void setShape();
void setPen();
void setFillRule();
void setBrush();
private:
PaintArea *paintArea;
QLabel *shapeLabel;
QComboBox *shapeComboBox;
QLabel *penColorLabel;
QFrame *penColorFrame;
QPushButton *penColorBtn;
QLabel *penWidthLabel;
QSpinBox *penWidthSpinBox;
QLabel *penStyleLabel;
QComboBox *penStyleComboBox;
QLabel *penCapLabel;
QComboBox *penCapComboBox;
QLabel *penJoinLabel;
QComboBox *penJoinComboBox;
QLabel *fillRuleLabel;
QComboBox *fillRuleComboBox;
QLabel *spreadLabel;
QComboBox *spreadComboBox;
QLabel *brushColorLabel;
QFrame *brushColorFrame;
QPushButton *brushColorBtn;
QLabel *brushStyleLabel;
QComboBox *brushStyleComboBox;
QGridLayout *rightLayout;
QHBoxLayout *mainLayout;
PaintArea::Shape shape;
QColor penColor;
int penWidth;
Qt::PenStyle penStyle;
Qt::PenCapStyle penCap;
Qt::PenJoinStyle penJoin;
Qt::FillRule rule;
QGradient::Spread spread;
QColor brushColor;
Qt::BrushStyle brushStyle;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include
Widget::Widget(QWidget *parent):QWidget(parent) {
setWindowFlags(Qt::WindowCloseButtonHint);
paintArea = new PaintArea;
shapeLabel = new QLabel(tr("形状:"));
shapeComboBox = new QComboBox;
shapeComboBox->addItem(tr("Line"), PaintArea::Line);
shapeComboBox->addItem(tr("Rectangle"), PaintArea::Rectangle);
shapeComboBox->addItem(tr("RoundRect"), PaintArea::RoundRect);
shapeComboBox->addItem(tr("Ellipse"), PaintArea::Ellipse);
shapeComboBox->addItem(tr("Polygon"), PaintArea::Polygon);
shapeComboBox->addItem(tr("Polyline"), PaintArea::Polyline);
shapeComboBox->addItem(tr("Points"), PaintArea::Points);
shapeComboBox->addItem(tr("Arc"), PaintArea::Arc);
shapeComboBox->addItem(tr("Path"), PaintArea::Path);
shapeComboBox->addItem(tr("Text"), PaintArea::Text);
shapeComboBox->addItem(tr("Pixmap"), PaintArea::Pixmap);
penColorLabel = new QLabel(tr("画笔颜色:"));
penColorFrame = new QFrame;
penColorFrame->setFrameStyle(QFrame::Panel | QFrame::Sunken);
penColorFrame->setAutoFillBackground(true);
penColorFrame->setPalette(QPalette(Qt::blue));
penColorBtn = new QPushButton(tr("更改"));
penWidthLabel = new QLabel(tr("画笔线宽:"));
penWidthSpinBox = new QSpinBox;
penWidthSpinBox->setRange(0, 20);
penStyleLabel = new QLabel(tr("画笔风格:"));
penStyleComboBox = new QComboBox;
penStyleComboBox->addItem(tr("SolidLine"), static_cast<int>(Qt::SolidLine));
penStyleComboBox->addItem(tr("DashLine"), static_cast<int>(Qt::DashLine));
penStyleComboBox->addItem(tr("DotLine"), static_cast<int>(Qt::DotLine));
penStyleComboBox->addItem(tr("DashDotLine"), static_cast<int>(Qt::DashDotLine));
penStyleComboBox->addItem(tr("DashDotDotLine"), static_cast<int>(Qt::DashDotDotLine));
penStyleComboBox->addItem(tr("CustomDashLine"), static_cast<int>(Qt::CustomDashLine));
penCapLabel = new QLabel(tr("画笔顶帽:"));
penCapComboBox = new QComboBox;
penCapComboBox->addItem(tr("SquareCap"), Qt::SquareCap);
penCapComboBox->addItem(tr("FlatCap"), Qt::FlatCap);
penCapComboBox->addItem(tr("RoundCap"), Qt::RoundCap);
penJoinLabel = new QLabel(tr("画笔连接点:"));
penJoinComboBox = new QComboBox;
penJoinComboBox->addItem(tr("BevelJoin"), Qt::BevelJoin);
penJoinComboBox->addItem(tr("MiterJoin"), Qt::MiterJoin);
penJoinComboBox->addItem(tr("RoundJoin"), Qt::RoundJoin);
fillRuleLabel = new QLabel(tr("填充模式:"));
fillRuleComboBox = new QComboBox;
fillRuleComboBox->addItem(tr("OddEvenFill"), Qt::OddEvenFill);
fillRuleComboBox->addItem(tr("WindingFill"), Qt::WindingFill);
spreadLabel = new QLabel(tr("铺展效果:"));
spreadComboBox = new QComboBox;
spreadComboBox->addItem(tr("PadSpread"), QGradient::PadSpread);
spreadComboBox->addItem(tr("RepeatSpread"), QGradient::RepeatSpread);
spreadComboBox->addItem(tr("ReflectSpread"), QGradient::ReflectSpread);
brushColorLabel = new QLabel(tr("画刷颜色:"));
brushColorFrame = new QFrame;
brushColorFrame->setFrameStyle(QFrame::Panel | QFrame::Sunken);
brushColorFrame->setAutoFillBackground(true);
brushColorFrame->setPalette(QPalette(Qt::green));
brushColorBtn = new QPushButton(tr("更改"));
brushStyleLabel = new QLabel(tr("画刷风格:"));
brushStyleComboBox = new QComboBox;
brushStyleComboBox->addItem(tr("SolidPattern"), static_cast<int>(Qt::SolidPattern));
brushStyleComboBox->addItem(tr("Dense1Pattern"), static_cast<int>(Qt::Dense1Pattern));
brushStyleComboBox->addItem(tr("Dense2Pattern"), static_cast<int>(Qt::Dense2Pattern));
brushStyleComboBox->addItem(tr("Dense3Pattern"), static_cast<int>(Qt::Dense3Pattern));
brushStyleComboBox->addItem(tr("Dense4Pattern"), static_cast<int>(Qt::Dense4Pattern));
brushStyleComboBox->addItem(tr("Dense5Pattern"), static_cast<int>(Qt::Dense5Pattern));
brushStyleComboBox->addItem(tr("Dense6Pattern"), static_cast<int>(Qt::Dense6Pattern));
brushStyleComboBox->addItem(tr("Dense7Pattern"), static_cast<int>(Qt::Dense7Pattern));
brushStyleComboBox->addItem(tr("HorPattern"), static_cast<int>(Qt::HorPattern));
brushStyleComboBox->addItem(tr("VerPattern"), static_cast<int>(Qt::VerPattern));
brushStyleComboBox->addItem(tr("CrossPattern"), static_cast<int>(Qt::CrossPattern));
brushStyleComboBox->addItem(tr("BDiagPattern"), static_cast<int>(Qt::BDiagPattern));
brushStyleComboBox->addItem(tr("FDiagPattern"), static_cast<int>(Qt::FDiagPattern));
brushStyleComboBox->addItem(tr("DiagCrossPattern"), static_cast<int>(Qt::DiagCrossPattern));
brushStyleComboBox->addItem(tr("LinearGradientPattern"), static_cast<int>(Qt::LinearGradientPattern));
brushStyleComboBox->addItem(tr("ConicalGradientPattern"), static_cast<int>(Qt::ConicalGradientPattern));
brushStyleComboBox->addItem(tr("RadialGradientPattern"), static_cast<int>(Qt::RadialGradientPattern));
brushStyleComboBox->addItem(tr("TexturePattern"), static_cast<int>(Qt::TexturePattern));
rightLayout = new QGridLayout;
rightLayout->addWidget(shapeLabel, 0, 0);
rightLayout->addWidget(shapeComboBox, 0, 1);
rightLayout->addWidget(penColorLabel, 1, 0);
rightLayout->addWidget(penColorFrame, 1, 1);
rightLayout->addWidget(penColorBtn, 1, 2);
rightLayout->addWidget(penWidthLabel, 2, 0);
rightLayout->addWidget(penWidthSpinBox, 2, 1);
rightLayout->addWidget(penStyleLabel, 3, 0);
rightLayout->addWidget(penStyleComboBox, 3, 1);
rightLayout->addWidget(penCapLabel, 4, 0);
rightLayout->addWidget(penCapComboBox, 4, 1);
rightLayout->addWidget(penJoinLabel, 5, 0);
rightLayout->addWidget(penJoinComboBox, 5, 1);
rightLayout->addWidget(fillRuleLabel, 6, 0);
rightLayout->addWidget(fillRuleComboBox, 6, 1);
rightLayout->addWidget(spreadLabel, 7, 0);
rightLayout->addWidget(spreadComboBox, 7, 1);
rightLayout->addWidget(brushColorLabel, 8, 0);
rightLayout->addWidget(brushColorFrame, 8, 1);
rightLayout->addWidget(brushColorBtn, 8, 2);
rightLayout->addWidget(brushStyleLabel, 9, 0);
rightLayout->addWidget(brushStyleComboBox, 9, 1);
mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(paintArea, 1);
mainLayout->addLayout(rightLayout);
connect(shapeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setShape()));
connect(penColorBtn, SIGNAL(clicked()), this, SLOT(on_penColorBtn_clicked()));
connect(penWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setPen()));
connect(penStyleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setPen()));
connect(penCapComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setPen()));
connect(penJoinComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setPen()));
connect(fillRuleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setFillRule()));
connect(spreadComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setBrush()));
connect(brushColorBtn, SIGNAL(clicked()), this, SLOT(on_brushColorBtn_clicked()));
connect(brushStyleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setBrush()));
setShape();
setPen();
setFillRule();
setBrush();
}
void Widget::on_penColorBtn_clicked() {
QColor color = QColorDialog::getColor(penColor, this, tr("设置画笔颜色"));
if(color.isValid()) {
penColorFrame->setPalette(QPalette(color));
setPen();
}
}
void Widget::on_brushColorBtn_clicked() {
QColor color = QColorDialog::getColor(brushColor, this, tr("设置画刷颜色"));
if(color.isValid()) {
brushColorFrame->setPalette(QPalette(color));
setBrush();
}
}
void Widget::setShape() {
shape = PaintArea::Shape(shapeComboBox->currentData().toInt());
paintArea->setShape(shape);
}
void Widget::setPen() {
penColor = penColorFrame->palette().color(QPalette::Window);
penWidth = penWidthSpinBox->value();
penStyle = Qt::PenStyle(penStyleComboBox->currentData().toInt());
penCap = Qt::PenCapStyle(penCapComboBox->currentData().toInt());
penJoin = Qt::PenJoinStyle(penJoinComboBox->currentData().toInt());
paintArea->setPen(QPen(penColor, penWidth, penStyle, penCap, penJoin));
}
void Widget::setFillRule() {
rule = Qt::FillRule(fillRuleComboBox->currentData().toInt());
paintArea->setFillRule(rule);
}
void Widget::setBrush() {
spread = QGradient::Spread(spreadComboBox->currentData().toInt());
brushColor = brushColorFrame->palette().color(QPalette::Window);
brushStyle = Qt::BrushStyle(brushStyleComboBox->currentData().toInt());
if(brushStyle == Qt::LinearGradientPattern){
QLinearGradient linearGradient(0, 0, 400, 400);
linearGradient.setColorAt(0.0, Qt::white);
linearGradient.setColorAt(0.2, brushColor);
linearGradient.setColorAt(1.0, Qt::black);
linearGradient.setSpread(spread);
paintArea->setBrush(linearGradient);
} else if(brushStyle == Qt::RadialGradientPattern){
QRadialGradient radialGradient(200, 200, 150, 150, 100);
radialGradient.setColorAt(0.0, Qt::white);
radialGradient.setColorAt(0.2, brushColor);
radialGradient.setColorAt(1.0, Qt::black);
radialGradient.setSpread(spread);
paintArea->setBrush(radialGradient);
} else if(brushStyle == Qt::ConicalGradientPattern){
QConicalGradient concialGradient(200, 200, 30);
concialGradient.setColorAt(0.0, Qt::white);
concialGradient.setColorAt(0.2, brushColor);
concialGradient.setColorAt(1.0, Qt::black);
concialGradient.setSpread(spread);
paintArea->setBrush(concialGradient);
} else if(brushStyle == Qt::TexturePattern){
paintArea->setBrush(QBrush(QPixmap("butterfly.png")));
} else {
paintArea->setBrush(QBrush(brushColor, brushStyle));
}
}
paintarea.h
#ifndef PAINTAREA_H
#define PAINTAREA_H
#include
#include
#include
class PaintArea : public QWidget {
Q_OBJECT
public:
enum Shape{Line, Rectangle, RoundRect, Ellipse, Polygon, Polyline, Points, Arc, Path, Text, Pixmap};
explicit PaintArea(QWidget *parent = nullptr);
void setShape(Shape s);
void setPen(QPen p);
void setBrush(QBrush b);
void setFillRule(Qt::FillRule rule);
void paintEvent(QPaintEvent *);
private:
Shape shape;
QPen pen;
QBrush brush;
Qt::FillRule fillRule;
};
#endif
paintarea.cpp
#include "paintarea.h"
#include
PaintArea::PaintArea(QWidget *parent) : QWidget(parent) {
setPalette(QPalette(Qt::white));
setAutoFillBackground(true);
/*与上面等价
QPalette p = palette();
p.setColor(QPalette::Window, Qt::white);
setPalette(p);
*/
setMinimumSize(400, 400);
shape = Line;
fillRule = Qt::OddEvenFill;
}
void PaintArea::setShape(Shape s){
shape = s;
update();
}
void PaintArea::setPen(QPen p){
pen = p;
update();
}
void PaintArea::setBrush(QBrush b){
brush = b;
update();
}
void PaintArea::setFillRule(Qt::FillRule rule){
fillRule = rule;
update();
}
void PaintArea::paintEvent(QPaintEvent *) {
static const QRect rect(50, 100, 300, 200);
static const QPoint points[4] = {
QPoint(150, 100),
QPoint(300, 150),
QPoint(350, 250),
QPoint(100, 300)
};
static const int startAngle = 30*16;
static const int spanAngle = 30*16;
static QPainterPath path;
path.addRect(150, 150, 100, 100);
path.moveTo(100, 100);
path.cubicTo(300, 100, 200, 200, 300, 300);
path.cubicTo(100, 300, 200, 200, 100, 100);
path.setFillRule(fillRule);
QPainter p(this);
p.setPen(pen);
p.setBrush(brush);
switch (shape) {
case Line:
p.drawLine(rect.topLeft(), rect.bottomRight());
break;
case Rectangle:
p.drawRect(rect);
break;
case RoundRect:
p.drawRoundRect(rect);
break;
case Ellipse:
p.drawEllipse(rect);
break;
case Polygon:
p.drawPolygon(points, 4, fillRule);
break;
case Polyline:
p.drawPolyline(points, 4);
break;
case Points:
p.drawPoints(points, 4);
break;
case Arc:
p.drawArc(rect, startAngle, spanAngle);
break;
case Path:
p.drawPath(path);
break;
case Text:
p.drawText(rect, Qt::AlignCenter, tr("你好 Qt!"));
break;
case Pixmap:
p.drawPixmap(150, 150, QPixmap("butterfly.png"));
break;
default:
break;
}
}
DrawWidgt.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp \
widget.cpp
HEADERS += \
mainwindow.h \
widget.h
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QFont font("ZYSong18030", 12);
a.setFont(font);
MainWindow w;
w.show();
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
class Widget : public QWidget {
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *);
public slots:
void setStyle(int);
void setWidth(int);
void setColor(QColor);
void clear();
private:
QPixmap *pix;
QPoint startPos;
int style;
int weight;
QColor color;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include
#include
Widget::Widget(QWidget *parent) : QWidget(parent) {
setAutoFillBackground(true);
setPalette(QPalette(Qt::white));
startPos = QPoint(0, 0);
style = Qt::SolidLine;
weight = 0;
color = Qt::black;
pix = new QPixmap(size());
pix->fill(Qt::white);
setMinimumSize(600, 400);
}
void Widget::mousePressEvent(QMouseEvent *e) {
startPos = e->pos();
}
void Widget::mouseMoveEvent(QMouseEvent *e) {
QPainter painter(this);
QPen pen;
pen.setStyle((Qt::PenStyle)style);
pen.setWidth(weight);
pen.setColor(color);
painter.begin(pix);
painter.setPen(pen);
painter.drawLine(startPos, e->pos());
painter.end();
update();
startPos = e->pos();
}
void Widget::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.drawPixmap(QPoint(0, 0), *pix);
}
void Widget::resizeEvent(QResizeEvent * e) {
if(height()>pix->height() || width()>pix->width()){
QPixmap *newPix = new QPixmap(size());
newPix->fill(Qt::white);
QPainter p(newPix);
p.drawPixmap(QPoint(0, 0), *pix);
delete pix;
pix = newPix;
}
QWidget::resizeEvent(e);
}
void Widget::setStyle(int s) {
style = s ;
}
void Widget::setWidth(int w) {
weight = w;
}
void Widget::setColor(QColor c) {
color = c;
}
void Widget::clear() {
pix->fill(Qt::white);
update();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include "widget.h"
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
void createToolBar();
private slots:
void setStyle();
void setWidth();
void setColor();
private:
Widget *widget;
QLabel *styleLabel;
QComboBox *styleComboBox;
QLabel *widthLabel;
QSpinBox *widthSpinBox;
QToolButton *colorBtn;
QToolButton *clearBtn;
int style;
int weight;
QColor color;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include
#include
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
setWindowFlags(Qt::WindowCloseButtonHint);
widget = new Widget;
setCentralWidget(widget);
createToolBar();
setMinimumSize(600, 400);
setStyle();
setWidth();
color = Qt::black;
}
void MainWindow::createToolBar()
{
QToolBar *toolBar = addToolBar("Tool");
styleLabel = new QLabel(tr("线型风格:"));
styleComboBox = new QComboBox;
styleComboBox->addItem(tr("SolidLine"), static_cast<int>(Qt::SolidLine));
styleComboBox->addItem(tr("DashLine"), static_cast<int>(Qt::DashLine));
styleComboBox->addItem(tr("DotLine"), static_cast<int>(Qt::DotLine));
styleComboBox->addItem(tr("DashDotLine"), static_cast<int>(Qt::DashDotLine));
styleComboBox->addItem(tr("DashDotDotLine"), static_cast<int>(Qt::DashDotDotLine));
widthLabel = new QLabel(tr("线宽:"));
widthSpinBox = new QSpinBox;
widthSpinBox->setRange(0, 20);
colorBtn = new QToolButton;
QPixmap pixmap(20, 20);
pixmap.fill(Qt::black);
colorBtn->setIcon(QIcon(pixmap));
clearBtn = new QToolButton;
clearBtn->setText(tr("清除"));
toolBar->addWidget(styleLabel);
toolBar->addWidget(styleComboBox);
toolBar->addWidget(widthLabel);
toolBar->addWidget(widthSpinBox);
toolBar->addWidget(colorBtn);
toolBar->addWidget(clearBtn);
connect(styleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setStyle()));
connect(widthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setWidth()));
connect(colorBtn, SIGNAL(clicked()), this, SLOT(setColor()));
connect(clearBtn, SIGNAL(clicked()), widget, SLOT(clear()));
color = Qt::black;
}
void MainWindow::setStyle() {
style = styleComboBox->currentData().toInt();
widget->setStyle(style);
}
void MainWindow::setWidth() {
weight = widthSpinBox->value();
widget->setWidth(weight);
}
void MainWindow::setColor() {
QColor cur_color = QColorDialog::getColor(color, this, tr("设置画笔颜色"));
if(cur_color.isValid()) {
color = cur_color;
widget->setColor(color);
QPixmap pixmap(20, 20);
pixmap.fill(color);
colorBtn->setIcon(QIcon(pixmap));
}
}
SVGTest.pro
QT += core gui svg
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = SVGTest
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\
mainwindow.cpp \
svgwindow.cpp \
svgwidget.cpp
HEADERS += mainwindow.h \
svgwindow.h \
svgwidget.h
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include "svgwindow.h"
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
void createMenu();
public slots:
void slotOpenFile();
private:
SvgWindow *svgWindow;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
setWindowTitle(tr("SVG Viewer"));
createMenu();
svgWindow =new SvgWindow;
setCentralWidget(svgWindow);
}
void MainWindow::createMenu() {
QAction *openAct = new QAction(tr("打开"), this);
connect(openAct, SIGNAL(triggered()), this, SLOT(slotOpenFile()));
QMenu *fileMenu = menuBar()->addMenu(tr("文件"));
fileMenu->addAction(openAct);
}
void MainWindow::slotOpenFile() {
QString name = QFileDialog::getOpenFileName(this, "打开", "/", "svg files(*.svg)");
if(!name.isEmpty()) {
svgWindow->setFile(name);
}
}
svgwindow.h
#ifndef SVGWINDOW_H
#define SVGWINDOW_H
#include
#include "svgwidget.h"
class SvgWindow : public QScrollArea {
Q_OBJECT
public:
explicit SvgWindow(QWidget *parent=0);
void setFile(QString);
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
private:
SvgWidget *svgWidget;
QPoint mousePressPos;
QPoint scrollBarValuesOnMousePress;
};
#endif // SVGWINDOW_H
svgwindow.cpp
#include "svgwindow.h"
SvgWindow::SvgWindow(QWidget *parent):QScrollArea(parent) {
svgWidget = new SvgWidget;
setWidget(svgWidget);
}
void SvgWindow::setFile(QString fileName) {
svgWidget->load(fileName);
QSvgRenderer *render = svgWidget->renderer();
svgWidget->resize(render->defaultSize());
}
void SvgWindow::mousePressEvent(QMouseEvent *event) {
mousePressPos = event->pos();
scrollBarValuesOnMousePress.rx() = horizontalScrollBar()->value();
scrollBarValuesOnMousePress.ry() = verticalScrollBar()->value();
event->accept();
}
void SvgWindow::mouseMoveEvent(QMouseEvent *event) {
horizontalScrollBar()->setValue(scrollBarValuesOnMousePress.x()-event->pos().x()+mousePressPos.x()); //对水平滑动条的新位置进行设置
verticalScrollBar()->setValue(scrollBarValuesOnMousePress.y()-event->pos().y()+mousePressPos.y()); //对垂直滑动条的新位置进行设置
horizontalScrollBar()->update();
verticalScrollBar()->update();
event->accept();
}
svgwidget.h
#ifndef SVGWIDGET_H
#define SVGWIDGET_H
#include
#include
#include
class SvgWidget : public QSvgWidget {
Q_OBJECT
public:
explicit SvgWidget(QWidget *parent=0);
void wheelEvent(QWheelEvent *);
private:
QSvgRenderer *render;
};
#endif // SVGWIDGET_H
svgwidget.cpp
#include "svgwidget.h"
SvgWidget::SvgWidget(QWidget *parent):QSvgWidget(parent) {
render = renderer();
}
void SvgWidget::wheelEvent(QWheelEvent *e) {
const double diff=0.1;
int width ;
int height;
if(e->delta()>0) {
width = this->width()*(1.0+diff);
height = this->height()*(1.0+diff);
} else {
width = this->width()*(1.0-diff);
height = this->height()*(1.0-diff);
}
resize(width, height);
}