QT怎么写一个代码QT中鼠标移动到柱状图上的柱子上显示详细信息
时间: 2024-02-28 07:52:58 浏览: 139
3D柱状图js代码,横坐标为年份,直接修改成你需要的年份,纵坐标为显示比例,将你的数据修改为相应的比例即可,兼容主流浏览器
以下是一个简单的实现方法,可以在鼠标移动到柱状图上的柱子时显示一个带有柱子值的提示框:
1. 创建一个QChartView对象,并将其设置为窗口的中心控件。
```c++
QChartView *chartView = new QChartView(this);
setCentralWidget(chartView);
```
2. 创建一个QChart对象,并设置其标题和标签。
```c++
QChart *chart = new QChart();
chart->setTitle("柱状图");
chart->setAnimationOptions(QChart::AllAnimations);
```
3. 创建一个QBarSeries对象,并添加一些QBarSet对象。
```c++
QBarSeries *series = new QBarSeries();
series->setBarWidth(1);
series->setLabelsVisible(true);
QBarSet *set1 = new QBarSet("Set 1");
*set1 << 10 << 20 << 30 << 40 << 50;
series->append(set1);
QBarSet *set2 = new QBarSet("Set 2");
*set2 << 20 << 30 << 40 << 50 << 60;
series->append(set2);
```
4. 将QBarSeries对象添加到QChart对象中,并将QChart对象设置为QChartView对象的图表。
```c++
chart->addSeries(series);
chart->createDefaultAxes();
chartView->setChart(chart);
```
5. 为QChartView对象添加一个鼠标移动事件处理函数,该函数根据鼠标的位置计算柱子的值,并将其显示在提示框中。
```c++
void ChartView::mouseMoveEvent(QMouseEvent *event)
{
QPoint pos = event->pos();
QPointF point = chartView->mapToValue(pos);
if (series) {
QBarSet *set = series->barSets()[int(point.x() + 0.5)];
if (set) {
QString tooltip = QString("Value: %1").arg(set->at(int(point.y() + 0.5)));
QToolTip::showText(pos, tooltip, this);
}
}
}
```
6. 在窗口的构造函数中启用鼠标追踪。
```c++
setMouseTracking(true);
```
完整的代码示例:
```c++
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QValueAxis>
#include <QtCharts/QCategoryAxis>
#include <QMouseEvent>
#include <QToolTip>
QT_CHARTS_USE_NAMESPACE
class ChartView : public QMainWindow
{
Q_OBJECT
public:
ChartView(QWidget *parent = nullptr)
: QMainWindow(parent)
{
QChart *chart = new QChart();
chart->setTitle("柱状图");
chart->setAnimationOptions(QChart::AllAnimations);
QBarSeries *series = new QBarSeries();
series->setBarWidth(1);
series->setLabelsVisible(true);
QBarSet *set1 = new QBarSet("Set 1");
*set1 << 10 << 20 << 30 << 40 << 50;
series->append(set1);
QBarSet *set2 = new QBarSet("Set 2");
*set2 << 20 << 30 << 40 << 50 << 60;
series->append(set2);
chart->addSeries(series);
chart->createDefaultAxes();
QValueAxis *axisX = qobject_cast<QValueAxis *>(chart->axes(Qt::Horizontal).at(0));
QCategoryAxis *axisY = qobject_cast<QCategoryAxis *>(chart->axes(Qt::Vertical).at(0));
axisX->setLabelFormat("%.0f");
axisX->setTickCount(series->count());
axisY->setCategories(QStringList() << "A" << "B" << "C" << "D" << "E");
QChartView *chartView = new QChartView(this);
chartView->setChart(chart);
setCentralWidget(chartView);
series = nullptr;
setMouseTracking(true);
}
private:
void mouseMoveEvent(QMouseEvent *event)
{
QPoint pos = event->pos();
QPointF point = chartView->mapToValue(pos);
if (series) {
QBarSet *set = series->barSets()[int(point.x() + 0.5)];
if (set) {
QString tooltip = QString("Value: %1").arg(set->at(int(point.y() + 0.5)));
QToolTip::showText(pos, tooltip, this);
}
}
}
private:
QChartView *chartView = nullptr;
QBarSeries *series = nullptr;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ChartView w;
w.show();
return a.exec();
}
```
阅读全文