質問

I have a binary tree implementation, and I am drawing qlabels having the numbers on it representing the nodes of the tree. What I want to do is while i am traversing the tree i want to change the background color of each node when i am on them. but I failed on this job :/ here is my code. please give me a clue on what to do.

void Node::ChangeColor()
{ 
    QPalette pal;
    pal.setColor( lbl->backgroundRole(), QColor(255,0,0) ); // change the color
    lbl->setPalette(pal);
    lbl->setAutoFillBackground(true);
    lbl->update();
    QThread::sleep(1);
    pal.setColor( lbl->backgroundRole(), QColor(255,255,0) ); // change it back 
    lbl->setPalette(pal);
    lbl->setAutoFillBackground(true);
    lbl->update();
}

i also used the stylesheet methods but i couldn't make it work either :/

役に立ちましたか?

解決

void Node::ChangeColor()
{ 
    QPalette pal;
    pal.setColor( lbl->backgroundRole(), QColor(255,0,0) ); // change the color
    lbl->setPalette(pal);
    lbl->setAutoFillBackground(true);
    lbl->repaint();
    QThread::sleep(1);
    pal.setColor( lbl->backgroundRole(), QColor(255,255,0) ); // change it back 
    lbl->setPalette(pal);
    lbl->setAutoFillBackground(true);
    lbl->repaint();
}

by changing updates to repaint methods of labels i solved the problem.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top