Qt:使用不同颜色的堆叠barchart [英] Qt: making a stacked barchart with different colors

查看:1092
本文介绍了Qt:使用不同颜色的堆叠barchart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Qt中创建一个时间轴,其中不同的颜色代表不同的任务。
最终,它应该看起来有点像这样(但只有一行);





现在任何人都有任何想法这没有安装额外的库,但只是与QPaint?
其代表的数据存储在自定义结构中。



欢迎任何帮助。



Thx!

解决方案

你似乎需要类似下面的代码,虽然请注意它只是伪代码,而且确切的细节很大程度上取决于您如何从您的数据结构中获取尚未与我们共享的数据。

 。 .. 
QPainter * painter = new QPainter(MyPaintDevice); // MyPaintDevice可以是'this'
QPen pen(Qt :: gray,2);
painter-> setPen(pen);
int currentX = 0;
const int currentY = 0;
const int height = hardCodedValue; //来自一些静态数据初始化

foreach(const设置和设置,settingsList){
QBrush brush(QColor(settings.colorString()));
painter-> setBrush(brush);
painter-> drawRect(currentX,currentY,settings.width(),height);
currentX + = settings.width();
}
...

诚然, QML而不是旧的 QPainter 引擎的几个原因。这是硬件加速的这些天,而不是软件光栅化作为 QPainter 方法,但可能更重要的是:它是多少更简单。



您需要查看本机Rect qml元素,以及可能的中继器。 p>

I'm trying to make a timeline in Qt, where a different color stands for a different task. Ultimately, it should look a bit like this (but only one line);

Now anyone got any ideas how to do this without installing extra libraries, but just with QPaint? The data it is representing is stored in a self-defined structure.

Any help is welcome.

Thx!

解决方案

You seem to need something like the code below, although please note that it is just pseudo code, and the exact details depend a lot on how you get the data from your datastructure which you have not yet shared with us.

...
QPainter *painter = new QPainter(MyPaintDevice); // MyPaintDevice could be even 'this'
QPen pen(Qt::gray, 2);
painter->setPen(pen);
int currentX = 0;
const int currentY = 0;
const int height = hardCodedValue; // Coming from some static data initialization

foreach (const Settings& settings, settingsList) {
    QBrush brush(QColor(settings.colorString()));
    painter->setBrush(brush);
    painter->drawRect(currentX, currentY, settings.width(), height);
    currentX += settings.width();
}
...

Admittedly, you would be better off going for QML rather than the old QPainter engine for several reasons. That is hardware accelerated these days rather than software rasterization as the QPainter approach, but probably more importantly for you: it is lotta simpler.

You would need to look into the native Rect qml element, and probably the Repeater.

这篇关于Qt:使用不同颜色的堆叠barchart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆