Utility Classes - QwtPlot Convenience API¶
Qwt 7 provides a set of utility classes in the Qwt namespace that simplify common plotting operations. These classes are organized by single-responsibility, so users can include only what they need.
Utility Class Overview¶
The class diagram below outlines the five utility classes and their respective targets: QwtPlotItemInfo and QwtPlotDataAccess query and read/write plot items, QwtPlotFactory creates items on a QwtPlot, QwtPlotTransform converts coordinates, and QwtPlotStyling reads and writes visual styles:
classDiagram
class QwtPlotItemInfo {
+isSeriesItem()
+isXYSeriesItem()
+filterByRtti()
+dataSize()
+rttiName()
}
class QwtPlotDataAccess {
+xySamples()
+intervalSamples()
+ohlcSamples()
+setXYSamples()
+removeSamplesInRange()
}
class QwtPlotFactory {
+createCurve()
+createBarChart()
+createGrid()
+createMarker()
+createHLine()
}
class QwtPlotTransform {
+transformPoint()
+toPlotPoint()
+onePixelOffset()
+visibleRange()
}
class QwtPlotStyling {
+color()
+setColor()
+pen()
+brush()
+setSymbol()
}
QwtPlotItemInfo ..> QwtPlotItem : queries
QwtPlotDataAccess ..> QwtPlotItem : reads/writes
QwtPlotFactory ..> QwtPlot : creates items
QwtPlotTransform ..> QwtPlot : transforms
QwtPlotStyling ..> QwtPlotItem : styles
| Class | Responsibility | Header |
|---|---|---|
QwtPlotItemInfo |
Type queries and filtering | <QwtPlotItemInfo> |
QwtPlotDataAccess |
Sample data extraction and setting | <QwtPlotDataAccess> |
QwtPlotFactory |
Factory methods for creating plot items | <QwtPlotFactory> |
QwtPlotTransform |
Coordinate transformations | <QwtPlotTransform> |
QwtPlotStyling |
Visual style read/write | <QwtPlotStyling> |
QwtPlotFactory — Factory Methods¶
QwtPlotFactory provides one-line methods to create and attach plot items to a QwtPlot, covering all common item types.
Creating Curves¶
createCurve() returns an already-attached QwtPlotCurve* and offers three data overloads: a QVector<QPointF>, separate x/y vectors, or y-only values with x generated as the sample index:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
The factory methods create the item, set its data, bind the axes, and attach it to the plot. The returned pointer can be further customized.
Creating Bar Charts and Histograms¶
createBarChart() builds a QwtPlotBarChart* from plain values, whereas createHistogram() expects pre-binned QwtIntervalSample data (value plus interval bounds) and produces a QwtPlotHistogram*:
1 2 3 4 5 6 7 8 9 10 | |
Creating Trading Curves (Candlestick/OHLC)¶
createTradingCurve() attaches a QwtPlotTradingCurve* fed with QwtOHLCSample values — each sample carries the time plus open, high, low, and close prices:
1 2 3 4 5 6 | |
Creating Box Charts and Vector Fields¶
createBoxChart() draws statistical boxes from QwtBoxSample values (position, minimum, first quartile, median, third quartile, maximum), while createVectorField() renders arrows from QwtVectorFieldSample entries carrying (x, y, u, v):
1 2 3 4 5 6 7 8 9 10 | |
Creating Decorator Items¶
The decorator factories cover grids, reference lines, markers, zones, legends, and arrows: createGrid() takes a boolean for minor grid lines, createHLine() / createVLine() default to a gray dashed pen, and createZone() highlights a QwtInterval with the given brush:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Tip — Complete Factory Method List
QwtPlotFactorycovers all 9 data plot items and 7 decorator items. See the API documentation for the full list.
QwtPlotItemInfo — Type Queries¶
QwtPlotItemInfo is used to query item types and filter items. It never modifies any plot or item state.
Type Checks¶
A set of boolean predicates classifies items by role: isSeriesItem() recognizes anything carrying a data series, isXYSeriesItem() / isIntervalSeriesItem() distinguish the two sample layouts, and isDecoratorItem() flags non-data items such as grids and markers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Filtering by Type¶
filterByRtti() selects items from a plot by a single rtti value or by a QSet<int> of several types; seriesItems(), xySeriesItems(), and visibleItems() are ready-made shortcuts for common subsets:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Getting Basic Information¶
dataSize() reports the number of samples (or -1 for items without data), dataRect() gives the bounding rectangle of the data, and rttiName() turns an rtti constant into a readable string:
1 2 3 4 5 6 7 8 | |
QwtPlotDataAccess — Data Read/Write¶
QwtPlotDataAccess dispatches through rtti to extract or set sample data from any QwtPlotItem, covering all 7 data types.
Extracting XY Data¶
xySamples() works on any item storing XY samples — such as QwtPlotCurve or QwtPlotBarChart — either returning a QVector<QPointF> or filling separate x and y vectors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Extracting Other Data Types¶
For the remaining sample layouts, dedicated getters return the native sample types: intervalSamples(), ohlcSamples(), xyzSamples(), setSamples(), vectorFieldSamples(), and boxSamples(), each yielding the corresponding QVector when the item's rtti matches:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Setting Data¶
The setters mirror the getters: setXYSamples() replaces the samples of an XY item (from points or from separate x/y vectors) and returns true on success, while setOhlcSamples() does the same for trading curves:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Template Extraction¶
When the concrete type is known, use the template methods directly:
1 2 3 4 5 6 7 8 9 | |
Range-Based Extraction and Removal¶
xySamplesInRange() selects the samples falling inside a QRectF or an arbitrary QPainterPath (in data coordinates), and removeSamplesInRange() deletes them, returning the number of samples removed:
1 2 3 4 5 6 7 8 9 10 11 12 | |
QwtPlotTransform — Coordinate Transformations¶
QwtPlotTransform provides conversions between coordinate systems. All methods are read-only and never modify plot state.
Cross-Axis Transformation¶
When a plot uses parasite axes (multiple axes), the same screen position has different data coordinates in different axis systems:
1 2 3 4 5 6 7 8 9 10 11 | |
Screen/Data Coordinate Conversion¶
toPlotPoint() converts a position within the widget (such as a mouse click) into data coordinates, and toScreenPoint() performs the inverse mapping:
1 2 3 4 5 6 | |
Pixel Offset¶
onePixelOffset() returns how much one pixel corresponds to in data coordinates on the given axes (defaults to QwtAxis::XBottom / QwtAxis::YLeft):
1 2 3 | |
This is useful for implementing drag, nudge, or other pixel-precision interactions.
Visible Range¶
visibleRange() reports the data rectangle currently shown on the canvas, while totalDataRange() computes the union of the data items' bounding rectangles; its onlyVisible flag (default true) controls whether hidden items are included:
1 2 3 4 5 6 7 8 9 10 | |
QwtPlotStyling — Style Operations¶
QwtPlotStyling dispatches through rtti to uniformly read/write colors, pens, brushes, and symbols on any plot item.
Unified Color Read/Write¶
color() reads back an item's dominant color according to its type, and setColor() writes it to wherever that item keeps its color (pen, brush, …). Items without a color notion yield an invalid QColor, or the defaultColor passed to color():
1 2 3 4 5 6 7 8 9 10 11 12 | |
Color sources by item type:
| Item Type | Color Source |
|---|---|
| QwtPlotCurve | pen().color() |
| QwtPlotBarChart | brush().color() |
| QwtPlotHistogram | brush().color() |
| QwtPlotGrid | majorPen().color() |
| QwtPlotMarker | linePen().color() |
| QwtPlotTradingCurve | symbolPen().color() |
Pen and Brush¶
pen() / brush() fetch an item's pen or brush regardless of its concrete type, and setPen() / setBrush() apply new ones, returning true when the item supports that property:
1 2 3 4 5 6 7 8 9 | |
Curve Symbols¶
For QwtPlotCurve, setSymbol() builds a QwtSymbol from a QwtSymbol::Style (e.g. Ellipse, Diamond) with a size defaulting to 8×8; the color parameter is optional and, when omitted, the symbol follows the curve color. setLineStyle() sets the pen style and width:
1 2 3 4 5 6 7 8 9 10 | |
Recommendations and Force Replot¶
recommendPenWidth() suggests a pen width from the number of points so dense curves stay readable, and forceReplot() redraws the plot immediately even when autoReplot is disabled:
1 2 3 4 5 | |
Complete Example¶
The following example demonstrates combining multiple utility classes for a common plotting workflow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
Supported Plot Item Types¶
The utility classes cover all plot item types in Qwt 7:
Data Plot Items¶
| Plot Item | Data Type | Factory Method | Data Read/Write |
|---|---|---|---|
QwtPlotCurve |
QPointF |
createCurve() |
xySamples() / setXYSamples() |
QwtPlotBarChart |
QPointF |
createBarChart() |
xySamples() / setXYSamples() |
QwtPlotMultiBarChart |
QwtSetSample |
createMultiBarChart() |
setSamples() / setSetSamples() |
QwtPlotHistogram |
QwtIntervalSample |
createHistogram() |
intervalSamples() / setIntervalSamples() |
QwtPlotIntervalCurve |
QwtIntervalSample |
createIntervalCurve() |
intervalSamples() / setIntervalSamples() |
QwtPlotTradingCurve |
QwtOHLCSample |
createTradingCurve() |
ohlcSamples() / setOhlcSamples() |
QwtPlotSpectroCurve |
QwtPoint3D |
createSpectroCurve() |
xyzSamples() / setXyzSamples() |
QwtPlotVectorField |
QwtVectorFieldSample |
createVectorField() |
vectorFieldSamples() / setVectorFieldSamples() |
QwtPlotBoxChart |
QwtBoxSample |
createBoxChart() |
boxSamples() / setBoxSamples() |
Decorator Items¶
| Plot Item | Factory Method |
|---|---|
QwtPlotGrid |
createGrid() |
QwtPlotMarker |
createMarker() / createHLine() / createVLine() |
QwtPlotZoneItem |
createZone() |
QwtPlotArrowMarker |
createArrowMarker() |
QwtPlotGraphicItem |
createGraphic() |
QwtPlotTextLabel |
createTextLabel() |
QwtPlotLegendItem |
createLegend() |
QwtPlotScaleItem |
createScaleItem() |
Example — Related Examples
See the examples under
examples/2D/for practical usage of these utility classes in context.
Qt stylesheet (QSS) presets¶
Besides the programmatic API, QwtPlot is an ordinary QWidget and accepts Qt stylesheets. The repository ships four ready-made QSS presets in examples/2D/stylesheets/ (blue.css, choco.css, oily.css, rosy.css) that define borders, rounded corners and gradient backgrounds for the QwtPlot widget:
1 2 3 | |
Tip
QSS only affects the widget appearance (canvas border, background, ...); pens, brushes and symbols of plot items remain governed by
QwtPlotStylingand each item's own properties.
Higher-Level Wrapper: QwtPyPlot¶
If you are familiar with matplotlib's interface style, QwtPyPlot offers a more compact plotting interface. QwtPyPlot uses the utility classes described above internally, but exposes a single interface similar to plt.plot():
1 2 3 4 5 6 7 8 9 10 | |