Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-1851

QTextEdit selection gets wrong color after setting the color

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: P3: Somewhat important P3: Somewhat important
    • 6.0.0
    • 4.4.0, 5.5.0, 5.15.4
    • GUI: Text handling
    • None

      Running the following example on Mac, the selection gets a gray color.

      It would be expected that it would have the native selection color.

      On Mac this would be specially seen if going into "System Preferences" and then changing highlight color to orange.

      When deselecting the area, then reselecting it again it gets the correct color.

      #include <QtGui>
      #include <QDebug>
      
      class CustomWidget : public QPushButton
      { Q_OBJECT
      public:
          CustomWidget(QWidget* parent=0) : QPushButton(parent)
          {
              connect(this,SIGNAL(clicked()), this, SLOT(aSlot())); 
          }
       QTextEdit *edit;
      public slots:
          void aSlot()
          {
              qDebug() << "aSlot invoked";
      
              QTextCursor highlightCursor = edit->textCursor();
      
      
      
        QTextCharFormat plainFormat(highlightCursor.charFormat());
        QTextCharFormat colorFormat = plainFormat;
        colorFormat.setFontWeight(QFont::Bold);
        colorFormat.setBackground(Qt::blue);
           highlightCursor.movePosition(QTextCursor::WordRight,QTextCursor::KeepAnchor);
       
        highlightCursor.mergeCharFormat(colorFormat);
        edit->setTextCursor(highlightCursor);
       
      
          }
      
      protected:
          void paintEvent(QPaintEvent *pe)
          {
              QPushButton::paintEvent(pe);
          }
      
      };
      
      #include "main.moc"
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
      
      
          CustomWidget wid;
          wid.show();
      
      
      
       QTextEdit edit;
       
       wid.edit = &edit;
       
       edit.setHtml("<html><body>hello worldasdf<br><br><br><br><br><br><br><br> asd f asdf asdf  adfs adfs <br><br><br><br>adfsasdfadfs <br><br>adfs fads dfsa</body></html>");
       
       QTimer::singleShot(6000, &wid, SLOT(aSlot()));
       
       
       edit.show();
      
          return app.exec();
      }
      

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            srutledg Shawn Rutledge
            admin Administrator
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes