You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Qt Widget开发中QtCharts库找不到、无法引入的问题?

Fixing "QtCharts file not found" Errors in Qt Widget/C++ Bar Chart Project

Hey there! Let's break down why you're hitting these errors and fix them step by step. The key thing to note here is that you're working on a Widget/C++ project, not a QML project—so the QML import instructions from the docs don't apply to you (that's why you can't find a .qml file, you don't need one!).

Step 1: Double-Check Your .pro File Configuration

You added charts to your QT line, but let's make sure it's properly structured and that you've refreshed the project configuration:

  • Update your .pro file to explicitly include widgets and charts together (since you're using Qt 5.11, which requires widgets for desktop apps):
    QT += core gui widgets charts
    
  • After modifying the .pro file, run qmake! In Qt Creator, right-click your project in the sidebar and select Run qmake—this tells Qt to regenerate the build files with the new module included. Skipping this step is a super common mistake that leads to "file not found" errors.

Step 2: Fix Header Includes & Namespace Usage

Qt Charts headers are under the QtCharts namespace, so you need to either:

Option A: Use Fully-Qualified Header Paths

Include each chart component with the QtCharts/ prefix in your main.cpp/mainwindow.cpp:

#include <QtCharts/QBarSet>
#include <QtCharts/QBarSeries>
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtCharts/QLegend>
#include <QtCharts/QBarCategoryAxis>

Option B: Add the Namespace to Your Code

Add this line at the top of your .cpp files (after includes) to avoid typing QtCharts:: every time:

using namespace QtCharts;

Just make sure this comes after your includes so the compiler recognizes the namespace.

Step 3: Verify Qt Charts is Installed

If you still get errors, check if you actually have the Qt Charts module installed with your Qt 5.11 setup:

  • Open the Qt Maintenance Tool (it should be in your Qt installation folder).
  • Select Add or Remove Components, then expand your Qt 5.11 installation.
  • Look for Qt Charts under the Qt 5.11.x -> Additional Libraries section—make sure it's checked, then proceed with the installation if it wasn't already.

Step 4: Cross-Reference the Example Code

The official bar chart example for Widgets uses these exact steps. Let's confirm your main.cpp matches the structure:

For example, the core chart setup code should look something like this (with the namespace or fully qualified names):

QBarSet *set0 = new QBarSet("Jane");
*set0 << 1 << 2 << 3 << 4 << 5;

QBarSeries *series = new QBarSeries();
series->append(set0);

QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple Bar Chart");

QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);

// Set chartView as central widget in your MainWindow
ui->centralWidget->layout()->addWidget(chartView);

Final Checks

  • Clean your project (Build -> Clean All)
  • Rebuild from scratch (Build -> Rebuild All)
  • Make sure you're not mixing up QML and Widget code—your project is a Widget app, so stick to the C++/Qt Widgets version of the example.

That should resolve the "QtCharts file not found" errors and get your bar chart running!

内容的提问来源于stack exchange,提问作者Limas Nursalim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:06:48