📄 canvas-chart-example.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/examples/chart/chart.doc:1 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>A Complete Canvas Application</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: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>A Complete Canvas Application</h1> <p> <p> This is a complete example program with a main window, menus andtoolbars. The main widget is a <a href="qcanvas.html">QCanvas</a>, and this exampledemonstrates basic canvas usage. <p> <hr><p> Project file:<p> <pre>TEMPLATE = appCONFIG += warn_onHEADERS += element.h \ canvastext.h \ canvasview.h \ chartform.h \ optionsform.h \ setdataform.hSOURCES += element.cpp \ canvasview.cpp \ chartform.cpp \ chartform_canvas.cpp \ chartform_files.cpp \ optionsform.cpp \ setdataform.cpp \ main.cpp</pre><p> <hr><p> Header files:<p> <pre></pre><pre>#ifndef ELEMENT_H#define ELEMENT_H#include <<a href="qcolor-h.html">qcolor.h</a>>#include <<a href="qnamespace-h.html">qnamespace.h</a>>#include <<a href="qstring-h.html">qstring.h</a>>#include <<a href="qvaluevector-h.html">qvaluevector.h</a>>class Element;typedef QValueVector<Element> ElementVector;/* Elements are valid if they have a value which is > EPSILON.*/const double EPSILON = 0.0000001; // Must be > INVALID.class Element{public: enum { INVALID = -1 }; enum { NO_PROPORTION = -1 }; enum { MAX_PROPOINTS = 3 }; // One proportional point per chart type Element( double value = INVALID, QColor valueColor = Qt::gray, int valuePattern = Qt::SolidPattern, const <a href="qstring.html">QString</a>& label = <a href="qstring.html#QString-null">QString::null</a>, <a href="qcolor.html">QColor</a> labelColor = Qt::black ) { init( value, valueColor, valuePattern, label, labelColor ); for ( int i = 0; i < MAX_PROPOINTS * 2; ++i ) m_propoints[i] = NO_PROPORTION; } ~Element() {} bool isValid() const { return m_value > EPSILON; } double value() const { return m_value; } <a href="qcolor.html">QColor</a> valueColor() const { return m_valueColor; } int valuePattern() const { return m_valuePattern; } <a href="qstring.html">QString</a> label() const { return m_label; } <a href="qcolor.html">QColor</a> labelColor() const { return m_labelColor; } double proX( int index ) const; double proY( int index ) const; void set( double value = INVALID, QColor valueColor = Qt::gray, int valuePattern = Qt::SolidPattern, const <a href="qstring.html">QString</a>& label = QString::null, <a href="qcolor.html">QColor</a> labelColor = Qt::black ) { init( value, valueColor, valuePattern, label, labelColor ); } void setValue( double value ) { m_value = value; } void setValueColor( <a href="qcolor.html">QColor</a> valueColor ) { m_valueColor = valueColor; } void setValuePattern( int valuePattern ); void setLabel( const <a href="qstring.html">QString</a>& label ) { m_label = label; } void setLabelColor( <a href="qcolor.html">QColor</a> labelColor ) { m_labelColor = labelColor; } void setProX( int index, double value ); void setProY( int index, double value );private: void init( double value, QColor valueColor, int valuePattern, const <a href="qstring.html">QString</a>& label, QColor labelColor ); double m_value; <a href="qcolor.html">QColor</a> m_valueColor; int m_valuePattern; <a href="qstring.html">QString</a> m_label; <a href="qcolor.html">QColor</a> m_labelColor; double m_propoints[2 * MAX_PROPOINTS];};Q_EXPORT QTextStream &operator<<( <a href="qtextstream.html">QTextStream</a>&, const Element& );Q_EXPORT QTextStream &operator>>( <a href="qtextstream.html">QTextStream</a>&, Element& );#endif</pre><pre>#ifndef CHARTFORM_H#define CHARTFORM_H#include "element.h"#include <<a href="qmainwindow-h.html">qmainwindow.h</a>>#include <<a href="qstringlist-h.html">qstringlist.h</a>>class CanvasView;class QAction;class QCanvas;class QFont;class QPrinter;class QString;class ChartForm: public <a href="qmainwindow.html">QMainWindow</a>{ <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>public: enum { MAX_ELEMENTS = 100 }; enum { MAX_RECENTFILES = 9 }; // Must not exceed 9 enum ChartType { PIE, VERTICAL_BAR, HORIZONTAL_BAR }; enum AddValuesType { NO, YES, AS_PERCENTAGE }; ChartForm( const <a href="qstring.html">QString</a>& filename ); ~ChartForm(); int chartType() { return m_chartType; } void setChanged( bool changed = true ) { m_changed = changed; } void drawElements(); <a href="qpopupmenu.html">QPopupMenu</a> *optionsMenu; // Why public? See canvasview.cppprivate slots: void fileNew(); void fileOpen(); void fileOpenRecent( int index ); void fileSave(); void fileSaveAs(); void fileSaveAsPixmap(); void filePrint(); void fileQuit(); void optionsSetData(); void updateChartType( <a href="qaction.html">QAction</a> *action ); void optionsSetFont(); void optionsSetOptions(); void helpHelp(); void helpAbout(); void helpAboutQt(); void saveOptions();private: void init(); void load( const <a href="qstring.html">QString</a>& filename ); bool okToClear(); void drawPieChart( const double scales[], double total, int count ); void drawVerticalBarChart( const double scales[], double total, int count ); void drawHorizontalBarChart( const double scales[], double total, int count ); <a href="qstring.html">QString</a> valueLabel( const <a href="qstring.html">QString</a>& label, double value, double total ); void updateRecentFiles( const <a href="qstring.html">QString</a>& filename ); void updateRecentFilesMenu(); void setChartType( ChartType chartType ); <a href="qpopupmenu.html">QPopupMenu</a> *fileMenu; <a href="qaction.html">QAction</a> *optionsPieChartAction; <a href="qaction.html">QAction</a> *optionsHorizontalBarChartAction; <a href="qaction.html">QAction</a> *optionsVerticalBarChartAction; <a href="qstring.html">QString</a> m_filename; <a href="qstringlist.html">QStringList</a> m_recentFiles; <a href="qcanvas.html">QCanvas</a> *m_canvas; CanvasView *m_canvasView; bool m_changed; ElementVector m_elements; <a href="qprinter.html">QPrinter</a> *m_printer; ChartType m_chartType; AddValuesType m_addValues; int m_decimalPlaces; <a href="qfont.html">QFont</a> m_font;};#endif</pre><p> <hr><p> Implementation:<p> <pre>#include "canvasview.h"#include "chartform.h"#include "optionsform.h"#include "setdataform.h"#include <<a href="qaction-h.html">qaction.h</a>>#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qcombobox-h.html">qcombobox.h</a>>#include <<a href="qfile-h.html">qfile.h</a>>#include <<a href="qfiledialog-h.html">qfiledialog.h</a>>#include <<a href="qfont-h.html">qfont.h</a>>#include <<a href="qfontdialog-h.html">qfontdialog.h</a>>#include <<a href="qmenubar-h.html">qmenubar.h</a>>#include <<a href="qmessagebox-h.html">qmessagebox.h</a>>#include <<a href="qpixmap-h.html">qpixmap.h</a>>#include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>#include <<a href="qprinter-h.html">qprinter.h</a>>#include <<a href="qradiobutton-h.html">qradiobutton.h</a>>#include <<a href="qsettings-h.html">qsettings.h</a>>#include <<a href="qspinbox-h.html">qspinbox.h</a>>#include <<a href="qstatusbar-h.html">qstatusbar.h</a>>#include <<a href="qtoolbar-h.html">qtoolbar.h</a>>#include <<a href="qtoolbutton-h.html">qtoolbutton.h</a>>#include "images/file_new.xpm"#include "images/file_open.xpm"#include "images/file_save.xpm"#include "images/file_print.xpm"#include "images/options_setdata.xpm"#include "images/options_setfont.xpm"#include "images/options_setoptions.xpm"#include "images/options_horizontalbarchart.xpm"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -