- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我必须制作具有固定大小句柄的滚动条,因为它的背景图像在缩放时变得很难看。我使用以下样式表:
QScrollBar::handle:vertical {
border-image:url(:/images/handle.png);
min-height: 47px;
max-height: 47px;
height: 47px;
width:10px;
}
但似乎 max-height
不是有效属性。如何防止句柄结垢?
最佳答案
小技巧,来自Qt源码:我的滚动条.h
class myScrollBar : public QScrollBar
{
Q_OBJECT
public:
myScrollBar(Qt::Orientation orientation, QWidget *parent = 0);
void setSliderLength(int length);
protected:
void paintEvent(QPaintEvent *);
private:
QRect subControlRect(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QStyle::SubControl sc, /*const*/ QWidget *widget);
QRect visualRect(Qt::LayoutDirection direction, const QRect &boundingRect, const QRect &logicalRect);
int sliderPositionFromValue(int min, int max, int logicalValue, int span, bool upsideDown);
private:
int _sliderLength;
};
我的滚动条.cpp
myScrollBar::myScrollBar(Qt::Orientation orientation, QWidget *parent)
: QScrollBar(orientation, parent),
_sliderLength(100)
{
}
void myScrollBar::paintEvent(QPaintEvent *e)
{
qDebug() << "scrollbar paintevent";
//Q_D(QScrollBar);
QScrollBar::paintEvent(e);
QPainter painter(this);
QStyleOptionSlider opt;
initStyleOption(&opt);
opt.subControls = QStyle::SC_All;
QWidget *widget = this;
// if (d->pressedControl) {
// opt.activeSubControls = (QStyle::SubControl)d->pressedControl;
// if (!d->pointerOutsidePressedControl)
// opt.state |= QStyle::State_Sunken;
// } else {
// opt.activeSubControls = (QStyle::SubControl)d->hoverControl;
// }
QPainter *p = &painter;
//style()->drawComplexControl(QStyle::CC_ScrollBar, &opt, &p, this);
if (const QStyleOptionSlider *scrollbar = &opt) {
// Make a copy here and reset it for each primitive.
QStyleOptionSlider newScrollbar = *scrollbar;
QStyle::State saveFlags = scrollbar->state;
if (scrollbar->subControls & QStyle::SC_ScrollBarSubLine) {
newScrollbar.state = saveFlags;
newScrollbar.rect = subControlRect(QStyle::CC_ScrollBar, &newScrollbar, QStyle::SC_ScrollBarSubLine, widget);
if (newScrollbar.rect.isValid()) {
if (!(scrollbar->activeSubControls & QStyle::SC_ScrollBarSubLine))
newScrollbar.state &= ~(QStyle::State_Sunken | QStyle::State_MouseOver);
style()->drawControl(QStyle::CE_ScrollBarSubLine, &newScrollbar, p, widget);
}
}
if (scrollbar->subControls & QStyle::SC_ScrollBarAddLine) {
newScrollbar.rect = scrollbar->rect;
newScrollbar.state = saveFlags;
newScrollbar.rect = subControlRect(QStyle::CC_ScrollBar, &newScrollbar, QStyle::SC_ScrollBarAddLine, widget);
if (newScrollbar.rect.isValid()) {
if (!(scrollbar->activeSubControls & QStyle::SC_ScrollBarAddLine))
newScrollbar.state &= ~(QStyle::State_Sunken | QStyle::State_MouseOver);
style()->drawControl(QStyle::CE_ScrollBarAddLine, &newScrollbar, p, widget);
}
}
if (scrollbar->subControls & QStyle::SC_ScrollBarSubPage) {
newScrollbar.rect = scrollbar->rect;
newScrollbar.state = saveFlags;
newScrollbar.rect = subControlRect(QStyle::CC_ScrollBar, &newScrollbar, QStyle::SC_ScrollBarSubPage, widget);
if (newScrollbar.rect.isValid()) {
if (!(scrollbar->activeSubControls & QStyle::SC_ScrollBarSubPage))
newScrollbar.state &= ~(QStyle::State_Sunken | QStyle::State_MouseOver);
style()->drawControl(QStyle::CE_ScrollBarSubPage, &newScrollbar, p, widget);
}
}
if (scrollbar->subControls & QStyle::SC_ScrollBarAddPage) {
newScrollbar.rect = scrollbar->rect;
newScrollbar.state = saveFlags;
newScrollbar.rect = subControlRect(QStyle::CC_ScrollBar, &newScrollbar, QStyle::SC_ScrollBarAddPage, widget);
if (newScrollbar.rect.isValid()) {
if (!(scrollbar->activeSubControls & QStyle::SC_ScrollBarAddPage))
newScrollbar.state &= ~(QStyle::State_Sunken | QStyle::State_MouseOver);
style()->drawControl(QStyle::CE_ScrollBarAddPage, &newScrollbar, p, widget);
}
}
if (scrollbar->subControls & QStyle::SC_ScrollBarFirst) {
newScrollbar.rect = scrollbar->rect;
newScrollbar.state = saveFlags;
newScrollbar.rect = subControlRect(QStyle::CC_ScrollBar, &newScrollbar, QStyle::SC_ScrollBarFirst, widget);
if (newScrollbar.rect.isValid()) {
if (!(scrollbar->activeSubControls & QStyle::SC_ScrollBarFirst))
newScrollbar.state &= ~(QStyle::State_Sunken | QStyle::State_MouseOver);
style()->drawControl(QStyle::CE_ScrollBarFirst, &newScrollbar, p, widget);
}
}
if (scrollbar->subControls & QStyle::SC_ScrollBarLast) {
newScrollbar.rect = scrollbar->rect;
newScrollbar.state = saveFlags;
newScrollbar.rect = subControlRect(QStyle::CC_ScrollBar, &newScrollbar, QStyle::SC_ScrollBarLast, widget);
if (newScrollbar.rect.isValid()) {
if (!(scrollbar->activeSubControls & QStyle::SC_ScrollBarLast))
newScrollbar.state &= ~(QStyle::State_Sunken | QStyle::State_MouseOver);
style()->drawControl(QStyle::CE_ScrollBarLast, &newScrollbar, p, widget);
}
}
if (scrollbar->subControls & QStyle::SC_ScrollBarSlider) {
newScrollbar.rect = scrollbar->rect;
newScrollbar.state = saveFlags;
QRect rect = subControlRect(QStyle::CC_ScrollBar, &newScrollbar, QStyle::SC_ScrollBarSlider, widget);
newScrollbar.rect = QRect(rect.topLeft(), QSize(rect.width(), 100));
if (newScrollbar.rect.isValid()) {
if (!(scrollbar->activeSubControls & QStyle::SC_ScrollBarSlider))
newScrollbar.state &= ~(QStyle::State_Sunken | QStyle::State_MouseOver);
style()->drawControl(QStyle::CE_ScrollBarSlider, &newScrollbar, p, widget);
if (scrollbar->state & QStyle::State_HasFocus) {
QStyleOptionFocusRect fropt;
fropt.QStyleOption::operator=(newScrollbar);
fropt.rect.setRect(newScrollbar.rect.x() + 2, newScrollbar.rect.y() + 2,
newScrollbar.rect.width() - 5,
newScrollbar.rect.height() - 5);
style()->drawPrimitive(QStyle::PE_FrameFocusRect, &fropt, p, widget);
}
}
}
}
}
void myScrollBar::setSliderLength(int length)
{
_sliderLength = length;
}
QRect myScrollBar::subControlRect(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QStyle::SubControl sc, /*const*/ QWidget *widget)
{
QRect ret;
switch (cc) {
case QStyle::CC_ScrollBar:
if (const QStyleOptionSlider *scrollbar = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
const QRect scrollBarRect = scrollbar->rect;
int sbextent = 0;
if (!style()->styleHint(QStyle::SH_ScrollBar_Transient, scrollbar, widget))
sbextent = style()->pixelMetric(QStyle::PM_ScrollBarExtent, scrollbar, widget);
int maxlen = ((scrollbar->orientation == Qt::Horizontal) ?
scrollBarRect.width() : scrollBarRect.height()) - (sbextent * 2);
int sliderlen;
// calculate slider length
if (scrollbar->maximum != scrollbar->minimum) {
uint range = scrollbar->maximum - scrollbar->minimum;
sliderlen = (qint64(scrollbar->pageStep) * maxlen) / (range + scrollbar->pageStep);
int slidermin = style()->pixelMetric(QStyle::PM_ScrollBarSliderMin, scrollbar, widget);
if (_sliderLength < slidermin || range > INT_MAX / 2)
_sliderLength = slidermin;
if (_sliderLength > maxlen)
_sliderLength = maxlen;
//length of the slider
sliderlen = _sliderLength;
} else {
sliderlen = maxlen;
}
int sliderstart = sbextent + this->sliderPositionFromValue(scrollbar->minimum,
scrollbar->maximum,
scrollbar->sliderPosition,
maxlen - sliderlen,
scrollbar->upsideDown);
switch (sc) {
case QStyle::SC_ScrollBarSubLine: // top/left button
if (scrollbar->orientation == Qt::Horizontal) {
int buttonWidth = qMin(scrollBarRect.width() / 2, sbextent);
ret.setRect(0, 0, buttonWidth, scrollBarRect.height());
} else {
int buttonHeight = qMin(scrollBarRect.height() / 2, sbextent);
ret.setRect(0, 0, scrollBarRect.width(), buttonHeight);
}
break;
case QStyle::SC_ScrollBarAddLine: // bottom/right button
if (scrollbar->orientation == Qt::Horizontal) {
int buttonWidth = qMin(scrollBarRect.width()/2, sbextent);
ret.setRect(scrollBarRect.width() - buttonWidth, 0, buttonWidth, scrollBarRect.height());
} else {
int buttonHeight = qMin(scrollBarRect.height()/2, sbextent);
ret.setRect(0, scrollBarRect.height() - buttonHeight, scrollBarRect.width(), buttonHeight);
}
break;
case QStyle::SC_ScrollBarSubPage: // between top/left button and slider
if (scrollbar->orientation == Qt::Horizontal)
ret.setRect(sbextent, 0, sliderstart - sbextent, scrollBarRect.height());
else
ret.setRect(0, sbextent, scrollBarRect.width(), sliderstart - sbextent);
break;
case QStyle::SC_ScrollBarAddPage: // between bottom/right button and slider
if (scrollbar->orientation == Qt::Horizontal)
ret.setRect(sliderstart + sliderlen, 0,
maxlen - sliderstart - sliderlen + sbextent, scrollBarRect.height());
else
ret.setRect(0, sliderstart + sliderlen, scrollBarRect.width(),
maxlen - sliderstart - sliderlen + sbextent);
break;
case QStyle::SC_ScrollBarGroove:
if (scrollbar->orientation == Qt::Horizontal)
ret.setRect(sbextent, 0, scrollBarRect.width() - sbextent * 2,
scrollBarRect.height());
else
ret.setRect(0, sbextent, scrollBarRect.width(),
scrollBarRect.height() - sbextent * 2);
break;
case QStyle::SC_ScrollBarSlider:
if (scrollbar->orientation == Qt::Horizontal)
ret.setRect(sliderstart, 0, sliderlen, scrollBarRect.height());
else
ret.setRect(0, sliderstart, scrollBarRect.width(), sliderlen);
break;
default:
break;
}
ret = visualRect(scrollbar->direction, scrollBarRect, ret);
}
return ret;
}
}
QRect myScrollBar::visualRect(Qt::LayoutDirection direction, const QRect &boundingRect, const QRect &logicalRect)
{
if (direction == Qt::LeftToRight)
return logicalRect;
QRect rect = logicalRect;
rect.translate(2 * (boundingRect.right() - logicalRect.right()) +
logicalRect.width() - boundingRect.width(), 0);
return rect;
}
int myScrollBar::sliderPositionFromValue(int min, int max, int logicalValue, int span, bool upsideDown)
{
if (span <= 0 || logicalValue < min || max <= min)
return 0;
if (logicalValue > max)
return upsideDown ? span : min;
uint range = max - min;
uint p = upsideDown ? max - logicalValue : logicalValue - min;
if (range > (uint)INT_MAX/4096) {
double dpos = (double(p))/(double(range)/span);
return int(dpos);
} else if (range > (uint)span) {
return (2 * p * span + range) / (2*range);
} else {
uint div = span / range;
uint mod = span % range;
return p * div + (2 * p * mod + range) / (2 * range);
}
// equiv. to (p * span) / range + 0.5
// no overflow because of this implicit assumption:
// span <= 4096
}
例如:
myScrollBar *bar = new myScrollBar(Qt::Vertical, ui->treeWidget);
ui->treeWidget->setVerticalScrollBar(bar);
关于css - 如何制作具有固定大小句柄的 QScrollBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7710357/
我有一个名为 main.css 的 css 文件和另一个名为 style.css 的文件。我怎样才能在 main.css 中做到这一点? .someClass { //apply rules to
在更新我的 css 之前,我在 Login.css 中有以下内容: body { background-image: url('./pictures/fond.png'); bac
我的 share point 2013 核心 css 和我的 css 之间存在 css 冲突。所以我想把我所有的类都放在 div #s4-workspace 下但是我搜索了一种方法来将所有类分组到这个
我知道您可以覆盖 jsp 页面从 jsp 包含 CSS 文件(即全局 CSS 文件)继承的 CSS 属性。 但是,如果元素中的某个属性弄乱了特定页面,而我不想只使用内联 CSS 在该页面中使用它怎么办
我刚刚发现了 initial-scale 元属性。 以前,我一直在使用 default.css 来定义我所有的样式和大小(用于字体和元素),以便它们在桌面计算机的屏幕上显示得很好。然后,如果您使用的是
我正在尝试使用 LESS CSS 来编写我的 CSS。我已经按顺序导入了 style.less 和 less.js 文件。 现在我想提取 LESS 生成的 CSS。有什么办法可以做到吗?我不想使用脚本
我想知道是否有任何一种软件可以读取大量内联样式中的 HTML 文档并将所有这些样式转换为外部 css 文件。如果只有一页,我可以手动完成。但是有100页。有人有想法吗? 最佳答案 就像有人说的那样,“
当我想从 Styled Components 迁移到 CSS Modules 时,出现了以下问题。 假设我有以下样式组件,它接受动态参数 offset和一个动态 CSS 字符串 theme : con
有没有办法将 CSS 类定义为与另一个类相等?例如,如果我有一个类: .myClass{ background-color: blue; } 有没有一种方法可以将第二个类定义为与 myClas
我正在尝试制作一组按钮,这些按钮贴在页面底部并且由固定的空间隔开。我正在使用 angularJS 的 ng-repeat 指令通过 ajax 请求获取数据,然后我用它来显示按钮。 我的问题在于让按
浏览器是否在加载 CSS 文件时解析 CSS?还是在整个 CSS 文件被浏览器下载后才进行解析?不同浏览器的做法有区别吗?我在哪里可以找到这种底层信息? 这个问题不是 Load and executi
这个问题在这里已经有了答案: Can a CSS class inherit one or more other classes? (29 个答案) 关闭 3 年前。 标题有点乱,我给大家看一下。假
我遇到了最奇怪的问题...... 在最简单的形式中,我有一个包含以下内容的 index.html 文件: (在尝试确定根本原因的过程中,我已经大大减少了它) 当我查看页面的源代码时,我得到以下信息:
我正在使用 Mindscape Workbench 来最小化我的 scss 文件。我的页面设置为使用 *.min.css 文件。在随机时间,min 文件不会与系统的其余部分一起发布。 我有很多 css
请告诉我 CSS 框架和 CSS 网格之间的区别。 最佳答案 CSS 框架也可以是 CSS 网格框架。 CSS 网格框架用于构建 CSS 布局。有一些框架除了构建布局还有其他用途,例如 Hartija
我有无法从页面中删除或更改的 original.css 文件。原始.css table { border-collapse: collapse; border-spacing: 0;
我以前使用 bootstrap css import 很好。 但是我正在尝试使用 CSS 模块,所以我添加了几行。 { test: /\.css$/, use:
有没有办法在 css 选择器中创建一个 css 组。 例如: .SectionHeader { include: .foo; include: .bar; include: .
今天我学习了 CSS 中的两个概念,一个是 CSS 定位(静态、相对、绝对、固定),另一个是 CSS Margin,它定义了元素之间的空间。 假设我想移动一个元素,这是最好的方法吗?因为这两个概念似乎
var paths = { css: './public/apps/user/**/*.css' } var dest = { css: './public/apps/user/css/' } /
我是一名优秀的程序员,十分优秀!