Results 1 to 1 of 1

Thread: QTextCursor - setTextColor()

  1. #1
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTextCursor - setTextColor()

    I've ported my Simple Telnet Client (called Dalamud) from QT3 to QT4.

    Before adding new features (see my previuos post)
    I want to correct one bad behaviuor.

    I've a QTextEdit to display Telnet Ouput , I want it clickable for selecting text, but this way a click changes cursor position for inserting new text (insertPlainText()).
    So I recoded the function for displaying output to reposition the cursor at the end of the document before inserting new Text.
    It works, but I've got a very bad side-effect. Text seems to get One Color and never changes it even if another function calls setTextColor()...

    Probably you need some code to help me better, I don't know how if it's better to choose some lines of code or to attach all the sources.
    Now I try the first option...

    The Output - QTextEdit
    Qt Code:
    1. OutText::OutText(QWidget * parent): QTextEdit(parent)
    2. {
    3. setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    4. //Settaggio Font
    5. QFont Font=currentFont();
    6. Font.setFamily("Courier New");
    7. setCurrentFont(Font);
    8. //Settaggio dei Colori
    9. QPalette Pal;
    10. Pal=palette();
    11. Pal.setColor(QPalette::Base,Qt::black);
    12. setPalette(Pal);
    13. // setPaper(QBrush("black"));
    14. setTextColor(Qt::green);
    15. insertPlainText("STIKAZZI");
    16. setTextColor(Qt::white);
    17. insertPlainText("!");
    18. setReadOnly(TRUE);
    19. }
    20.  
    21. void OutText::writeIn(QString str)
    22. {
    23. QTextCursor Cursor=textCursor();
    24. Cursor.setPosition(document()->toPlainText().length());
    25. setTextCursor(Cursor);
    26.  
    27. insertPlainText(str);
    28. QScrollBar* Vbar;
    29. Vbar=verticalScrollBar();
    30. Vbar->setValue(Vbar->maximum());
    31. }
    To copy to clipboard, switch view to plain text mode 

    The slot that would change Text Color (it's on the parent class of OutText, the instance of OutText it's called OutT)

    Qt Code:
    1. void MainDialog::changeColor()
    2. {
    3. QColor NewColor;
    4. if (Telnet.Color.high)
    5. {
    6. switch(Telnet.Color.foreground)
    7. {
    8. case Foreground::BLACK:
    9. NewColor=Qt::gray;
    10. break;
    11. case Foreground::RED:
    12. NewColor=Qt::red;
    13. break;
    14. case Foreground::GREEN:
    15. NewColor=Qt::green;
    16. break;
    17. case Foreground::YELLOW:
    18. NewColor=Qt::yellow;
    19. break;
    20. case Foreground::BLUE:
    21. NewColor=Qt::blue;
    22. break;
    23. case Foreground::MAGENTA:
    24. NewColor=Qt::magenta;
    25. break;
    26. case Foreground::CYAN:
    27. NewColor=Qt::cyan;
    28. break;
    29. case Foreground::WHITE:
    30. NewColor=Qt::white;
    31. break;
    32. }
    33. }
    34. else
    35. {
    36. switch(Telnet.Color.foreground)
    37. {
    38. case Foreground::BLACK:
    39. NewColor=Qt::black;
    40. break;
    41. case Foreground::RED:
    42. NewColor=Qt::darkRed;
    43. break;
    44. case Foreground::GREEN:
    45. NewColor=Qt::darkGreen;
    46. break;
    47. case Foreground::YELLOW:
    48. NewColor=Qt::darkYellow;
    49. break;
    50. case Foreground::BLUE:
    51. NewColor=Qt::darkBlue;
    52. break;
    53. case Foreground::MAGENTA:
    54. NewColor=Qt::darkMagenta;
    55. break;
    56. case Foreground::CYAN:
    57. NewColor=Qt::darkCyan;
    58. break;
    59. case Foreground::WHITE:
    60. NewColor=Qt::lightGray;
    61. break;
    62. }
    63. }
    64. OutT.setTextColor(NewColor);
    65. }
    To copy to clipboard, switch view to plain text mode 

    Screenshots
    Last edited by Dalamar; 20th February 2006 at 19:50.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.