Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make a QTextEdit or QTextBrowser blink

How to make a QTextEdit or QTextBrowser blink

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deepnguyen
    wrote on last edited by
    #1

    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. :(

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Use a timer, not a loop. See [[doc:QTimer]].

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deepnguyen
        wrote on last edited by
        #3

        Thank you. However can you be more specify? How can i actually time the color change with the timeout() signal?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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 sequence

          QChar 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.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deepnguyen
            wrote on last edited by
            #5

            thank you, this helped me a lot

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nasit.bhavin
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hardcodes.de
                wrote on last edited by
                #7

                I think it would be best to open up a new thread since these topics are not really related.

                while(!sleep){++sheep;}

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  [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);
                  @

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved