Results 1 to 3 of 3

Thread: Inverted QTextEdit

  1. #1
    Join Date
    Mar 2007
    Posts
    57
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Inverted QTextEdit

    Hi all,

    I've taken an example from Qt labs (http://labs.trolltech.com/blogs/2009...e-in-qwebview/), trying to apply this to a QTextEdit.

    Ultimately, this should give me a QTextEdit with all colors reversed, which I find easier on the eyes

    Anyway, problem with this code is that after changing the setting (press F3), it should repaint the entire widget, but I can not get it to do so. I've tried both update() and repaint().

    Any help would be appreciated!

    Qt Code:
    1. /****************************************************************************
    2. **
    3. ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4. ** Contact: Qt Software Information (qt-info@nokia.com)
    5. **
    6. ** This file is part of the Graphics Dojo project on Qt Labs.
    7. **
    8. ** This file may be used under the terms of the GNU General Public
    9. ** License version 2.0 or 3.0 as published by the Free Software Foundation
    10. ** and appearing in the file LICENSE.GPL included in the packaging of
    11. ** this file. Please review the following information to ensure GNU
    12. ** General Public Licensing requirements will be met:
    13. ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
    14. ** http://www.gnu.org/copyleft/gpl.html.
    15. **
    16. ** If you are unsure which license is appropriate for your use, please
    17. ** contact the sales department at qt-sales@nokia.com.
    18. **
    19. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    20. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    21. **
    22. ****************************************************************************/
    23.  
    24. #include <QtCore>
    25. #include <QtGui>
    26.  
    27. #if QT_VERSION < 0x0040500
    28. #error You need Qt 4.5 or newer
    29. #endif
    30.  
    31. class NightModeView : public QTextEdit
    32. {
    33. Q_OBJECT
    34.  
    35. public:
    36.  
    37. NightModeView(QWidget *parent = 0): QTextEdit(parent), invert(true) {
    38. connect(this, SIGNAL(titleChanged(const QString&)), SLOT(setWindowTitle(const QString&)));
    39. }
    40.  
    41. private:
    42.  
    43. bool invert;
    44.  
    45. void paintEvent(QPaintEvent *event) {
    46. QTextEdit::paintEvent(event);
    47. if (invert) {
    48. QPainter p(viewport());
    49. p.setCompositionMode(QPainter::CompositionMode_Difference);
    50. p.fillRect(rect(), Qt::white);
    51. p.end();
    52. }
    53. }
    54.  
    55. void keyPressEvent(QKeyEvent *event) {
    56. if (event->key() == Qt::Key_F3) {
    57. invert = !invert;
    58. update();
    59. event->accept();
    60. return;
    61. }
    62. QTextEdit::keyPressEvent(event);
    63. }
    64.  
    65. };
    66.  
    67. #include "webnightmode.moc"
    68.  
    69. int main(int argc, char **argv)
    70. {
    71. #ifdef Q_WS_X11
    72. QApplication::setGraphicsSystem("raster");
    73. #endif
    74.  
    75. QApplication app(argc, argv);
    76.  
    77. NightModeView w;
    78. w.show();
    79.  
    80. QMessageBox::information(&w, "Hint", "Use F3 to toggle night mode");
    81.  
    82. return app.exec();
    83. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2007
    Posts
    57
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Inverted QTextEdit

    If its any help, if I replace the
    Qt Code:
    1. update();
    To copy to clipboard, switch view to plain text mode 

    in the code above with
    Qt Code:
    1. hide ();
    2. show ();
    To copy to clipboard, switch view to plain text mode 

    it does repaint the entire widget! However, that's obviously a dirty hack and not what I want.

  3. #3
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Inverted QTextEdit

    QTextEdit is a QAbstractScrollArea. A ScrollArea delegates the painting of the content to its viewport. So use:

    Qt Code:
    1. viewport()->update();
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to shentian for this useful post:

    TMan (5th August 2009)

Similar Threads

  1. QTextEdit Performance handling large paragraphs
    By netuno in forum Qt Programming
    Replies: 14
    Last Post: 1st September 2010, 22:58
  2. Printing Overlay and QTextEdit
    By jgrauman in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2009, 20:47
  3. Drawing on QTextEdit
    By jgrauman in forum Qt Programming
    Replies: 3
    Last Post: 7th February 2009, 09:40
  4. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  5. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03

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.