- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在 QTreeView
中显示图像以及一些其他数据。为此,我创建了一个 QAbstractItemModel
。在 data
方法中,我为列索引 0 和 1 返回一些字符串,对于第三个我希望返回一个图像。但是,此图像未显示。
当我将图像作为装饰返回时,它显示正常,但我希望将点击监听器添加到将触发某些事件的图像中。我还希望将我的图像定位在 TreeView 的最右侧,这似乎需要一个自定义委托(delegate)。出于这些原因,我为图像创建了一个单独的列。
图像在构造函数中创建如下:mEditImage = QImage(":/images/myImage.png");
我的模型
QVariant MyModel::data(const QModelIndex &index, int role) const {
if (!index.isValid()) {
return QVariant();
}
if(role == Qt::FontRole) {
return fontData(index);
}
if(role == Qt::ForegroundRole) {
return foreGroundData(index);
}
MyModelItem *item = getItem(index);
/*
* Use decoration to display image. Probably needs a
* custom delegate to be able to position the image correctly.
* (http://www.qtcentre.org/threads/49639-decoration-position-and-alignment)
* (https://qt-project.org/forums/viewthread/24493)
*
* Will use extra column for now instead. Might help with click
* listeners as well.
*
* if(role == Qt::DecorationRole && item->parent() != mRootItem) {
return index.column() == 1 ? mEditImage : QVariant();
}*/
if(role == Qt::SizeHintRole) {
return mEditImage.size();
}
if (role == Qt::DisplayRole) {
QString id = QString::number(item->id());
QString name = item->name();
if(item->parent() != mRootItem && index.column() == 2) {
return mEditImage;
}
if(item->parent() == mRootItem){
return index.column() == 0 ? name : "";
} else {
return index.column() == 0 ? id : name;
}
} else if(role == Qt::BackgroundRole) {
return QVariant();
}
我看过这里:
http://www.qtcentre.org/threads/29550-How-do-I-display-a-picture-on-a-QTableView-cell
How to set an image for a row?
我已经尝试将图像更改为 QPixmap
和 QIcon
,还尝试将其嵌入 QLabel
(无法转换到 QVariant
) 没有运气。将图像更改为 QString
显示字符串,因此行/列逻辑看起来很好。删除 SizeHintRole
逻辑也没有任何区别。
任何有助于理解如何在 QTreeView
中显示图像数据的帮助都会有所帮助。我似乎是从错误的方向出发。
干杯。
最佳答案
没关系,图片怎么存。 QPixmap 更适合快速绘图。
QVariant MyModel::data(const QModelIndex &index, int role) const
{
item = itemFromIndex( index ); // Your item implementation
...
case Qt::DisplayRole:
return item->getText();
case Qt::DecorationRole:
return item->getImage();
}
阅读有关角色的 Qt 文档,这很好 - http://doc.qt.io/qt-4.8/qt.html#ItemDataRole-enum
关于c++ - 在 QTreeView 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22093510/
有人知道如何为 QTreeView 项目的子组实现/实现具有不同颜色的 QTreeView 吗? 像这样的东西: 有没有人做过类似的事情,可以给我一个教程或操作方法的链接,或者示例代码也不错。目前我完
在 Qt 文档中 http://doc.qt.io/qt-5/model-view-programming.html#model-subclassing-reference ,据说,如果你的模型是可排
我正在使用 Qt 4.7.0,一个多列的 Qtreeview。 我想要做的是“简单”:我想要一条线增加它的高度 , 当它被选中时。 代表是否足以做到这一点? 我已经通过 QTableView 经历了一
我需要在 QTreeView 中通过拖放来实现行移动,并在行之间显示放置指示器。我想知道是否有一种方法可以覆盖指标绘图,因此它仅针对行之间的所有层次结构显示(不是项目周围的矩形),该行必须与整行一样宽
我现在正在测试 QTreeView 功能,我对一件事感到惊讶。似乎 QTreeView 内存消耗取决于项目数 O_O。这是非常不寻常的,因为这种类型的模型 View 容器只跟踪显示的项目,其余项目都在
我正在尝试让 QTreeView(使用底层 QFileSystemModel)显示目录树。如果我将 RootPath 设置为父目录,那么我会看到所有子目录,但看不到父目录。如果我将 RootPath
我尝试编写一个基于 QTreeView 的可编辑表格,在单元格中包含自动换行的内容,而这些单元格又应该像 MSWord 中表格中的普通单元格一样扩展高度(整个文本可见),但遇到了无法克服的障碍: 首先
我想要一个 TreeView ,它在各个列中显示项目名称、项目描述和两个相关的 bool 值。我首先修改 Editable Tree Mode example ,所以有一个 TreeModel 跟踪一
我想在我的程序中实现一个带有嵌套子级别的树,我正在寻找这两种( View /小部件)中的哪一种最适合我的目标。 我有一个任务完成/错过/失败的天数列表,每个任务都有完成/错过/失败的次数,最后是当天的
我想要我的 QTreeView总是展开所有的项目。在这种情况下,所有展开按钮/装饰都是不必要的,我想摆脱它们。我怎样才能删除所有这些? setRootIsDecorated只会删除第一级的按钮....
我有一个 QTreeView 子类(和 QAbstractItemModel 子类),它有一个漂亮的水平标题。我想添加垂直标题(从左侧向下)以匹配。但与具有单独的垂直 ( setVerticalHea
我想在 Qt 中实现对 QTreeView 对象的手动控制。 (这意味着将对一切进行编程控制,包括导航)到目前为止,我已经在 sibling 中实现了导航/选择。但是,我想建立一个更容易控制的状态,无
我想在 Qt 中实现对 QTreeView 对象的手动控制。 (这意味着将对一切进行编程控制,包括导航)到目前为止,我已经在 sibling 中实现了导航/选择。但是,我想建立一个更容易控制的状态,无
我有需要在 QTreeView 中显示的自定义数据。我从 QAbstractTableModel 派生了我的模型,并制作了自己的 rowCount()、columnCount()、data() 和 h
我的小部件中有一个QTreeView。当在 View 中选择一个项目时,我有 在详细信息窗口中更新一系列信息小部件的信号处理程序 关于所选项目。然后,用户可以编辑项目详细信息并提交 更改回模型。 更改
在下面的代码中,QTreeView 小部件始终占用最大空间,并为 QPushButton 小部件留下最小空间(代码后的图像)。如果 QTreeView 小部件被另一个 QPushButton 替换(当
我正在使用 Qt4,我正在尝试显示带有 QTreeView 的模型。我拥有的模型由多个批处理过程组成,每个批处理都有一些元素,每个元素都有一些子元素(同一批元素具有相同数量的子元素,但我认为这与问题无
我有一个 qt 对象 QTreeView我想切换标签位置。基本上我有一个填充的 TreeView。和功能 columnViewportPosition ( int column ) 第一列返回 0,第
我目前正在尝试制作一个 QTreeView 来显示计算机上文件夹的内容。但是,我遇到了一些奇怪的问题,其中 .和 .. 显示在 TreeView 中,我不希望这种情况发生。我该如何禁用显示 .以及 .
我在从 QTreeWidget 迁移到 QtreeView 时遇到问题。对于 QTreeWidget 来说显而易见且微不足道的事情在 View 中似乎是不可能的。具体来说:我有一个带有 TreeVie
我是一名优秀的程序员,十分优秀!