这是Qt网站上的示例 在这种情况下,匹配字符不是粗体,而其他字符是粗体。我想采取另一种方法:匹配的字符应为粗体,其他字符不应为粗体。我该怎么办?
MyLineEdit.cpp
MyLineEdit::MyLineEdit( QWidget *p_parent ) : QWidget( p_parent )
{
............
m_completer = new Completer();
m_lineEdit = new QLineEdit;
m_lineEdit->setObjectName( "lineEdit" );
connect( m_lineEdit, &LineEdit::textChanged, this, &MyLineEdit::setTextForFilter );
}
void MyLineEdit::setTextForFilter( const QString &p_text )
{
m_completer->setTextForFilter( p_text );
}
Completer.cpp
Completer::Completer( QWidget *p_parent ) : QWidget( p_parent )
{
.........
QGridLayout *tableViewLayout = new QGridLayout( this );
m_caption = new Label;
tableViewLayout->addWidget( m_caption );
m_table = new QTreeView();
m_table->setItemDelegate( new CompleterDelegate() );
m_sourceModel = new CompleterModel( this );
m_proxyModel = new CompleterProxyModel( this );
m_proxyModel->setSourceModel( m_sourceModel );
m_table->setModel( m_proxyModel );
tableViewLayout->addWidget( m_table );
}
void Completer::setTextForFilter( const QString &p_text )
{
QRegExp regExp( p_text, Qt::CaseInsensitive, QRegExp::RegExp );
m_proxyModel->setFilterRegExp( regExp );
}
CompleterModel.cpp
QVariant CompleterModel::data( const QModelIndex &p_index, int p_role ) const
{
int row = p_index.row();
int col = p_index.column();
switch ( p_role )
{
case Qt::DisplayRole:
if ( col < m_completerData.rowData().at( row ).columnCount() )
{
return m_completerData.rowData().at( row ).columnData( col ).first;
}
break;
case Qt::TextAlignmentRole:
return Qt::AlignLeft;
case Qt::UserRole:
return QVariant( static_cast<int>( m_completerData.rowData().at( row ).type() ) );
}
return QVariant();
}
CompleterProxyModel.cpp
bool CompleterProxyModel::filterAcceptsRow( int p_sourceRow, const QModelIndex &p_sourceParent ) const
{
QModelIndex index0 = sourceModel()->index( p_sourceRow, 0, p_sourceParent );
QModelIndex index1 = sourceModel()->index( p_sourceRow, 1, p_sourceParent );
return ( sourceModel()->data( index0 ).toString().contains( filterRegExp() ) || sourceModel()->data( index1 ).toString().contains( filterRegExp() ) );
}
答案 0 :(得分:0)
您将需要一个自定义委托并进行自己的渲染。不幸的是,开箱即用不支持使用富文本格式。