目錄
一、 問題描述
二、 解決方案
三、代碼實(shí)現(xiàn)
一、 問題描述
如題所述。
二、 解決方案
自定義一個(gè)繼承QCustomPlot類的實(shí)現(xiàn)類,重寫mouseMove虛函數(shù)實(shí)現(xiàn),從而獲取鼠標(biāo)位置,然后計(jì)算出點(diǎn)的坐標(biāo)值,再利用QToolTip顯示點(diǎn)坐標(biāo)。
三、代碼實(shí)現(xiàn)
繼承自QCustomPlot組件的MFMCustomPlot.h實(shí)現(xiàn)代碼如下:
class MFMCustomPlot : public QCustomPlot
{
Q_OBJECT
public:
MFMCustomPlot(int nIndex,QWidget *parent = 0);
~MFMCustomPlot();
protected:
void mouseMoveEvent(QMouseEvent *e);
};
繼承自QCustomPlot組件的MFMCustomPlot.cpp的實(shí)現(xiàn)代碼如下:
void MFMCustomPlot::mouseMoveEvent(QMouseEvent *e)
{
/* 獲取光標(biāo)位置 */
int x_pos = e->pos().x();
int y_pos = e->pos().y();
/* 轉(zhuǎn)化為坐標(biāo)系位置 */
double xv = this->xAxis->pixelToCoord(x_pos);
double yv = this->yAxis->pixelToCoord(y_pos);
QString str;
str = QString("\nx:%1\ny:%2").arg(QString::asprintf("%.3f", xv)).arg(QString::asprintf("%.3f",yv));
QToolTip::showText(cursor().pos(), str, this);
}
實(shí)現(xiàn)效果
?文章來源地址http://www.zghlxwxcb.cn/news/detail-514999.html文章來源:http://www.zghlxwxcb.cn/news/detail-514999.html
?
到了這里,關(guān)于【Qt】QCustomPlot組件跟隨鼠標(biāo)顯示xy軸坐標(biāo)位置的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!