Results 1 to 6 of 6

Thread: QTextEdit. To make selected text as Bold.

  1. #1
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default QTextEdit. To make selected text as Bold.

    There is a QTextEdit, and how to make selected text as Bold?

    P.S. Sorry for probable mistakes in the text.

  2. #2
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTextEdit. To make selected text as Bold.

    use html tags while setting a text.

    textEdit->setText("<html><b>Hello</b</html>");

    go through QtAssistant, checkout "QTextEdit Class".

  3. #3
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QTextEdit. To make selected text as Bold.

    I use textCursor() method for return selected text and then I don't know what to do with it
    In documentation I found QTextFormat Class, but i don't understand how to use it..

  4. #4
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QTextEdit. To make selected text as Bold.

    I found an example in the documentation:
    QTextDocument *document = edit->document();
    QTextCursor cursor(document);

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);

    QTextCharFormat format;
    format.setFontWeight(QFont::Bold);

    cursor.mergeCharFormat(format);//do the text as Bold

  5. #5
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTextEdit. To make selected text as Bold.

    Ok. I just tried a way. But you need to do tweak in a little bit for total functionality.

    connect the signal slot,

    Qt Code:
    1. connect(ui.textEdit, SIGNAL(copyAvailable(bool)), this, SLOT(formatText(bool)));
    To copy to clipboard, switch view to plain text mode 
    and slot goes like this

    Qt Code:
    1. void samplewidget::formatText(bool bSelectState)
    2. {
    3. if(!bSelectState)
    4. return;
    5.  
    6. ui.textEdit->copy();
    7.  
    8. QClipboard* pClip = QApplication::clipboard();
    9. QString str = pClip->text(QClipboard::Clipboard);
    10.  
    11. QString sFullString = ui.textEdit->toPlainText();
    12. int pos = ui.textEdit->textCursor().position();
    13.  
    14. pos -= str.length();
    15. QString str1 = "<html><b>" + str + "</b></html>";
    16. sFullString.replace(pos, str.length(), str1);
    17.  
    18. ui.textEdit->setText(sFullString);
    19. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Dec 2016
    Posts
    1
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11

    Lightbulb Re: QTextEdit. To make selected text as Bold.

    This is the code that is easy for me.

    Qt Code:
    1. format.setFontWeight(QFont::Bold);
    2. ui->plainTextEdit->textCursor().mergeCharFormat(format);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QStandardItem : subpart of the text as bold
    By titoo in forum Qt Programming
    Replies: 5
    Last Post: 1st November 2012, 09:34
  2. set Bold to MessageBox particular Text
    By BalaQT in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2009, 11:53
  3. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45
  4. Replies: 3
    Last Post: 20th November 2007, 07:03
  5. coordinates of selected text in QTextEdit
    By sreedhar in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2006, 17:22

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.