我正在寻找一种方法来突出显示QTreeView的行,而无需重新实现QTreeView子类。
我见过类似的问题:
但他们都使用委托。原因是我在小部件工具中创建搜索文本,该工具浏览所有小部件并查找并突出显示其中的文本。因此,我不能使用委托。
有没有可能的解决方案? 在它上面画一个半透明的物品?
以下是存储小部件的工具代码以及在其中搜索文本的方式。
class GUI_EXPORT QgsSearchHighlightOptionWidget : public QObject
{
Q_OBJECT
public:
/**
* Constructor
* \param widget the widget used to search text into
*/
explicit QgsSearchHighlightOptionWidget( QWidget *widget = nullptr );
/**
* Returns if it valid: if the widget type is handled and if the widget is not still available
*/
bool isValid() { return mWidget && mValid; }
/**
* search for a text pattern and highlight the widget if the text is found
* \returns true if the text pattern is found
*/
bool searchHighlight( const QString &searchText );
/**
* reset the style to the original state
*/
void reset();
/**
* return the widget
*/
QWidget *widget() { return mWidget; }
bool eventFilter( QObject *obj, QEvent *event ) override;
private slots:
void widgetDestroyed();
private:
QPointer< QWidget > mWidget;
QString mStyleSheet;
bool mValid = true;
bool mChangedStyle = false;
std::function < bool( QString )> mTextFound = []( QString searchText ) {Q_UNUSED( searchText ); return false;};
bool mInstalledFilter = false;
};
QgsSearchHighlightOptionWidget::QgsSearchHighlightOptionWidget( QWidget *widget )
: QObject( widget )
, mWidget( widget )
{
if ( qobject_cast<QLabel *>( widget ) )
{
mStyleSheet = QStringLiteral( "QLabel { background-color: yellow; color: blue;}" );
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QLabel *>( mWidget )->text().contains( searchText, Qt::CaseInsensitive );};
}
else if ( qobject_cast<QCheckBox *>( widget ) )
{
mStyleSheet = QStringLiteral( "QCheckBox { background-color: yellow; color: blue;}" );
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QCheckBox *>( mWidget )->text().contains( searchText, Qt::CaseInsensitive );};
}
else if ( qobject_cast<QAbstractButton *>( widget ) )
{
mStyleSheet = QStringLiteral( "QAbstractButton { background-color: yellow; color: blue;}" );
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QAbstractButton *>( mWidget )->text().contains( searchText, Qt::CaseInsensitive );};
}
else if ( qobject_cast<QGroupBox *>( widget ) )
{
mStyleSheet = QStringLiteral( "QGroupBox::title { background-color: yellow; color: blue;}" );
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QGroupBox *>( mWidget )->title().contains( searchText, Qt::CaseInsensitive );};
}
else if ( qobject_cast<QTreeView *>( widget ) )
{
// TODO - style individual matching items
mTextFound = [ = ]( QString searchText )
{
QTreeView *tree = qobject_cast<QTreeView *>( mWidget );
if ( !tree )
return false;
QModelIndexList hits = tree->model()->match( tree->model()->index( 0, 0 ), Qt::DisplayRole, searchText, 1, Qt::MatchContains | Qt::MatchRecursive );
return !hits.isEmpty();
};
}
else
{
mValid = false;
}
if ( mValid )
{
mStyleSheet.prepend( "/*!search!*/" ).append( "/*!search!*/" );
QgsDebugMsgLevel( mStyleSheet, 4 );
connect( mWidget, &QWidget::destroyed, this, &QgsSearchHighlightOptionWidget::widgetDestroyed );
}
}
bool QgsSearchHighlightOptionWidget::searchHighlight( const QString &searchText )
{
bool found = false;
if ( !mWidget )
return found;
if ( !searchText.isEmpty() )
{
found = mTextFound( searchText );
}
if ( found && !mChangedStyle )
{
if ( !mWidget->isVisible() )
{
// show the widget to get initial stylesheet in case it's modified
QgsDebugMsg( QString( "installing event filter on: %1 (%2)" )
.arg( mWidget->objectName() )
.arg( qobject_cast<QLabel *>( mWidget ) ? qobject_cast<QLabel *>( mWidget )->text() : QString() ) );
mWidget->installEventFilter( this );
mInstalledFilter = true;
}
else
{
mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
mChangedStyle = true;
}
}
return found;
}
bool QgsSearchHighlightOptionWidget::eventFilter( QObject *obj, QEvent *event )
{
if ( mInstalledFilter && event->type() == QEvent::Show && obj == mWidget )
{
mWidget->removeEventFilter( this );
mInstalledFilter = false;
// instead of catching the event and calling show again
// it might be better to use a timer to change the style
// after the widget is shown
#if 1
mWidget->show();
mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
return true;
#else
QTimer::singleShot( 500, this, [ = ]
{
mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
mChangedStyle = true;
} );
#endif
}
return QObject::eventFilter( obj, event );
}
void QgsSearchHighlightOptionWidget::reset()
{
if ( mWidget && mValid )
{
if ( mChangedStyle )
{
QString ss = mWidget->styleSheet();
ss.remove( mStyleSheet );
mWidget->setStyleSheet( ss );
mChangedStyle = false;
}
else if ( mInstalledFilter )
{
mWidget->removeEventFilter( this );
mInstalledFilter = false;
}
}
}
void QgsSearchHighlightOptionWidget::widgetDestroyed()
{
mWidget = nullptr;
mValid = false;
}
以下是从对话框中实际注册小部件的代码:
void QgsOptionsDialogBase::registerTextSearchWidgets()
{
mRegisteredSearchWidgets.clear();
for ( int i = 0; i < mOptStackedWidget->count(); i++ )
{
Q_FOREACH ( QWidget *w, mOptStackedWidget->widget( i )->findChildren<QWidget *>() )
{
QgsSearchHighlightOptionWidget *shw = new QgsSearchHighlightOptionWidget( w );
if ( shw->isValid() )
{
QgsDebugMsgLevel( QString( "Registering: %1" ).arg( w->objectName() ), 4 );
mRegisteredSearchWidgets.append( qMakePair( shw, i ) );
}
else
{
delete shw;
}
}
}
}
答案 0 :(得分:1)
阅读文档,我不知道如何直接进行。 以下是一些建议。希望有一个适合你:
1)您可以使用QTreeWidget
代替QTreeView
吗?
有了这个,它应该很容易。
使用item
功能,例如该项目setBackground()
。
2)如何过滤树而不是突出显示?使用setRowHidden()
?
3)如果您还没有,也可以尝试使用QTreeView::keyboardSearch()
并查看其功能。
4)您可以使用select*
并在搜索框中添加下一个/上一个按钮。即你跳过树,选择当前的搜索结果。