- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当鼠标悬停在 QListWidget 项目上时,我试图在该项目周围绘制轮廓。我对 QStyledItemDelegate
进行了子类化并覆盖 paint
占QStyle::State_MouseOver
情况如下:
class MyDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
MyDelegate(QObject *parent = nullptr)
: QStyledItemDelegate(parent){}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QStyledItemDelegate::paint(painter, option, index);
if(option.state & QStyle::State_MouseOver) painter->drawRect(option.rect);
}
~MyDelegate(){}
};
然后我用一些项目实例化一个 QListWidget 并启用
Qt::WA_Hover
属性:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QListWidget w;
w.addItems(QStringList{"item1", "item2", "item3", "item4"});
w.setItemDelegate(new MyDelegate(&w));
w.viewport()->setAttribute(Qt::WA_Hover);
w.show();
return a.exec();
}
不幸的是,这种行为不是我所期望的。特别是,当我将鼠标移到一个项目上时,会绘制轮廓,但是当我移动到另一个项目时,第一个项目周围的轮廓不会被删除。相反,它会不断在我将鼠标移到的所有项目周围绘制轮廓,最终在所有项目周围都有一个轮廓。这是正常的吗?我知道另一种解决方案是使用
QStyleSheet
s 但我想了解为什么当前的方法不像我预期的那样运行。
paint
方法:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
int outlineWidth = 1;
QPen pen;
pen.setWidth(outlineWidth);
painter->setPen(pen);
QStyledItemDelegate::paint(painter, option, index);
if(option.state & QStyle::State_MouseOver) {
int a = round(0.5*(outlineWidth - 1));
int b = round(-0.5*outlineWidth);
painter->drawRect(option.rect.adjusted(a, a, b, b));
}
}
不幸的是,行为非常相似。这是从上到下悬停在所有项目上后的屏幕截图:
最佳答案
原因QPainter::drawRect
绘制一个比绘制区域略大(高度和宽度正好一个像素)的矩形。这种行为的原因可以在 the way QPaintEngine
draws a rectangle 中看到。 :
for (int i=0; i<rectCount; ++i) {
QRectF rf = rects[i];
QPointF pts[4] = { QPointF(rf.x(), rf.y()),
QPointF(rf.x() + rf.width(), rf.y()),
QPointF(rf.x() + rf.width(), rf.y() + rf.height()),
QPointF(rf.x(), rf.y() + rf.height()) };
drawPolygon(pts, 4, ConvexMode);
}
QPaintEngine
绘制一个封闭的多边形,从点
(x, y)
开始, 转至
(x + width, y)
,然后转至
(x + width, y + height)
最后到
(x, y + height)
.这看起来很直观,但是让我们看看如果我们用实数替换这些变量会发生什么:
4x2
(0, 0)
处的 px 矩形.
QPaintEngine
将使用以下坐标:
(0, 0)
,
(4, 0)
,
(4, 2)
和
(0, 2)
.以像素表示,绘图将如下所示:
4x2
px,我们最终得到
5x3
px 矩形,即确实宽和高一个像素。
option.rect
来进一步证明这一点。在调用
drawRect
之前像这样:
if (option.state & QStyle::State_MouseOver) {
painter->setClipRect(option.rect);
painter->drawRect(option.rect);
}
结果是轮廓的底部和右侧边缘被剪裁(非常边缘,我们预计在绘制区域内):
QRect::adjusted
减小轮廓的高度和宽度.
painter->drawRect(option.rect.adjusted(0, 0, -1, -1));
但是,这仅适用于轮廓,即 1px 厚和
devicePixelRatio
是 1,就像在 PC 上一样。如果轮廓的边框大于 1px 和/或
devicePixelRatio
是 2,就像在 Mac 上一样,当然更多的轮廓会伸出绘制区域,所以你应该考虑到这一点并相应地调整矩形,例如:
int effectiveOutlineWidth = m_outineWidth*m_devicePixelRatio;
int tl = round(0.5*(effectiveOutlineWidth - 1));
int br = round(-0.5*effectiveOutlineWidth);
painter->drawRect(option.rect.adjusted(tl, tl, br, br));
m_outineWidth
和
m_devicePixelRatio
是类成员,分别代表所需的轮廓宽度。绘制设备的物理像素和与设备无关的像素之间的比率。如果你已经为它们创建了公共(public)的 setter 方法,你可以像这样设置它们的值:
auto *delegate = new MyDelegate(&w);
delegate->setOutlineWidth(1);
delegate->setDevicePixelRatio(w.devicePixelRatio());
w.setItemDelegate(delegate);
例子
#include <QApplication>
#include <QStyledItemDelegate>
#include <QListWidget>
#include <QPainter>
class MyDelegate : public QStyledItemDelegate
{
int m_outineWidth;
int m_devicePixelRatio;
public:
MyDelegate(QObject *parent = nullptr) :
QStyledItemDelegate(parent),
m_outineWidth(1),
m_devicePixelRatio(1) {
}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override {
QStyledItemDelegate::paint(painter, option, index);
if (option.state & QStyle::State_MouseOver) {
int effectiveOutlineWidth = m_outineWidth*m_devicePixelRatio;
int tl = round(0.5*(effectiveOutlineWidth - 1));
int br = round(-0.5*effectiveOutlineWidth);
painter->setPen(QPen(QBrush(Qt::red), m_outineWidth, Qt::SolidLine,
Qt::SquareCap, Qt::MiterJoin));
painter->drawRect(option.rect.adjusted(tl, tl, br, br));
}
}
void setOutlineWidth(int outineWidth) {
m_outineWidth = outineWidth;
}
void setDevicePixelRatio(int devicePixelRatio) {
m_devicePixelRatio = devicePixelRatio;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QListWidget w;
auto *delegate = new MyDelegate(&w);
delegate->setOutlineWidth(3);
delegate->setDevicePixelRatio(w.devicePixelRatio());
w.setItemDelegate(delegate);
w.addItems(QStringList{"item1", "item2", "item3", "item4"});
w.viewport()->setAttribute(Qt::WA_Hover);
w.show();
return a.exec();
}
结果
关于c++ - 将鼠标悬停在 QListWidget 项目上时如何绘制轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64576846/
我试图找到在庞大的代码库中创建 NaN 的位置。是否有一些编译器标志或我可以用来在 NaN 上 panic 的东西,这样我就可以找到它在哪一行? 最佳答案 没有编译器标志。你能做的最好的事情就是把你的
A类 class ClassA { @Autowired class ClassB; } 类配置: @Configuration class TestConfi
我是一名统计学研究生,经常使用 R。我熟悉其他编程环境中的 OOP。我什至在各种定义用于存储数据的新类的统计包中看到了它的使用。 在我研究生生涯的这个阶段,我通常会为一些类作业编写一些算法——一些接收
我想要两个不同的网络摄像头视频输出,一个是普通的网络摄像头镜头,另一个是它的“镜像”版本。 cv2可以吗? import time, cv2 video=cv2.VideoCapture(0) a=0
我创建了一个可以通过两种方式过滤的图库。一个通过单击按钮,另一个通过搜索过滤器。过滤器工作完美,除了当 div 隐藏在过滤器上时,其余显示的 div 不会彼此相邻 float 。 这是过滤前的样子:
我们作为一个 4 人团队工作,我们的项目部署在 openshift我们使用 git 存储库 进行提交、推送和 pull 。当有人提交更多更改时,其他人必须 pull 它以在我们的系统中进行更新。但是从
我正在尝试扩展自动完成功能,以便在选择某个项目时显示辅助标签。例如,给定显示项目的自动完成功能,项目名称将显示在包含代码的输入框旁边的 span 标记中。 查看自动完成源代码,我发现过滤值的下拉列表是
我有一个包含歌曲、艺术家和专辑实体的核心数据。 歌曲有可选的一对一关系艺术家到艺术家实体和专辑到专辑实体这两个实体都与 Song 实体具有反向关系。 相册有可选的一对一关系艺术家到艺术家实体和可选的一
XmlSerializer正在调用 IList.Add()在我的课上,我不明白为什么。 我有一个自定义类(层次结构中的几个类之一),其中包含我使用 XmlSerializer 与 XML 相互转换的数
我们在 Web 应用程序中定义了此事件,它创建了一个名为 timelineEventClicked 的自定义触发器 canvas.addEventListener('click', function
有大量资源可用于使用 Swift(可达性)检查有效的 Internet 连接,以及在进行 API 调用时检查 httpResponse 的 statusCode 的方法,但是检查和处理这些的“正确”方
谁能告诉我是否可以在 Controller 规范中 stub params[] 值,以便 Controller 接受 stub 值作为 View 中的实际 params[] 值。 例如,我的观点有一个
我的问题是没有在 UserControl 中连接 DependencyProperties。这不是问题。当我将 UserControl 中的按钮绑定(bind)到 UserControl 的 Depe
如何#define 路径 L"C:\Windows\System32\taskmgr.exe"来处理宽字符 #define TASK_MGR "C:\\Windows\\System32\\taskm
我正在尝试使用 Jasmine 和 Sion 编写单元测试,但是在使用 RequireJs 加载模块时我很难找到以下等效项: sinon.stub(window, "MyItemView"); 使用
我有一个包含三个 div 的示例页面,如下所示: 当浏览器大小达到 md 点并且第二个 div 高于第一个 div 时,第三个 div 开始在第一个的右侧
我在 C++ 端有 CString cs,在 C# 端有 IntPtr ip,它通过编码(marshal)处理机制包含 cs 的值。 然后,我只需将需要的字符串作为 Marshal.PtrToStri
我是一名优秀的程序员,十分优秀!