#pragma once
#include
#include
#include
#include
#include
#include
class WaterfallWidget : public QWidget {
Q_OBJECT
public:
explicit WaterfallWidget(QWidget* parent = nullptr) :
QWidget(parent),
m_cols(10),
m_rows(30),
m_data(m_cols * m_rows, 0),
m_pixmap(QSize(m_cols, m_rows)),
m_timer(new QTimer(this))
{
// 设置定时器,控制瀑布图的刷新速度
m_timer->setInterval(100);
connect(m_timer, &QTimer::timeout, this, &WaterfallWidget::onTimeout);
QVBoxLayout* layout = new QVBoxLayout();
QLabel* label = new QLabel();
layout->addWidget(label);
setLayout(layout);
m_label = label;
}
~WaterfallWidget() override {
delete m_timer;
}
void start() {
m_timer->start();
}
void stop() {
m_timer->stop();
}
private:
void paintEvent(QPaintEvent* event) override {
QPainter painter(this);
painter.drawPixmap(rect(), m_pixmap);
}
void onTimeout() {
// 更新数据
for (int i = 0; i < m_cols; ++i) {
int index = i * m_rows;
int value = m_data[index];
for (int j = 0; j < m_rows - 1; ++j) {
m_data[index + j] = m_data[index + j + 1];
}
m_data[index + m_rows - 1] = value + rand() % 10 - 5;
}
// 更新图像
QPixmap oldPixmap = m_pixmap;
QPainter painter(&m_pixmap);
painter.fillRect(m_pixmap.rect(), Qt::black);
QColor color(0, 255, 0);
for (int i = 0; i < m_cols; ++i) {
int index = i * m_rows;
for (int j = 0; j < m_rows; ++j) {
int value = m_data[index + j];
color.setAlpha(qMax(0, qMin(value, 255)));
painter.setPen(color);
painter.drawPoint(i, m_rows - 1 - j);
}
}
if (oldPixmap.size() != m_pixmap.size()) {
m_label->setFixedSize(m_pixmap.size());
}
m_label->setPixmap(m_pixmap);
update();
}
private:
const int m_cols;
const int m_rows;
QVector m_data;
QPixmap m_pixmap;
QTimer* m_timer;
QLabel* m_label;
};
一个Qt/C++ Demo,用于实现一个用于显示瀑布图的控件效果
2023-04-23 浏览:
标签ad报错:该广告ID(7)不存在。
Code Demo
- 热门标签
-
- 最新发布
-
- Qt 信号槽与原编译系统(2024-06-04)
- Qt信号槽关联中使用普通变量关联(包含不修改和修改槽内数据两种)、使用引用变量关联对(2023-05-05)
- 一个qt/c++ demo,用于显示 Koch Snowflake (科赫雪花) 分(2023-04-24)
- 一个qt/c++ demo,用于显示 Peano Curve(皮亚诺曲线) 分形图(2023-04-24)
- 一个qt/c++ demo,用于显示 Apollonian Gasket(阿波罗尼恩(2023-04-24)
- 一个qt/c++ demo,用于显示 Terdragon Curve(三头龙曲线)(2023-04-24)
- 一个qt/c++ demo,用于显示 Spiral Fractal(螺旋分形) 分形(2023-04-24)
- 一个qt/c++ demo,用于显示 Sierpinski Tetrahedron((2023-04-24)
- 一个qt/c++ demo,用于显示 Gasket Fractal(镂垫分形) 分(2023-04-24)
- 一个qt/c++ demo,用于显示 Hexagonal Gosper Curve(2023-04-24)
- 半年热点
-
-
一个Qt/C++ Demo,用于实现一个用于显示3D瀑布图的控件效果
浏览: 1067
-
一个qt/c++ demo,用于显示分星图
浏览: 1005