I want to create a timeline widget.
I've a Row
class that I insert in a Timeline
widget, using QGraphicsScene
and QGraphicsView
.
If I insert three rows in order, I can show them in the widget.
Then I want to drag them in order to reorder rows. For example, if I have
+----------------------+
| Row 1 |
+----------------------+
| Row 2 |
+----------------------+
| Row 3 |
+----------------------+
If I drag Row 1 between Row2 and Row3 I obtain
+----------------------+
| Row 2 |
+----------------------+
| Row 1 |
+----------------------+
| Row 3 |
+----------------------+
And so on.
When I start dragging the reordering works, but after some dragging (I drag always the first row between the other two) drag stops. I can't drag anymore the first row. Then I start to drag another row and then it works again.
These are the classes that I've used (I can't use only hpp files because of moc files):
Row class:
#ifndef ROW_HPP_
#define ROW_HPP_
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QBrush>
#include <QObject>
const qreal TopZValue{ std::numeric_limits<qreal>::max() };
class Row : public QObject, public QGraphicsItem {
Q_OBJECT
public:
Row();
virtual ~Row() = default;
void setBrush(const QBrush& b);
void setOrigin(int x, int y);
void setHeight(int height);
int getHeight() const;
const QPoint& getOrigin() const;
public:
virtual QRectF boundingRect() const override;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
signals:
void originUpdated(const QPoint& origin);
protected:
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
private:
void drawBackground(QPainter* painter);
private:
QBrush m_background;
int m_height = 0;
int m_width = 0;
QPoint m_origin;
qreal m_zValueWhenDragged = 0.0;
};
#endif // !ROW_HPP_
// CPP file
#include "Row.hpp"
Row::Row() :
QGraphicsItem(nullptr) {
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemSendsGeometryChanges);
}
void Row::setBrush(const QBrush& b) {
m_background = b;
}
void Row::setOrigin(int x, int y) {
m_origin.rx() = x;
m_origin.ry() = y;
setPos(0, 0);
}
void Row::setHeight(int height) {
m_height = height;
}
int Row::getHeight() const {
return m_height;
}
const QPoint& Row::getOrigin() const {
return m_origin;
}
QRectF Row::boundingRect() const {
return QRectF(m_origin.x(), m_origin.y(), m_width, m_height);
}
void Row::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
Q_UNUSED(option)
Q_UNUSED(widget)
drawBackground(painter);
QGraphicsView *view = scene()->views().first();
m_width = view->width();
painter->drawRect(m_origin.x(), m_origin.y(), m_width, m_height);
}
void Row::mousePressEvent(QGraphicsSceneMouseEvent *event) {
m_zValueWhenDragged = zValue();
QGraphicsItem::mousePressEvent(event);
}
void Row::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
setZValue(TopZValue);
QGraphicsItem::mouseMoveEvent(event);
}
void Row::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
setZValue(m_zValueWhenDragged);
QPoint newOrigin(0, m_origin.y() + scenePos().toPoint().y());
m_origin = newOrigin;
emit originUpdated(newOrigin);
QGraphicsItem::mouseReleaseEvent(event);
}
void Row::drawBackground(QPainter* painter) {
auto brush = painter->brush();
auto width = painter->viewport().width();
painter->setBrush(m_background);
painter->drawRect(m_origin.x(), m_origin.y(), width, m_height);
painter->setBrush(brush);
}
Timeline class:
#ifndef TIMELINE_HPP_
#define TIMELINE_HPP_
#include "Row.hpp"
#include <QHBoxLayout>
#include <QMainWindow>
#include <QWidget>
class Timeline : public QWidget {
Q_OBJECT
public:
Timeline(QWidget* parent = nullptr);
virtual ~Timeline() = default;
size_t addRow(Row* row);
size_t getNumberOfRows() const;
private slots:
void setRowOrigin(const QPoint& origin);
private:
void orderRowsOriginsByTheirPosition();
private:
QGraphicsView* m_view;
QGraphicsScene* m_scene;
QHBoxLayout* m_layout;
std::vector<Row*> m_rows;
};
#endif //!TIMELINE_HPP_
// CPP file
#include "Timeline.hpp"
Timeline::Timeline(QWidget* parent) :
QWidget(parent) {
m_view = new QGraphicsView(this);
m_scene = new QGraphicsScene(this);
m_layout = new QHBoxLayout(this);
m_layout->addWidget(m_view);
m_view->setScene(m_scene);
m_view->setAlignment(Qt::AlignTop | Qt::AlignLeft);
}
size_t Timeline::addRow(Row* row) {
m_rows.push_back(row);
m_scene->addItem(row);
orderRowsOriginsByTheirPosition();
connect(row, &Row::originUpdated, this, &Timeline::setRowOrigin);
return getNumberOfRows();
}
size_t Timeline::getNumberOfRows() const {
return m_rows.size();
}
void Timeline::setRowOrigin(const QPoint& origin) {
Q_UNUSED(origin)
orderRowsOriginsByTheirPosition();
}
void Timeline::orderRowsOriginsByTheirPosition() {
int offsetY = 0;
std::sort(m_rows.begin(), m_rows.end(), [] (Row* left, Row* right) { return left->getOrigin().y() < right->getOrigin().y();});
for (auto& it : m_rows) {
it->setOrigin(0, offsetY);
offsetY += it->getHeight();
}
m_scene->update();
m_view->update();
}
MainWindow class:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "Timeline.hpp"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow() = default;
private:
Timeline* m_timeline;
};
#endif // MAINWINDOW_H
// CPP file
#include "MainWindow.hpp"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_timeline(new Timeline(this)) {
setCentralWidget(m_timeline);
setMinimumSize(300, 200);
auto row1 = new Row();
row1->setHeight(40);
m_timeline->addRow(row1);
row1->setBrush(Qt::red);
auto row2 = new Row();
row2->setHeight(30);
m_timeline->addRow(row2);
row2->setBrush(Qt::blue);
auto row3 = new Row();
row3->setHeight(50);
m_timeline->addRow(row3);
row3->setBrush(Qt::green);
}
main.cpp
#include "Row.hpp"
#include "MainWindow.hpp"
#include <QCoreApplication>
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
When I start the program I obtain:
Then I drag the red row between the green and the blue:
Now I can't drag the green row, but if I drag another one in another position, I can drag the green one again.
What I'm doing wrong?
When you change Row::m_origin
you change the value returned by Row::boundingRect()
without calling QGraphicsItem::prepareGeometryChange()
.
But Qt documentation states:
If you want to change the item's bounding rectangle, you must first call prepareGeometryChange(). This notifies the scene of the imminent change, so that it can update its item geometry index; otherwise, the scene will be unaware of the item's new geometry, and the results are undefined (typically, rendering artifacts are left within the view).
Also you could make it with a simpler code.
m_origin
and use QGraphicsItem::pos()
and QGraphicsItem::setPos()
.Row::boundingRect()
can now simply return QRectF(0.0, 0.0, m_width, m_height)
and you call QGraphicsItem::prepareGeometryChange()
only when changing m_width
or m_height
.Row::paint()
This way you can leverage the QGraphicsScene positioning system, without having to handle changes in geometry.
On a side not instead of class Row : public QObject, public QGraphicsItem
you could write class Row : public QGraphicsObject
.