我有一个QCompleter
和一个设置为QStringListModel
的QCompleter
。现在,如何在设置为StringList
的QStringListModel
的项目之间画一条线作为分隔符。最后,QCompleter
将设置为QLineEdit
。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9mNVZacS5wbmcifQ==” alt =“在此处输入图像说明”>
使用委托类型为小部件创建具有新类型和自定义委托的自定义模型,您可以在您的新类型中绘制任何东西,并允许默认类型在父对象中绘制...see this example for combobox
好,所以我想提供的细节比我最初的提示要多,因为我认为这已经足够了。我担心人们会开始提出进一步的要求,所以去了:
#include <QItemDelegate>
#include <QSize>
#include <QModelIndex>
#include <QStyleOptionViewItem>
class CompleterSeparatorDelegate : public QItemDelegate
{
Q_OBJECT
public:
CompleterSeparatorDelegate(QObject *parent = 0);
~CompleterSeparatorDelegate();
protected:
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const;
};
#include "completerseparatordelegate.h"
#include <QPainter>
#include <QString>
CompleterSeparatorDelegate::CompleterSeparatorDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
CompleterSeparatorDelegate::~CompleterSeparatorDelegate()
{
}
void CompleterSeparatorDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.data(Qt::AccessibleDescriptionRole).toString() == QLatin1String("separator"))
{
painter->setPen(Qt::red);
painter->drawLine(option.rect.left(), option.rect.center().y(), option.rect.right(), option.rect.center().y());
} else {
QItemDelegate::paint(painter, option, index);
}
}
QSize CompleterSeparatorDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QString type = index.data(Qt::AccessibleDescriptionRole).toString();
if(type == QLatin1String("separator"))
return QSize(0, 2);
return QItemDelegate::sizeHint( option, index );
}
#include "completerseparatordelegate.h"
...
listView->setItemDelegate(new CompleterSeparatorDelegate);
completer->setPopup(listView);
...
[我还没有尝试编译此代码,因为我只是以原始文本的形式即时编写它,但是主要思想基本上是来自here,并稍有改动。