代碼下載連接: http://pan.baidu.com/s/1hsc41Ek 密碼: 5hdgc++
顯示效果以下:函數
代碼附有詳細註釋(代碼以下)工具
1 /*** 2 * 先新建QMainWindow, 項目名稱: DrawWidget 基類選擇: QMainWindow, 3 * 類名默認, 而後在DrawWidget項目名上新建c++class文件, 選擇基類: QWidget 4 */ 5 //先完成繪圖區的實現 6 //以下爲: drawwidget.h 7 #ifndef DRAWWIDGET_H 8 #define DRAWWIDGET_H 9 10 #include <QWidget> 11 #include <QtGui> 12 #include <QMouseEvent> 13 #include <QPaintEvent> 14 #include <QResizeEvent> 15 #include <QColor> 16 #include <QPixmap> 17 #include <QPoint> 18 #include <QPainter> 19 #include <QPalette> 20 21 class DrawWidget : public QWidget 22 { 23 Q_OBJECT 24 public: 25 explicit DrawWidget(QWidget *parent = 0); 26 //鼠標事件重定義 27 void mousePressEvent (QMouseEvent *); 28 void mouseMoveEvent (QMouseEvent *); 29 //重畫事件重定義 30 void paintEvent (QPaintEvent *); 31 //尺寸變化事件重定義 32 void resizeEvent (QResizeEvent *); 33 signals: 34 public slots: 35 void setStyle (int); 36 void setWidth (int); 37 void setColor (QColor); 38 void clear (); 39 private: 40 QPixmap *pix; 41 QPoint startPos; //點類 42 QPoint endPos; 43 int style; 44 int weight; 45 QColor color; 46 }; 47 48 #endif // DRAWWIDGET_H
1 //drawwidget.cpp 2 //DrawWidget構造函數完成對窗體參數及部分功能的初始化工做 3 #include "drawwidget.h" 4 #include <QtGui> 5 #include <QPen> 6 7 DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent) 8 { 9 setAutoFillBackground (true); //對窗體背景色的設置 10 setPalette (QPalette(Qt::white)); //背景色爲白 11 pix = new QPixmap(size()); //此QPixmap對象用來準備隨時接受繪製的內容 12 pix->fill (Qt::white); //填充背景色爲白色 13 setMinimumSize (600, 400); //設置繪製區窗體的最小尺寸 14 } 15 16 //接受主窗體傳來的線型風格參數 17 void DrawWidget::setStyle (int s) 18 { 19 style = s; 20 } 21 22 //setWidth()接受主窗體傳來的線寬參數值 23 void DrawWidget::setWidth (int w) 24 { 25 weight = w; 26 } 27 28 //接受主窗體傳來的畫筆顏色值 29 void DrawWidget::setColor (QColor c) 30 { 31 color = c; 32 } 33 34 //重定義鼠標按下事件--按下鼠標時,記錄當前鼠標位置值startPos 35 void DrawWidget::mousePressEvent (QMouseEvent *e) 36 { 37 startPos = e->pos (); 38 } 39 40 //重定義鼠標移動事件--默認狀況下,在鼠標按下的同時拖曳鼠標時被觸發. 41 //mouseTracking事件,能夠經過設置setMouseTracking(bool enable)爲true, 42 //則不管是否有鼠標鍵按下,只要鼠標移動,就會觸發mouseMoveEvent() 43 //在此函數中,完成向QPixmap對象中繪圖的工做. 44 void DrawWidget::mouseMoveEvent (QMouseEvent *e) 45 { 46 QPainter *painter = new QPainter; //新建一個QPainter對象 47 QPen pen; //新建一個QPen對象 48 //設置畫筆的線型,style表示當前選擇的線型是Qt::PenStyle枚舉數據中的第幾個元素 49 pen.setStyle ((Qt::PenStyle)style); 50 pen.setWidth (weight); //設置畫筆的線寬值 51 pen.setColor (color); //設置畫筆的顏色 52 /*** 53 * 以QPixmap對象爲QPaintDevice參數繪製,構造一個QPainter對象, 54 * 就當即開始對繪畫設備進行繪製,此構造QPainter對象是短時間的 55 * 因爲當一個QPainter對象的初始化失敗時構造函數不能提供反饋信息, 56 * 因此在繪製 外部設備時 應使用begin()和end()(Ps:如打印機外部設備) 57 */ 58 painter->begin (pix); 59 painter->setPen (pen); //將QPen對象應用到繪製對象當中 60 //繪製從startPos到鼠標當前位置的直線 61 painter->drawLine (startPos, e->pos ()); 62 painter->end (); //繪製成功返回true 63 startPos = e->pos (); //更新鼠標的當前位置,爲下次繪製作準備 64 update (); //重繪繪製區窗體 65 } 66 67 /*** 68 * 重畫函數paintEvent()完成繪製區窗體的更新工做,只須要調用drawPixmap()函數將用於接收圖形繪製的 69 * 的QPixmap對象繪製在繪製區窗體控件上便可. 70 */ 71 void DrawWidget::paintEvent (QPaintEvent *) 72 { 73 QPainter painter(this); 74 painter.drawPixmap (QPoint(0,0), *pix); 75 } 76 77 /*** 78 * 調整繪製區大小函數resizeEvent(): 79 * 當窗體大小改變是,實際可以繪製的區域仍然沒有改變,由於繪圖的大小沒有改變 80 * 因此窗體尺寸變化時,應及時調整用於繪製的QPixmap對象的尺寸大小 81 */ 82 void DrawWidget::resizeEvent (QResizeEvent *event) 83 { 84 //判斷改變後的窗體長或寬是否大於原窗體的長和寬; 85 //若大於則進行相應調整; 86 if (height () > pix->height () || width () > pix->width ()) 87 { 88 QPixmap *newPix = new QPixmap(size()); //建立一個新的QPixmap對象 89 newPix->fill (Qt::white); //填充新QPixmap對象newPix的顏色爲白色背景色 90 QPainter p(newPix); 91 p.drawPixmap (QPoint(0, 0), *pix); //在newPix中繪製原pix中內容 92 pix = newPix; //將newPix賦值給Pix做爲新的繪製圖形接收對象 93 } 94 //不然直接調用QWidget的resizeEvent()函數返回 95 QWidget::resizeEvent (event); //完成其他工做 96 97 } 98 99 /*** 100 * clear()函數完成繪製區的清除工做,只須要一個新的,乾淨的QPixmap對象代替pix,並調用update()重繪便可 101 */ 102 void DrawWidget::clear () 103 { 104 QPixmap *clearPix = new QPixmap(size()); 105 clearPix->fill (Qt::white); 106 pix = clearPix; 107 update (); 108 }
1 //以上爲可以響應鼠標事件進行繪圖功能的窗體類實現 2 //主窗口的實現 3 //mainwindow.h 4 #ifndef MAINWINDOW_H 5 #define MAINWINDOW_H 6 7 #include <QMainWindow> 8 #include <QToolButton> 9 #include <QLabel> 10 #include <QComboBox> //下拉列表框 11 #include <QSpinBox> //自選盒 12 #include "drawwidget.h" 13 14 class MainWindow : public QMainWindow 15 { 16 Q_OBJECT 17 18 public: 19 MainWindow(QWidget *parent = 0); 20 ~MainWindow(); 21 void createToolBar(); //建立工具欄 22 public slots: 23 void ShowStyle(); //進行選擇線型風格的槽函數 24 void ShowColor(); //選擇顏色的槽函數 25 private: 26 DrawWidget *drawWidget; //建立可以響應鼠標事件進行繪圖功能的窗體類 27 QLabel *styleLabel; //風格 28 QComboBox *styleComboBox; 29 QLabel *widthLabel; //線寬 30 QSpinBox *widthSpinBox; //線寬自旋框 31 QToolButton *colorBtn; //顏色工具 32 QToolButton *clearBtn; //清除按鈕 33 }; 34 35 #endif // MAINWINDOW_H
1 //mainwindow.cpp 2 #include "mainwindow.h" 3 #include <QToolBar> 4 #include <QColorDialog> 5 6 MainWindow::MainWindow(QWidget *parent) 7 : QMainWindow(parent) 8 { 9 drawWidget = new DrawWidget; //新建一個DrawWidget對象--可以響應鼠標事件進行繪圖功能的窗體類 10 setCentralWidget (drawWidget); //新建的DrawWidget對象做爲主窗口的中央窗體 11 createToolBar (); //實現一個工具欄 12 setMinimumSize (600, 400); //設置主窗口的最小尺寸 13 ShowStyle (); //初始化線型,設置控件中的當前值做爲初始值 14 drawWidget->setWidth (widthSpinBox->value ()); //初始化線寬 15 drawWidget->setColor (Qt::black); //初始化顏色 16 } 17 18 //工具欄建立 19 void MainWindow::createToolBar () 20 { 21 QToolBar *toolBar = addToolBar ("Tool"); //爲主窗口新建一個工具欄對象 22 styleLabel = new QLabel(tr("線型風格: ")); //建立線性選擇控件 23 styleComboBox = new QComboBox; 24 styleComboBox->addItem (tr("SolidLine"), 25 static_cast<int>(Qt::SolidLine)); 26 styleComboBox->addItem (tr("DashLine"), 27 static_cast<int>(Qt::DashLine)); 28 styleComboBox->addItem (tr("DotLine"), 29 static_cast<int>(Qt::DotLine)); 30 styleComboBox->addItem (tr("DashDotLine"), 31 static_cast<int>(Qt::DashDotLine)); 32 styleComboBox->addItem (tr("DashDotDotLine"), 33 static_cast<int>(Qt::DashDotDotLine)); 34 connect (styleComboBox, SIGNAL(activated(int)), this, SLOT(ShowStyle())); //關聯相應的槽函數 35 widthLabel = new QLabel(tr("線寬: ")); //建立線寬選擇控件 36 widthSpinBox = new QSpinBox; 37 connect (widthSpinBox, SIGNAL(valueChanged(int)), drawWidget, SLOT(setWidth(int))); 38 39 colorBtn = new QToolButton; //建立顏色選擇控件 40 QPixmap pixmap(20, 20); //顏色選擇按鈕控件上的圖像 41 pixmap.fill (Qt::black); //填充黑色 42 colorBtn->setIcon (QIcon(pixmap)); //設置按鈕圖像 43 connect (colorBtn, SIGNAL(clicked(bool)), this, SLOT(ShowColor())); 44 45 clearBtn = new QToolButton(); //建立清除按鈕 46 clearBtn->setText (tr("清除")); 47 connect (clearBtn, SIGNAL(clicked(bool)), drawWidget, SLOT(clear())); 48 49 toolBar->addWidget (styleLabel); 50 toolBar->addWidget (styleComboBox); 51 toolBar->addWidget (widthLabel); 52 toolBar->addWidget (widthSpinBox); 53 toolBar->addWidget (colorBtn); 54 toolBar->addWidget (clearBtn); 55 } 56 57 //ShowStyle(),經過調用DrawWidget類的setStyle()函數將當前線型選擇控件中的線型參數傳給繪製區; 58 void MainWindow::ShowStyle () 59 { 60 drawWidget->setStyle (styleComboBox->itemData (styleComboBox->currentIndex (), 61 Qt::UserRole).toInt ()); 62 } 63 64 //ShowColor(),經過DrawWidget類的setColor()函數將用戶在標準顏色對話框中選擇的顏色值傳給繪製區 65 void MainWindow::ShowColor () 66 { 67 QColor color = QColorDialog::getColor (static_cast<int>(Qt::black)); //默認爲黑(static_cast<int>轉換成int節省內存 68 //使用標準顏色對話框QColorDialog得到一個顏色值 69 if (color.isValid ()) 70 { 71 //先將新選擇的顏色傳給繪製區,用於改變畫筆的顏色值 72 drawWidget->setColor (color); 73 //改變按鈕圖案 74 QPixmap p(20, 20); //設置圖像大小 75 p.fill (color); //填充顏色 76 colorBtn->setIcon (QIcon(p)); //設置顏色按鈕圖案 77 } 78 } 79 80 81 MainWindow::~MainWindow() 82 { 83 }
1 //main.cpp 2 #include "mainwindow.h" 3 #include <QApplication> 4 #include <QFont> 5 6 int main(int argc, char *argv[]) 7 { 8 QApplication a(argc, argv); 9 QFont font("ZYSong18030", 12); 10 a.setFont (font); 11 12 MainWindow w; 13 w.show(); 14 15 return a.exec(); 16 }