#include "urllineedit.h" #include UrlLineEdit::UrlLineEdit(QWidget *parent) : QLineEdit(parent) { //setStyleSheet("color: #808080"); } void UrlLineEdit::setUrl(const QUrl &url) { QString urlText = url.toString(); QString domain = url.host(); QTextCharFormat f_host; f_host.setFontWeight(QFont::Bold); //f_host.setForeground(QBrush(QColor::fromRgb(255, 255, 255))); QTextLayout::FormatRange fr_tracker; fr_tracker.start = urlText.indexOf(domain); fr_tracker.length = domain.length(); fr_tracker.format = f_host; clear(); clearTextFormat(); setTextFormat(fr_tracker); setText(urlText); } void UrlLineEdit::setTextFormat(const QTextLayout::FormatRange &format) { QList attributes; attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, format.start, format.length, format.format)); QInputMethodEvent ev(QString(), attributes); event(&ev); } void UrlLineEdit::clearTextFormat() { setTextFormat(QTextLayout::FormatRange()); }