How to make a QTextEdit or QTextBrowser blink
-
Hi,
I'm a newbie learning how to use Qt, currently making a Qt GUI application.
I'm trying to blink a QTextBrowser (change its color from black to white and vice versa) in a pattern stored in a int array (0 for white and 1 for black)
The interval between each color changes is store in another variable (currently sets to 0.5s).
There is also a button to start the sequence.The problem is when i'm trying to set the QTextBrowser's color when the pushButton is clicked by using
@ui->textBrowser->setStyleSheet("QTextBrowser { background-color: rgb(0, 0, 0);}");@
it only change the color after the whole on_pushButton_clicked() class is finish, which mean even i change the color many times according to the pattern, the QTextBrowser only update it's color once.Also i don't know how add a delay between each color changes.
I'm stuck, please help. :(
-
Thank you. However can you be more specify? How can i actually time the color change with the timeout() signal?
-
You create a slot to advance to the next stage of your blinking sequence.
Say, you define your blinking pattern as a string: ----111---11-1-1-11---111 or something like that, where each character represents a fixed time interval (say, 500 ms). You'd set the timer to that time interval, and on each timeout, advance one step in your blinking sequence. Then, you return to the eventloop to wait for the next timout.
@
//somewhere in your header in the class declaration:
private slots:
void advanceBlinkingSequence();private: //members
QString m_sequenceString;
int m_currentSequenceStep;
@@
//your implementation (cpp file):
MyClass::MyClass(QWidget* parent)
{
m_sequenceString = "----111---11-1-1-11---111";
m_currentSequenceStep = -1;QTimer* timer = new QTimer(this);
timer->setTimeout(500);
connect(timer, SIGNAL(timeout()), this, SLOT(advanceBlinkingSequence()));
timer.start();
advanceBlinkingSequence();
}void advanceBlinkingSequence()
{
QChar currentStep;
if (m_currentSequenceStep > 0)
currentStep = m_sequenceString[m_currentSequenceStep];
}m_currentSequenceStep++; //next step
if (m_currentSequenceStep >= m_sequenceString.count())
m_currentSequenceStep = 0; //reset to beginning of sequenceQChar nextStep = m_sequenceString[m_currentSequenceStep];
if (nextStep != currentStep) {
if (nextStep == '1') {
ui->textBrowser->setStyleSheet("QTextBrowser { background-color: rgb(255, 255, 255);}");
} else if (nextStep == '-') {
ui->textBrowser->setStyleSheet("QTextBrowser { background-color: rgb(0, 0, 0);}");
}
}
}
@//all code is brain-to terminal, of course, and completely untested. Meant for illustration purposes only.
-
thank you, this helped me a lot
-
Hi Andre,
Can you tell me how to change color in QTextBrowser (line by line)? I mean,
This line should be red . .
this line should be black. .Thanks in advance.
-
I think it would be best to open up a new thread since these topics are not really related.
-
[quote author="bhavin.nasit" date="1368784110"]
Can you tell me how to change color in QTextBrowser (line by line)?[/quote]This should give you an hint:
@
QTextEdit::ExtraSelection extraSelection;
extraSelection.format.setBackground( QColor(Qt::black) );
extraSelection.format.setProperty( QTextFormat::FullWidthSelection, true );
extraSelection.cursor = myTextEdit.textCursor();
extraSelection.cursor.clearSelection();QListQTextEdit::ExtraSelection extraSelections;
extraSelections << extraSelection;myTextEdit->setExtraSelections(extraSelections);
@