Question

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);

stacked

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!

Was it helpful?

Solution

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.

OTHER TIPS

You can subclass Qwidget, and redefine at least paintEvent(QPaintEvent * event) and resizeEvent(QResizeEvent * event).

I assume your widget hold a initial list of pairs of QRect and QColor. When a resize event occurs you compute a rendering list of QRect with basic relative arithmetic. When a paint even occurs you use this rendering list to fill with the color.

You can even allow to modify colors with mouse events for some neat rendering.

All of these can be done with Qml too, and may be even easier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top