我有一个类(EditorTagManager)包含一个QTreeWidget。在运行时,树可以包含任意数量的标记项,所有这些项都是可检查的。我试图在QTreeWidgetItems之间添加水平线,以便清楚地表明这些标记是不相关的,并且是相互分离的(每个项都是根级节点)。
从我对这个主题的研究中,我发现控制QtreeWidgetItems外观的唯一有意义的方法是子类QStyledItemDelegate并将委托绑定到QTreeWidget。这是一个抽象的概念,所以我不完全理解。因为我以前从未对Qt对象进行过子类,所以我不确定是否正确。
由于Qt文档没有真正解释如何做到这一点,所以我使用Clementine1.0.1源代码中的setingsdialog.cpp/.h文件作为我的指导/引用,因为Clementine的preferences窗口在其QTreeWidget上使用了类似的分隔符。我正试图从Clementine的代码中反求我自己的解决方案,唯一的问题是Clementine的实现做了我不需要的事情(所以我必须弄清楚什么与我的代码相关,什么与我的代码无关)。这就是我到这一点的原因;我的代码非常类似于Clementine代码(我刚刚更改了委托类名):
下面是udortreemanager.h中当前的委托头声明:
class TagListDelegate : public QWidget
{
public:
TagListDelegate(QObject* parent);
void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
};下面是我在editortreemanager.cpp中的当前委托源:
TagListDelegate::TagListDelegate(QObject *parent) :
TagListDelegate(parent){
}
void TagListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const{
}尽管TagListDelegate::code ()实际上还没有做任何事情,但在尝试更改QTreeWidgetItems的外观之前,我只想让这段代码正常工作。我的目标是暂时保持这件事尽可能简单。
在我告诉QTreeWidget (ui->AvailableTags)使用委托之前,所有内容都编译得很好:
ui->AvailableTags->setItemDelegate(new TagListDelegate(this)); 编译器错误如下:
/home/will/qt_projects/robojournal/ui/editortagmanager.cpp:211:错误:没有调用'QTreeWidget::setItemDelegate(TagListDelegate*)‘的匹配函数
我在这里有点过意不去,所以我非常感谢你帮我弄清楚这件事。
更新(7/30/13):
我的代表班现在看起来是这样的:
来源:
TagListDelegate::TagListDelegate(QStyledItemDelegate *parent) :
TagListDelegate(parent){
}
void TagListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const{
QStyledItemDelegate::paint(painter, option, index);
}标题声明:
class TagListDelegate : public QStyledItemDelegate
{
public:
TagListDelegate(QStyledItemDelegate* parent);
void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
};最新情况(7/31/13)
下面是我的课程现在的样子:
标题:
class TagListDelegate : public QStyledItemDelegate
{
public:
TagListDelegate(QObject* parent);
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
};来源:
TagListDelegate::TagListDelegate(QObject *parent)
: TagListDelegate(parent){
}
QSize TagListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSize ret = QStyledItemDelegate::sizeHint(option, index);
return ret;
}
void TagListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const{
QStyledItemDelegate::paint(painter, option, index);
}发布于 2013-07-30 06:15:32
您没有在代码中子类QStyledItemDelegate。你是在子类QWidget。变化
class TagListDelegate : public QWidget至:
class TagListDelegate : public QStyledItemDelegate别忘了包括标题:
#include <QStyledItemDelegate>https://stackoverflow.com/questions/17936185
复制相似问题