📄 scribble-main-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - scribble/main.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Simple Painting Application</h1><br clear="all"> This example implements the famous scribble example. You can draw around in the canvas with different pens and save the result as picture. <hr> Header file: <pre>/****************************************************************************** $Id: qt/examples/scribble/scribble.h 2.3.8 edited 2004-05-12 $**** Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef SCRIBBLE_H#define SCRIBBLE_H#include <<a href="qmainwindow-h.html">qmainwindow.h</a>>#include <<a href="qpen-h.html">qpen.h</a>>#include <<a href="qpoint-h.html">qpoint.h</a>>#include <<a href="qpixmap-h.html">qpixmap.h</a>>#include <<a href="qwidget-h.html">qwidget.h</a>>#include <<a href="qstring-h.html">qstring.h</a>>#include <<a href="qpointarray-h.html">qpointarray.h</a>>class QMouseEvent;class QResizeEvent;class QPaintEvent;class QToolButton;class QSpinBox;class Canvas : public QWidget{ Q_OBJECTpublic: Canvas( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 ); void setPenColor( const QColor &c ) { pen.<a href="qpen.html#00d674">setColor</a>( c ); } void setPenWidth( int w ) { pen.<a href="qpen.html#6a009f">setWidth</a>( w ); } <a href="qcolor.html">QColor</a> penColor() { return pen.<a href="qpen.html#0db022">color</a>(); } int penWidth() { return pen.<a href="qpen.html#7bedc6">width</a>(); } void save( const QString &filename, const QString &format ); void clearScreen();protected: void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ); void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ); void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ); void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> *e ); void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> *e ); <a href="qpen.html">QPen</a> pen; <a href="qpointarray.html">QPointArray</a> polyline; bool mousePressed; <a href="qpixmap.html">QPixmap</a> buffer;};class Scribble : public QMainWindow{ Q_OBJECTpublic: Scribble( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );protected: Canvas* canvas; <a href="qspinbox.html">QSpinBox</a> *bPWidth; <a href="qtoolbutton.html">QToolButton</a> *bPColor, *bSave, *bClear;protected slots: void slotSave(); void slotColor(); void slotWidth( int ); void slotClear();};#endif</pre> <hr> Implementation: <pre>/****************************************************************************** $Id: qt/examples/scribble/scribble.cpp 2.3.8 edited 2004-05-12 $**** Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "scribble.h"#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qevent-h.html">qevent.h</a>>#include <<a href="qpainter-h.html">qpainter.h</a>>#include <<a href="qtoolbar-h.html">qtoolbar.h</a>>#include <<a href="qtoolbutton-h.html">qtoolbutton.h</a>>#include <<a href="qspinbox-h.html">qspinbox.h</a>>#include <<a href="qtooltip-h.html">qtooltip.h</a>>#include <<a href="qrect-h.html">qrect.h</a>>#include <<a href="qpoint-h.html">qpoint.h</a>>#include <<a href="qcolordialog-h.html">qcolordialog.h</a>>#include <<a href="qfiledialog-h.html">qfiledialog.h</a>>#include <<a href="qcursor-h.html">qcursor.h</a>>#include <<a href="qimage-h.html">qimage.h</a>>#include <<a href="qstrlist-h.html">qstrlist.h</a>>#include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>#include <<a href="qintdict-h.html">qintdict.h</a>>const bool no_writing = FALSE;Canvas::Canvas( <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qwidget.html">QWidget</a>( parent, name, WNorthWestGravity ), pen( Qt::red, 3 ), polyline(3), mousePressed( FALSE ), buffer( width(), <a href="qwidget.html#e3c588">height</a>() ){ if ((qApp->argc() > 0) && !buffer.load(qApp->argv()[1])) buffer.fill( <a href="qwidget.html#d92bf4">colorGroup</a>().base() ); <a href="qwidget.html#e84bbe">setBackgroundMode</a>( QWidget::PaletteBase );#ifndef QT_NO_CURSOR <a href="qwidget.html#9b6e22">setCursor</a>( Qt::crossCursor );#endif}void <a name="440"></a>Canvas::save( const QString &filename, const QString &format ){ if ( !no_writing ) buffer.save( filename, format.<a href="qstring.html#c4017b">upper</a>() );}void <a name="441"></a>Canvas::clearScreen(){ buffer.fill( <a href="qwidget.html#d92bf4">colorGroup</a>().base() ); <a href="qwidget.html#7569b1">repaint</a>( FALSE );}void <a name="442"></a>Canvas::mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ){ mousePressed = TRUE; polyline[2] = polyline[1] = polyline[0] = e->pos();}void <a name="443"></a>Canvas::mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * ){ mousePressed = FALSE;}void <a name="444"></a>Canvas::mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ){ if ( mousePressed ) { <a href="qpainter.html">QPainter</a> painter; painter.<a href="qpainter.html#02ed5d">begin</a>( &buffer ); painter.<a href="qpainter.html#0183e4">setPen</a>( pen ); polyline[2] = polyline[1]; polyline[1] = polyline[0]; polyline[0] = e->pos(); painter.<a href="qpainter.html#134ad2">drawPolyline</a>( polyline ); painter.<a href="qpainter.html#365784">end</a>(); <a href="qrect.html">QRect</a> r = polyline.boundingRect(); r = r.<a href="qrect.html#cd60e4">normalize</a>(); r.<a href="qrect.html#818d83">setLeft</a>( r.<a href="qrect.html#369cab">left</a>() - penWidth() ); r.<a href="qrect.html#2052ad">setTop</a>( r.<a href="qrect.html#4dd27e">top</a>() - penWidth() ); r.<a href="qrect.html#f802ff">setRight</a>( r.<a href="qrect.html#896e32">right</a>() + penWidth() ); r.<a href="qrect.html#3ea256">setBottom</a>( r.<a href="qrect.html#2de4b0">bottom</a>() + penWidth() ); <a href="qpaintdevice.html#35ae2e">bitBlt</a>( this, r.<a href="qrect.html#fccae7">x</a>(), r.<a href="qrect.html#f448f7">y</a>(), &buffer, r.<a href="qrect.html#fccae7">x</a>(), r.<a href="qrect.html#f448f7">y</a>(), r.<a href="qrect.html#45fe95">width</a>(), r.<a href="qrect.html#581ab8">height</a>() ); }}void <a name="445"></a>Canvas::resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> *e ){ <a href="qwidget.html#7d375f">QWidget::resizeEvent</a>( e ); int w = width() > buffer.width() ? <a href="qwidget.html#2edab1">width</a>() : buffer.width(); int h = height() > buffer.height() ? <a href="qwidget.html#e3c588">height</a>() : buffer.height(); <a href="qpixmap.html">QPixmap</a> tmp( buffer ); buffer.resize( w, h ); buffer.fill( <a href="qwidget.html#d92bf4">colorGroup</a>().base() ); <a href="qpaintdevice.html#35ae2e">bitBlt</a>( &buffer, 0, 0, &tmp, 0, 0, tmp.<a href="qpixmap.html#86dbc5">width</a>(), tmp.<a href="qpixmap.html#d5bb33">height</a>() );}void <a name="446"></a>Canvas::paintEvent( <a href="qpaintevent.html">QPaintEvent</a> *e ){ <a href="qwidget.html#e3d821">QWidget::paintEvent</a>( e ); <a href="qarray.html">QArray</a><<a href="qrect.html">QRect</a>> rects = e-><a href="qpaintevent.html#dc5fc5">region</a>().rects(); for ( uint i = 0; i < rects.<a href="qarray.html#33baa8">count</a>(); i++ ) { <a href="qrect.html">QRect</a> r = rects[(int)i]; <a href="qpaintdevice.html#35ae2e">bitBlt</a>( this, r.<a href="qrect.html#fccae7">x</a>(), r.<a href="qrect.html#f448f7">y</a>(), &buffer, r.<a href="qrect.html#fccae7">x</a>(), r.<a href="qrect.html#f448f7">y</a>(), r.<a href="qrect.html#45fe95">width</a>(), r.<a href="qrect.html#581ab8">height</a>() ); }}//------------------------------------------------------Scribble::Scribble( <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qmainwindow.html">QMainWindow</a>( parent, name ){ canvas = new Canvas( this ); <a href="qmainwindow.html#fce9ba">setCentralWidget</a>( canvas ); <a href="qtoolbar.html">QToolBar</a> *tools = new <a href="qtoolbar.html">QToolBar</a>( this ); bSave = new <a href="qtoolbutton.html">QToolButton</a>( <a href="qpixmap.html">QPixmap</a>(), "Save", "Save as PNG image", this, SLOT( <a href=#447>slotSave</a>() ), tools ); bSave->setText( "Save as..." ); tools-><a href="qtoolbar.html#af219d">addSeparator</a>(); bPColor = new <a href="qtoolbutton.html">QToolButton</a>( <a href="qpixmap.html">QPixmap</a>(), "Choose Pen Color", "Choose Pen Color", this, SLOT( <a href=#448>slotColor</a>() ), tools ); bPColor->setText( "Choose Pen Color..." ); tools-><a href="qtoolbar.html#af219d">addSeparator</a>(); bPWidth = new <a href="qspinbox.html">QSpinBox</a>( 1, 20, 1, tools ); <a href="qtooltip.html#184bfe">QToolTip::add</a>( bPWidth, "Choose Pen Width" ); <a href="qobject.html#fbde73">connect</a>( bPWidth, SIGNAL( valueChanged( int ) ), this, SLOT( <a href=#449>slotWidth</a>( int ) ) ); bPWidth->setValue( 3 ); tools-><a href="qtoolbar.html#af219d">addSeparator</a>(); bClear = new <a href="qtoolbutton.html">QToolButton</a>( <a href="qpixmap.html">QPixmap</a>(), "Clear Screen", "Clear Screen", this, SLOT( <a href=#450>slotClear</a>() ), tools ); bClear->setText( "Clear Screen" );}void <a name="447"></a>Scribble::slotSave(){ <a href="qpopupmenu.html">QPopupMenu</a> *menu = new <a href="qpopupmenu.html">QPopupMenu</a>( 0 ); <a href="qintdict.html">QIntDict</a><<a href="qstring.html">QString</a>> formats; formats.<a href="qcollection.html#a8ef9f">setAutoDelete</a>( TRUE ); for ( unsigned int i = 0; i < <a href="qimageio.html#040fd9">QImageIO::outputFormats</a>().count(); i++ ) { <a href="qstring.html">QString</a> str = QString( <a href="qimageio.html#040fd9">QImageIO::outputFormats</a>().at( i ) ); formats.<a href="qintdict.html#8ec12f">insert</a>( menu-><a href="qmenudata.html#deddb9">insertItem</a>( <a href="qstring.html">QString</a>( "%1..." ).arg( str ) ), new <a href="qstring.html">QString</a>( str ) ); } menu-><a href="qwidget.html#36406c">setMouseTracking</a>( TRUE ); int id = menu-><a href="qpopupmenu.html#ac4d90">exec</a>( bSave->mapToGlobal( <a href="qpoint.html">QPoint</a>( 0, bSave->height() + 1 ) ) ); if ( id != -1 ) { <a href="qstring.html">QString</a> format = *formats[ id ]; <a href="qstring.html">QString</a> filename = QFileDialog::getSaveFileName( QString::null, QString( "*.%1" ).arg( format.<a href="qstring.html#6ee35a">lower</a>() ), this ); if ( !filename.<a href="qstring.html#c62623">isEmpty</a>() ) canvas->save( filename, format ); } delete menu;}void <a name="448"></a>Scribble::slotColor(){ <a href="qcolor.html">QColor</a> c = QColorDialog::getColor( canvas->penColor(), this ); canvas->setPenColor( c );}void <a name="449"></a>Scribble::slotWidth( int w ){ canvas->setPenWidth( w );}void <a name="450"></a>Scribble::slotClear(){ canvas->clearScreen();}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/scribble/main.cpp 2.3.8 edited 2004-05-12 $**** Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "scribble.h"#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>int main( int argc, char **argv ){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv ); Scribble scribble; scribble.<a name="resize"></a><a href="qwidget.html#ff9d07">resize</a>( 500, 350 ); scribble.<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Scribble"); a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( &scribble ); if ( <a name="QApplication::desktop"></a><a href="qapplication.html#d8ed8c">QApplication::desktop</a>()->width() > 550 && <a href="qapplication.html#d8ed8c">QApplication::desktop</a>()->height() > 366 ) scribble.<a name="show"></a><a href="qmainwindow.html#eb53e3">show</a>(); else scribble.<a name="showMaximized"></a><a href="qwidget.html#b6e000">showMaximized</a>(); return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -