13,007
回編集
265行目: | 265行目: | ||
item->attach(m_pPlot); | item->attach(m_pPlot); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | |||
== 画像の縮小 == | |||
以下の例では、ボタンを押下する時、プロットの中心を基準に画像を縮小している。<br> | |||
<syntaxhighlight lang="c++"> | |||
void MyPlot::ZoomOut() | |||
{ | |||
QRectF rcNew; | |||
// 現在のズーム枠を取得 | |||
QRectF rc = m_pZoomer->zoomRect(); | |||
rc.normalized(); | |||
// ズームアウト枠を設定 | |||
rcNew.setLeft(rc.left() - dRange * 0.1); | |||
rcNew.setRight(rc.right() + dRange * 0.1); | |||
rcNew.setTop(rc.top() - dRange * 0.1); | |||
rcNew.setBottom(rc.bottom() + dRange * 0.1); | |||
// ズームアウト枠を適用 | |||
m_pZoomer->zoom( rcNew ); | |||
// ズームスタックをリセット | |||
m_pZoomer->setZoomBase(); | |||
canvas()->update(); | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
画像の縮小機能を実装する場合、ズームスタックは、都度リセットする方が無難である。<br> | |||
また、パンニングの時もズームスタックを都度リセットして、ズームベースを更新する。<br> | |||
<u>ズームベースとは、ズームの初期化時のズーム範囲のこと。</u><br> | |||
<syntaxhighlight lang="c++"> | |||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) | |||
{ | |||
// コンストラクタでパンニング完了時のシグナル・スロットを設定 | |||
QObject::connect(m_pPanner, SIGNAL(panned(int, int)), this, SLOT(updateZoom(int, int))); | |||
// ...略 | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// QwtPlotクラスを継承した派生クラスのcppファイル | |||
// パンニング完了時のスロット | |||
void MyPlot::updateZoom(int nX, int nY) | |||
{ | |||
QRectF rc = m_pZoomer->zoomRect(); | |||
QPointF pnt = rc.topLeft(); | |||
// 現在のズーム枠を移動してズームベースを更新 | |||
m_pZoomer->setZoomBase(QRectF(QPointF(pnt.x() + nX, pnt.y() + nY), rc.size())); | |||
canvas()->replot(); | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
== 目盛りの設定 == | |||
標準では、目盛りは実数値が表示されるが、目盛りの値を変更することができる。<br> | |||
<br> | |||
以下の例では、度単位の数値を度分秒表記にしている。<br> | |||
まず、<code>QwtScaleDraw</code>クラスを継承した派生クラスを作成する。<br> | |||
<syntaxhighlight lang="c++"> | |||
// 縦軸クラス | |||
class DMSScaleDrawLat : public QwtScaleDraw | |||
{ | |||
public: | |||
DMSScaleDrawLat() | |||
{ | |||
setLabelAlignment( Qt::AlignLeft | Qt::AlignVCenter ); | |||
} | |||
// 与えられた数値を度分秒に変換 | |||
virtual QwtText label( double dVal ) const | |||
{ | |||
int nM; | |||
double dS; | |||
int nD = (int)floor( dVal ); | |||
double dS = (dVal - nD)*60; | |||
int nM = (int)floor( dS ); | |||
double dS = (dS - nM) * 60; | |||
return QObject::tr( "%1d\n %2' %3\"" ).arg(nD).arg(nM).arg(dS); | |||
} | |||
}; | |||
// 横軸クラス | |||
// 縦軸クラスの派生クラスとして、ラベルの向きだけを変更 | |||
class DMSScaleDrawLon : public DMSScaleDrawLat | |||
{ | |||
public: | |||
DMSScaleDrawLon() | |||
{ | |||
setLabelRotation( -90.0 ); | |||
} | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
上記で定義した縦軸および横軸クラスを、<code>QwtPlot</code>クラスを継承した派生クラスの各軸に設定する。<br> | |||
以下の例では、<code>QwtPlot</code>クラスを継承した派生クラスのコンストラクタで設定している。<br> | |||
<syntaxhighlight lang="c++"> | |||
MyPlot::MyPlot(QWidget *parent) : QwtPlot(parent) | |||
{ | |||
// ...略 | |||
// 縦軸および横軸クラスを設定する | |||
setAxisScaleDraw(QwtPlot::xBottom, new DMSScaleDrawLon()); | |||
setAxisScaleDraw(QwtPlot::yLeft, new DMSScaleDrawLat()); | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
標準の軸ラベルは、与えられた数値に対して、<code>QLocale().toString(value)</code>で返される文字列を表示する。<br> | |||
(qwt_abstract_scale_draw.hファイルの<code>label</code>メソッド)<br> | |||
<br> | |||
この時、大きい数値は指数型の文字列を返す。<br> | |||
指数型の文字列を表示させない場合も、上記のように、<code>QwtPlot</code>クラスを継承した派生クラスを作成する必要がある。<br> | |||
<br><br> | <br><br> | ||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:Qt]] | [[カテゴリ:Qt]] |