如何在Qt中的qgraphicstractem中制作ticker

how to make ticker in qgraphicsrectitem in Qt

本文关键字:ticker qgraphicstractem 中的 Qt      更新时间:2023-10-16

如何在Qt中的qgraphicstractitem中制作ticker我试着跟踪

rect = new QGraphicsRectItem;
    text = new QGraphicsTextItem;
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    rect = scene->addRect(0,0,500,500);
    text->setPlainText("HELLO from constructor");
    scene->addItem(text);
    QFont font;
    fm = new QFontMetrics(font);
    ntextWidth = fm->width(text->toPlainText());
    text->setPos(0,rect->boundingRect().height()/2);
    ntextHeight = fm->height();
    QString strtxt = fm->elidedText(text->toPlainText(), Qt::ElideRight, ntextWidth-50, Qt::TextShowMnemonic);
    qDebug() << "Here Lenght in pixels:" << ntextWidth <<"====" <<ntextHeight <<"-----"<<strtxt.contains("…");
    connect(&timer, SIGNAL(timeout()),this,SLOT(update()));
    timer.start(5);
}
GraphicsTicker::~GraphicsTicker()
{
    delete ui;
}
void GraphicsTicker::update()
{
    text->setPos(text->x()+1,text->y());
    if(text->x()==rect->boundingRect().width()-ntextWidth)
    {
        ntextWidth = ntextWidth-1;
        strNewText = fm->elidedText(text->toPlainText(),Qt::ElideRight,ntextWidth).remove("…");
        text->setPlainText(strNewText);
    }
    if(text->x() == rect->boundingRect().width()-20)
    {
        text->setPlainText("HELLO from constructor");
        ntextWidth = fm->width(text->toPlainText());
        text->setPos(0,text->y());
    }

为此,我使用了qgraphicstextitem和qgraphicstrectitem。我移动的文本取决于使用计时器的矩形的位置。但它并不像Ticker那样完美请给我智能解决方案

提前感谢

我已经在Qt中完成了ticker。我只是简单地使用了Qable,使用它我旋转了上面的文本,然后我在Qgraphicscene中用Qgraphicproxywidget添加了这个标签。谢谢大家。

您可以使用Qpropertymation制作ticker,只需将Qable添加到小部件中,并设置其开始值和结束值,使其看起来像一个正在移动的ticker。对于标签的这种"几何形状",考虑移动