- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
count()"错误?-6ren"> count()"错误?-我开始使用QtCharts在我的申请中。我正在考虑的图表是一个折线图,使用对象 QChart和 QLineSeries .由于所有点都是动态添加的,因此我使用信号/槽系统来更新图表: QLineSer-6ren">
我开始使用QtCharts
在我的申请中。我正在考虑的图表是一个折线图,使用对象 QChart
和 QLineSeries
.由于所有点都是动态添加的,因此我使用信号/槽系统来更新图表:
QLineSeries* serie = new QLineSeries(this);
connect(serie, SIGNAL(pointAdded(int)), this, SLOT(onPointAdded(int)));
void MyChart::onPointAdded(int index) {
// Delete the first items if the number of points has reached a threshold
while (serie->points().length() >= threshold)
serie->remove(0);
}
函数 onPointAdded
在 serie
(一个 QLineSeries
对象)中添加一个点时被调用。我给出的代码片段删除了 serie
中的第一个点,例如图中的点数始终是固定的(开头除外)。
当我在 Release
中运行这段代码时,没有问题。但是,当我在 Debug
上运行它并且点数达到阈值时,我收到以下错误消息:
此对话框不会停止程序,但每次添加一个点(并达到阈值)时,都会在前一个对话框之上出现一个新对话框。
以下是重现错误的最少代码:
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QChart>
#include <QLineSeries>
#include <QMainWindow>
#include <QValueAxis>
#include <QtCharts/QChart>
#include <QtCharts/QLineSeries>
QT_CHARTS_USE_NAMESPACE
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QChart* chart = nullptr;
QLineSeries* serie = nullptr;
int threshold = 5;
private slots:
void onAddPointButtonClicked();
void onPointAdded(int index);
};
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
chart = new QChart;
serie = new QLineSeries(this);
connect(ui->bt_addPoint, SIGNAL(clicked()), this, SLOT(onAddPointButtonClicked()));
connect(serie, SIGNAL(pointAdded(int)), this, SLOT(onPointAdded(int)));
chart->legend()->hide();
chart->addSeries(serie);
ui->graphicsView->setChart(chart);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::onAddPointButtonClicked() {
serie->append(0, 1);
}
void MainWindow::onPointAdded(int index) {
while (serie->points().length() >= threshold)
serie->remove(0);
}
我使用UI Form 来生成图形界面。此接口(interface)包含一个 QChartView
和一个 QPushButton
(动态添加点)。
我的Qt版本是5.11.2,这个bug是用MSVC 2017 64-bits产生的。使用 QChart
需要插件 QtCharts , QChartView
和 QLineSeries
.
我想知道是否可以解决此问题或禁用 Qt 调试对话框消息。
最佳答案
这不是错误,而是插槽 MainWindow::onPointAdded
(在您的代码中)和 XYChart::handlePointAdded
顺序的预期结果被执行。这是整个故事:
从错误消息中可以清楚地看出,在文件 xychart.cpp
的第 142 行,检查 index
与 m_series
的计数> 失败。使检查无效的是您的 serie->remove(0);
。原因是您的插槽在进行检查的插槽之前执行,因为您的connect
语句首先出现。问题是:首先要什么?好吧,这是棘手的部分,我不得不承认它确实不是很明显。然而,通过深入挖掘源代码,可以找到问题的根源。路径如下:
chart = new QChart;
在您的代码中实例化一个 QChart
,它又是 instantiates一个 PIMPL QChartPrivate
QChartPrivate
connects in it's constructor ChartDataSet::seriesAdded
到 ChartPresenter::handleSeriesAdded
:
QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), m_presenter, SLOT(handleSeriesAdded(QAbstractSeries*)));
重要 现在将 QLineSeries::pointAdded
连接到 MainWindow::onPointAdded
chart->addSeries(serie);
在您的代码中导致 ChartPresenter::handleSeriesAdded
要执行的插槽,其中调用了 QLineSeriesPrivate::initializeGraphics
:
series->d_ptr->initializeGraphics(rootItem());
在QLineSeriesPrivate::initializeGraphics
LineChartItem
被实例化:
LineChartItem *line = new LineChartItem(q,parent);
LineChartItem
calls其基类 XYChart
的构造函数在其自身构造函数的初始化列表中
重要 现在只有 connect
语句 executed ,这会给你带来麻烦 XYChart::handlePointAdded
当一个点被添加到系列时调用插槽:
QObject::connect(series, SIGNAL(pointAdded(int)), this, SLOT(handlePointAdded(int)));
只关注标记为重要的步骤,两个 connect
语句的顺序就很明显了。这也是调用各个插槽的顺序。
考虑到这个原因,我建议您先将系列添加到图表中,然后然后连接pointAdded
信号,即:
移动
connect(serie, SIGNAL(pointAdded(int)), this, SLOT(onPointAdded(int)));
之后的任何地方
chart->addSeries(serie);
关于c++ - 如何修复 xychart.cpp 中的 "index < m_series->count()"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53025680/
这个问题在这里已经有了答案: Why can templates only be implemented in the header file? (18 个答案) 关闭 7 年前。 我的 .hpp
我想使用 yaml-cpp 发出一个带引号的字符串,所以它看起来像 时间戳:“2011 年 8 月 10 日 01:37:52” 在输出yaml文件中。我该怎么做?谢谢。 最佳答案 YAML::Emi
我理解了模板的概念以及为什么我们需要在头文件中定义模板成员函数。另一种选择是在 cpp 文件中定义模板函数并显式实例化模板类,如下所示。 模板.h #include using namespace
是否可以发出和读取(解析)二进制数据(图像、文件等)?如下所示: http://yaml.org/type/binary.html我如何在 yaml-cpp 中执行此操作? 最佳答案 截至revisi
我尝试查找此内容并使用头文件等得到混合结果。 基本上我有多个 .cpp 文件,其中包含我为使用二叉树而制作的所有函数,BST , 链表等 我不想复制和粘贴我需要的函数,我只想能够做一个: #inclu
我正在发出一个 YAML 文档,如下所示: YAML::Node doc; // ...populate doc... YAML::Emitter out; out << doc; 在节点层次结构的某
这个问题在这里已经有了答案: Access extern variable in C++ from another file (1 个回答) 关闭 4 年前。 考虑以下场景: MyFile.cpp:
所以我在上基础编程课,我们正在学习如何将文件链接在一起。问题是我遇到了一个似乎没有人能够修复的错误。我已经去过我的教授、学生助理和校园里的编程辅助实验室,但运气不佳。 我还在这里搜索了至少 10 篇与
在下面的代码中,我在使用 parser.GetNextDocument(doc); 解析我的 .yaml 文件时遇到了一些问题。经过大量调试后,我发现这里的(主要)问题是我的 for 循环没有运行,因
我们有以下类(class)考试成绩:完成本类(class)的学生中有 75 人参加了考试。我们想知道学生在考试中的表现如何,并给出了 75 名学生的分数。我们想编写一个程序,按以下方式总结和分析结果:
主要.cpp #include #include #include #include "cootie.h" using namespace std; int main() { cout
试图制作电子鸡程序,但编译器抛出未定义的对“Tamagotchi::age()”错误的引用 理想情况下,这段代码会返回电子鸡的年龄,它应该在开始时由类的构造函数初始化为 0。 我显然在某个地方搞砸了,
我一直在开发一个使用 Microsoft Visual Studio 2010 命令提示符编译原始 .cpp 文件并分析其输出的应用程序。我遇到了很多麻烦,网上似乎没有太多关于这个的资料。这是麻烦的代
我试图从另一个 .cpp 文件调用 c++ 函数。我使用了 .h header 。看看下面我做了什么。 我有一个f.h文件: #ifndef PACKAGENAME_ADD_H #define PAC
我在 CPP 中有一个函数,其原型(prototype)如下: char* complexFunction(char* arg1, ...); 我使用 DLLImport 属性从 C# 导入它。问题是
也许这是一个幼稚的问题 - 但有没有办法构建/安装 yaml-cpp,以便在构建包含 yaml.h 的项目时不需要使用 Boost 库 header ? IE:我正在开发一个使用 yaml-cpp 结
我有一个在 .cpp 函数中声明的静态函数,我不能在 header 中声明它,因为它不应该是可见的。我想在同一项目的另一个 .cpp 中重新使用它。 这有可能吗? 最佳答案 这里有两个问题: 这可能吗
我正在使用 php-cpp 为我的 php 代码创建扩展,当我尝试编译 main.cpp 文件的简单结构时,我得到这个错误。这是编译错误: main.cpp:15:5: error: ‘PHPCPP_
我决定将必要的代码减少到显示此错误所需的最低限度。我有一个存在于 hc_list.h 文件中的 STL 列表包装器模板类。完整代码如下: // hc_list.h file #ifndef HC_LI
您好,我目前正在尝试通过 AMQPCPP 将 RabbitMQ 集成到我的 VisualStudio 项目中。我只能使用 Windows PC,这对安装来说是一件很痛苦的事情。我想我能够使用 CMAK
我是一名优秀的程序员,十分优秀!