- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 QTreeView 中有一个自定义的 QItemDelegate 绘图文本。在 paint() 中,我从样式中获取单元格的大小。然后,我使用当前单元格的宽度绘制带有自动换行的文本。
在sizeHint()中,我真的只想计算高度。宽度应该是当前单元格的宽度。当用户更改单元格宽度时,sizeHint 会计算自动换行文本的新高度并返回。
问题是我无法像在 paint() 中那样获取 sizeHint() 中的单元格宽度。我正在使用:
style = QApplication.style()
style.subElementRect(QStyle.SE_ItemViewItemText, option).width()
这在 paint() 中有效,但在 sizeHint() 中返回 -1。如何在 sizeHint() 中获取当前单元格宽度?
最佳答案
原文:我使用基类的 sizeHint() 方法来获取单元格大小,然后根据需要修改 QSize,如下所示:
QSize MyDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QSize sz=QItemDelegate::sizeHint(option, index);
int h=sz.height();
< MODIFY h HERE >
sz.setHeight(h);
return sz;
}
似乎工作正常。
编辑:发帖者表示这对他不起作用...所以这里有另一个选项,也许不是最优雅的:向委托(delegate)添加一个 View 成员(我想不到一种从委托(delegate)中获取的方法),并使用模型索引来获取 header 的部分大小。例如:
class MyDelegate : public QItemDelegate
{
public:
<the usual, then add>
void setView(QAbstractItemView* v) { theView=v; }
protected:
QAbstractItemView* theView;
};
并在实现中
QSize MyDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
int theWidth=-1;
// handle other types of view here if needed
QTreeView* tree=qobject_cast<QTreeView*>(theView);
if(tree)
{
qDebug("WIDTH @ %d : %d",index.column(),tree->header()->sectionSize(index.column()));
theWidth=tree->header()->sectionSize(index.column());
}
QSize sz=QItemDelegate::sizeHint(option, index);
if(theWidth>=0)
sz.setWidth(theWidth);
// compute height here and call sz.setHeight()
return sz;
}
唯一剩下要做的就是在您的代码中,在创建委托(delegate)后调用 setView():
MyDelegate* theDelegate=new MyDelegate(...);
theDelegate->setView(theView);
关于python - 如何在 QItemDelegate sizeHint() 中获取 QTreeView 单元格宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8932966/
我正在创建一个小部件,其中包含一个国际象棋 table 和六个按钮,这些按钮显示在国际象棋 table 下方的 2 行中。按钮行比棋盘宽。国际象棋表和按钮位于 QGridLayout 中。 Chess
我有一个 QAbstractItemDelegate 并且在 paint 方法中,我试图从标签中绘制文本。但我看到的问题是 QLabel 的大小提示对于它包含的文本来说总是太小。我怎样才能解决这个问题
我在 QSplitter 中有 QListView 和 QTabWidget。 QListView 正在使用自定义模型和自定义委托(delegate)。在委托(delegate)中,我重新实现了 pa
我有一个 QListView 有很多不同高度的项目。我实现了一个自定义委托(delegate)来绘制项目并将布局模式设置为批处理。 但是,当分配模型时, ListView 会预先为模型中的每个项目请求
我有两个小部件彼此相邻,WidgetA 和 WidgetB,在 QDialog 上,带有水平布局管理器。我正在尝试执行以下尺寸/调整尺寸政策: 对于 WidgetA: 水平方向:宽度应为 900,并且
我有一个启用了自动换行的自定义 QLabel。调整 MyWidget 的大小时,它会换行,但 sizeHint() 仍返回原始高度。 我尝试了这篇文章中的修复:QLabel cutting off t
我正在使用自定义编译的 Qt 库版本 4.8.5 和 MinGW GCC 4.8.2 以及 Qt Creator 3.0.1,我试图在中央小部件下的水平拆分器内设置两个选项卡小部件的 sizeHint
我想在布局中排列一堆 QPushButton,以便它们水平换行(如文本)。我正在尝试在 PySide2 中使用 Qt 示例 FlowLayout。 我发布了一个简单的示例 revision 2 her
背景 我正在 Python 2.7.6 中解析非常大的文本文件(30GB+)。为了稍微加快这个过程,我将文件分成 block ,并使用多处理库将它们分配给子进程。为此,我在主进程中迭代文件,记录要分割
我从 Swift 移植了一个带有可变高度单元格的 TableView 实现。但在我的 Xamarin/ReactiveUI 实现中,单元格不会在内容增长时调整大小(即显示可选标签时)。请注意,单元格会
来自 Rust 标准库 implementation of unzip : fn unzip(self) -> (FromA, FromB) where FromA: Default + Ex
因此,我使用 createWindowContainer 将自定义 QWindow 包装在一个小部件中。默认情况下,这会给出一个无效的大小提示 (-1),因为 QWindow 不在布局中。此外,QWi
我在 QTreeView 中有一个自定义的 QItemDelegate 绘图文本。在 paint() 中,我从样式中获取单元格的大小。然后,我使用当前单元格的宽度绘制带有自动换行的文本。 在sizeH
我有QTableView使用 QSqlQueryModel (它从 SQLite 中获取数据)。 有一个QStyledItemDelegate名为 MiniItemDelegate 的子类,我将其用作
我是一名优秀的程序员,十分优秀!