c++ 将一个活动的Qt树小部件项目 Flink 为绿色

jhiyze9q  于 2023-05-20  发布在  Flink
关注(0)|答案(1)|浏览(142)

我想在Qt中的QTreeWidget中 Flink (绿色和白色之间)一个活动项目。这意味着它应该停止 Flink 以前活动过一次的项目。
我尝试了以下方法,但没有按照我想要的那样工作:

**void Dialog::slotTestRunUpdate(QString sTestName)
{**
    QHashIterator<QString, QWidget*> hashIt(treeFormHash); //iterator for testnames and test forms// 
    //e.g treeFormHash.insert("Test 1",    pCTest1FormObj);
    treeFormHash.insert("Test 2",    pCTest2FormObj);
    treeFormHash.insert("Test 3",    pCTest3FormObj);
    treeFormHash.insert("Test 4",    pCTest4FormObj);
    treeFormHash.insert("Test 5",    pCTest5FormObj);//

    QTreeWidgetItemIterator treeIt(ui->treeWidget); //iterator for items in QTreeWidget
    //Store a pointer to the last active item
    QTreeWidgetItem* prevItem = nullptr;

    while(*treeIt) //Search for an existing item with the specified text//
    {
        QTreeWidgetItem* item = *treeIt;
        ui->treeWidget->clearSelection();

        if(item->text(0) == sTestName) {
            qDebug() << "treeIt=" << item->text(0) << "\r\n";

            //A timer to flash between green and white//
            QTimer *timer = new QTimer(this); //a timer object

            timer->setInterval(1000); //interval of 1s
            timer->start(); //start timer

            //When the timer times out, it toggles the background color of the active item between green and white.
            //The timer is connected to a lambda function that captures the item variable by value.
            //The lambda function checks the current background color of the item and switches it to the opposite color.
            QObject::connect(timer, &QTimer::timeout, [=]() mutable{
            if (item->background(0).color() == Qt::green) {item->setBackground(0, Qt::white);}
                else { item->setBackground(0, Qt::green); }
            });

            //Stop flashing the last active item
            if ((prevItem != nullptr) && (prevItem != item)) { prevItem->setBackground(0, Qt::white); timer->stop(); delete timer;}

            ui->treeWidget->setCurrentItem(item);
            item->setSelected(true);
            ui->verticalLayout->itemAt(0)->widget()->setVisible(false);
            
            while(hashIt.hasNext()) {
                hashIt.next();
                //qDebug() << "i: " << hashIt.key() << " " << hashIt.value();
                if(hashIt.key() == sTestName) {
                    ui->verticalLayout->insertWidget(0, hashIt.value());
                    ui->verticalLayout->itemAt(0)->widget()->setVisible(true); break;} }

            prevItem = item;
            return;}
        treeIt++;}   
**}**
yqkkidmi

yqkkidmi1#

我理解你想达到的效果,但我只能提供一个一般情况下的例子,你必须调整它以满足你的需要。
下面是实现:

//create a basic QTreeWidget and populate it
QTreeWidget *treeWidget = new QTreeWidget();
treeWidget->setColumnCount(1);
QList<QTreeWidgetItem *> items;
for (int i = 0; i < 10; ++i)
{
    items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(QString("item: %1").arg(i))));
}
treeWidget->insertTopLevelItems(0, items);

treeWidget->setMinimumSize(800,600);
treeWidget->show();

//use 2 timers, t is to iterate through QTreeWidgetItems
//f is for color flashing
QTimer *t = new QTimer();
QTimer *f = new QTimer();

//select the first QTreeWidgetItem
treeWidget->setCurrentIndex(treeWidget->indexAt(QPoint(0,0)));
treeWidget->currentItem()->setFlags(Qt::ItemIsEnabled);
QTreeWidgetItem *i = treeWidget->currentItem();

//start its animation
treeWidget->connect(f,&QTimer::timeout,[i]()
{
        if(i->background(0).color()==Qt::white)
            i->setBackground(0,QBrush(Qt::green));
        else
            i->setBackground(0,QBrush(Qt::white));
});
f->start(500);

//get into the loop, you can see there are 2 connects, the animation is within the iteration
treeWidget->connect(t,&QTimer::timeout,[treeWidget,f]()
{
    //reset the previous item
    treeWidget->currentItem()->setBackground(0, QBrush());
    //disconnect its animation's timer
    f->disconnect();

    //select the next item, I'm looping through 10 of them
    int next = (treeWidget->currentIndex().row()+1)%10;
    treeWidget->setCurrentIndex(treeWidget->currentIndex().siblingAtRow(next));

    //get the current item
    QTreeWidgetItem *item = treeWidget->currentItem();

    //and pass it to the animation lambda
    treeWidget->connect(f,&QTimer::timeout,[item]()
    {
        if(item->background(0).color()==Qt::white)
            item->setBackground(0,QBrush(Qt::green));
        else
            item->setBackground(0,QBrush(Qt::white));
    });

    //prevent the item from being highlighted without disabling it
    //if you don't, the animation will not be visible
    treeWidget->currentItem()->setFlags(Qt::ItemIsEnabled);
    //start the animation
    f->start(500);
});
//start the iteration
t->start(3000);

它看起来是这样的:

相关问题