class GraphicsTextItem : public QGraphicsWidget
{
public:
: QGraphicsWidget(parent)
, m_Name(name)
, m_Color(50 + (std::rand() % (255 - 50 + 1)), 50 + (std::rand() % (255 - 50 + 1)), 50 + (std::rand() % (255 - 50 + 1)))
{
setMaximumHeight(12);
setMinimumHeight(12);
}
{
painter->setFont(font);
painter->setPen(m_Color);
painter->drawText(0, 0, m_Name);
}
};
class GraphicsTextItem;
{
private:
const GraphicsTextItem* m_Source;
bool m_SourceGood;
public:
GraphicsCircleItem(const GraphicsTextItem* source)
, m_Source(source)
, m_SourceGood(false)
{
setBrush
(QBrush(m_Source
->m_Color
));
setPen
(QPen(m_Source
->m_Color
));
setRect(0, 0, 20, 20);
}
bool isSourcePositionValid() const { return m_Source->pos().x() != 0.0 || m_Source->pos().y() != 0.0; }
void moveAndShow()
{
if(isSourcePositionValid() && m_SourceGood == false)
{
m_SourceGood = true;
setPos(m_Source->scenePos());
show();
return;
}
else
{
if(!m_SourceGood)
hide();
}
if(m_SourceGood)
{
moveBy(1,6);
}
}
};
int main(int argc, char *argv[])
{
MainWindow w;
QGraphicsWidget* layout_widget = new QGraphicsWidget();
QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(Qt::Vertical);
layout_widget->setLayout(layout);
scene->setBackgroundBrush(Qt::black);
scene->addItem(layout_widget);
scene_view->setScene(scene);
scene_view->setSceneRect(0, 0, 1024, 768);
std::vector<GraphicsTextItem*> source_list;
std::vector<GraphicsCircleItem*> message_list;
for(int i = 0; i < 1000; i++)
{
std::string source = std::string("SourceName_") + std::to_string(i);
GraphicsTextItem
* source_item
= new GraphicsTextItem
(nullptr,
QString(source.
c_str()));
layout->addItem(source_item);
source_list.push_back(source_item);
GraphicsCircleItem* message_item = new GraphicsCircleItem(source_item);
scene->addItem(message_item);
message_list.push_back(message_item);
}
timer.callOnTimeout([&]()
{
static uint64_t counter = 0;
if(counter == 500)
{
for(auto& item : message_list)
{
scene->removeItem(item);
delete item;
}
message_list.clear();
for(auto& item : source_list)
{
GraphicsCircleItem* message_item = new GraphicsCircleItem(item);
scene->addItem(message_item);
message_list.push_back(message_item);
}
counter = 0;
}
for(auto& item : message_list)
{
item->moveAndShow();
}
counter++;
});
timer.start(10);
w.setCentralWidget(scene_view);
w.show();
return a.exec();
}
class GraphicsTextItem : public QGraphicsWidget
{
public:
QString m_Name;
QColor m_Color;
GraphicsTextItem(QGraphicsItem * parent = nullptr, const QString& name = QString())
: QGraphicsWidget(parent)
, m_Name(name)
, m_Color(50 + (std::rand() % (255 - 50 + 1)), 50 + (std::rand() % (255 - 50 + 1)), 50 + (std::rand() % (255 - 50 + 1)))
{
setMaximumHeight(12);
setMinimumHeight(12);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
QFont font("Times", 10);
painter->setFont(font);
painter->setPen(m_Color);
painter->drawText(0, 0, m_Name);
}
};
class GraphicsTextItem;
class GraphicsCircleItem : public QGraphicsEllipseItem
{
private:
const GraphicsTextItem* m_Source;
bool m_SourceGood;
public:
GraphicsCircleItem(const GraphicsTextItem* source)
: QGraphicsEllipseItem()
, m_Source(source)
, m_SourceGood(false)
{
setBrush(QBrush(m_Source->m_Color));
setPen(QPen(m_Source->m_Color));
setRect(0, 0, 20, 20);
}
bool isSourcePositionValid() const { return m_Source->pos().x() != 0.0 || m_Source->pos().y() != 0.0; }
void moveAndShow()
{
if(isSourcePositionValid() && m_SourceGood == false)
{
m_SourceGood = true;
setPos(m_Source->scenePos());
show();
return;
}
else
{
if(!m_SourceGood)
hide();
}
if(m_SourceGood)
{
moveBy(1,6);
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QGraphicsWidget* layout_widget = new QGraphicsWidget();
QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(Qt::Vertical);
layout_widget->setLayout(layout);
QGraphicsScene* scene = new QGraphicsScene(&w);
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
scene->setBackgroundBrush(Qt::black);
scene->addItem(layout_widget);
QGraphicsView* scene_view = new QGraphicsView(&w);
scene_view->setScene(scene);
scene_view->setSceneRect(0, 0, 1024, 768);
std::vector<GraphicsTextItem*> source_list;
std::vector<GraphicsCircleItem*> message_list;
for(int i = 0; i < 1000; i++)
{
std::string source = std::string("SourceName_") + std::to_string(i);
GraphicsTextItem* source_item = new GraphicsTextItem(nullptr, QString(source.c_str()));
layout->addItem(source_item);
source_list.push_back(source_item);
GraphicsCircleItem* message_item = new GraphicsCircleItem(source_item);
scene->addItem(message_item);
message_list.push_back(message_item);
}
QTimer timer;
timer.callOnTimeout([&]()
{
static uint64_t counter = 0;
if(counter == 500)
{
for(auto& item : message_list)
{
scene->removeItem(item);
delete item;
}
message_list.clear();
for(auto& item : source_list)
{
GraphicsCircleItem* message_item = new GraphicsCircleItem(item);
scene->addItem(message_item);
message_list.push_back(message_item);
}
counter = 0;
}
for(auto& item : message_list)
{
item->moveAndShow();
}
counter++;
});
timer.start(10);
w.setCentralWidget(scene_view);
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks