本文整理汇总了C++中QLinearGradient::setStops方法的典型用法代码示例。如果您正苦于以下问题:C++ QLinearGradient::setStops方法的具体用法?C++ QLinearGradient::setStops怎么用?C++ QLinearGradient::setStops使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QLinearGradient
的用法示例。
在下文中一共展示了QLinearGradient::setStops方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: createRectGradient
QImage createRectGradient(int width, int height, const QGradient &gradient)
{
QImage::Format format = QImage::Format_ARGB32_Premultiplied;
QImage buffer(width, height, format);
buffer.fill( qRgba(255, 255,255, 255) );
QPainter painter(&buffer);
painter.setPen(Qt::NoPen);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setCompositionMode(QPainter::CompositionMode_DestinationAtop);
// Divide the rectangle into four triangles, each facing North, South,
// East, and West. Every triangle will have its bounding box and linear
// gradient.
QLinearGradient linearGradient;
// from center going North
linearGradient = QLinearGradient(0, 0, width, height / 2);
linearGradient.setStops( gradient.stops() );
linearGradient.setStart(width / 2, height / 2);
linearGradient.setFinalStop(width / 2, 0);
painter.setBrush(linearGradient);
painter.drawRect(0, 0, width, height / 2);
// from center going South
linearGradient = QLinearGradient(0, 0, width, height / 2);
linearGradient.setStops( gradient.stops() );
linearGradient.setStart(width / 2, height / 2);
linearGradient.setFinalStop(width / 2, height);
painter.setBrush(linearGradient);
painter.drawRect(0, height / 2, width, height / 2);
// clip the East and West portion
QPainterPath clip;
clip.moveTo(width, 0);
clip.lineTo(width, height);
clip.lineTo(0, 0);
clip.lineTo(0, height);
clip.closeSubpath();
painter.setClipPath(clip);
// from center going East
linearGradient = QLinearGradient(0, 0, width / 2, height);
linearGradient.setStops( gradient.stops() );
linearGradient.setStart(width / 2, height / 2);
linearGradient.setFinalStop(width, height / 2);
painter.setBrush(linearGradient);
painter.drawRect(width / 2, 0, width, height);
// from center going West
linearGradient = QLinearGradient(0, 0, width / 2, height);
linearGradient.setStops( gradient.stops() );
linearGradient.setStart(width / 2, height / 2);
linearGradient.setFinalStop(0, height / 2);
painter.setBrush(linearGradient);
painter.drawRect(0, 0, width / 2, height);
painter.end();
return buffer;
}
示例2: loadSettings
void DkTransferToolBar::loadSettings() {
QSettings& settings = Settings::instance().getSettings();
settings.beginGroup("Pseudo Color");
int gSize = settings.beginReadArray("oldGradients");
for (int idx = 0; idx < gSize; idx++) {
settings.setArrayIndex(idx);
QVector<QGradientStop> stops;
int sSize = settings.beginReadArray("gradient");
for (int sIdx = 0; sIdx < sSize; sIdx++) {
settings.setArrayIndex(sIdx);
QGradientStop s;
s.first = settings.value("posRGBA", 0).toFloat();
s.second = QColor::fromRgba(settings.value("colorRGBA", QColor().rgba()).toInt());
qDebug() << "pos: " << s.first << " col: " << s.second;
stops.append(s);
}
settings.endArray();
QLinearGradient g;
g.setStops(stops);
oldGradients.append(g);
}
settings.endArray();
settings.endGroup();
}
示例3: paintEvent
void KarbonGradientWidget::paintEvent(QPaintEvent*)
{
int w = width() - 4; // available width for gradient and points
int h = height() - 7; // available height for gradient and points
int ph = colorStopBorder_height + 2; // point marker height
int gh = h - ph; // gradient area height
QPainter painter(this);
QLinearGradient gradient;
gradient.setStart(QPointF(2, 2));
gradient.setFinalStop(QPointF(width() - 2, 2));
gradient.setStops(m_stops);
m_checkerPainter.paint(painter, QRectF(2, 2, w, gh));
painter.setBrush(QBrush(gradient));
painter.drawRect(QRectF(2, 2, w, gh));
painter.setBrush(QBrush());
painter.setPen(palette().light().color());
// light frame around widget
QRect frame(1, 1, width() - 2, height() - 2);
painter.drawRect(frame);
// light line between gradient and point area
painter.drawLine(QLine(QPoint(1, 3 + gh), QPoint(width() - 1, 3 + gh)));
painter.setPen(palette().dark().color());
// left-top frame around widget
painter.drawLine(QPoint(), QPoint(0, height() - 1));
painter.drawLine(QPoint(), QPoint(width() - 1, 0));
// right-bottom from around gradient
painter.drawLine(QPoint(width() - 2, 2), QPoint(width() - 2, 2 + gh));
painter.drawLine(QPoint(width() - 2, 2 + gh), QPoint(2, 2 + gh));
// upper line around point area
painter.drawLine(QPoint(1, height() - 3 - ph), QPoint(width() - 1, height() - 3 - ph));
// right-bottom line around point area
painter.drawLine(QPoint(width() - 2, height() - ph - 1), QPoint(width() - 2, height() - 2));
painter.drawLine(QPoint(width() - 2, height() - 2), QPoint(2, height() - 2));
m_pntArea.setRect(2, height() - ph - 2, w, ph);
painter.fillRect(m_pntArea.x(), m_pntArea.y(), m_pntArea.width(), m_pntArea.height(), palette().window().color());
painter.setClipRect(m_pntArea.x(), m_pntArea.y(), m_pntArea.width(), m_pntArea.height());
painter.translate(m_pntArea.x(), m_pntArea.y());
foreach(const QGradientStop & stop, m_stops)
paintColorStop(painter, (int)(stop.first * m_pntArea.width()), stop.second);
}
示例4: paint
void KoResourceItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
if( ! index.isValid() )
return;
KoResource * resource = static_cast<KoResource*>( index.internalPointer() );
if (!resource)
return;
painter->save();
if (option.state & QStyle::State_Selected)
painter->fillRect( option.rect, option.palette.highlight() );
QRect innerRect = option.rect.adjusted( 2, 1, -2, -1 );
KoAbstractGradient * gradient = dynamic_cast<KoAbstractGradient*>( resource );
if (gradient) {
QGradient * g = gradient->toQGradient();
QLinearGradient paintGradient;
paintGradient.setStops( g->stops() );
paintGradient.setStart( innerRect.topLeft() );
paintGradient.setFinalStop( innerRect.topRight() );
m_checkerPainter.paint( *painter, innerRect );
painter->fillRect( innerRect, QBrush( paintGradient ) );
delete g;
}
else {
QImage thumbnail = index.data( Qt::DecorationRole ).value<QImage>();
QSize imageSize = thumbnail.size();
if(imageSize.height() > innerRect.height() || imageSize.width() > innerRect.width()) {
qreal scaleW = static_cast<qreal>( innerRect.width() ) / static_cast<qreal>( imageSize.width() );
qreal scaleH = static_cast<qreal>( innerRect.height() ) / static_cast<qreal>( imageSize.height() );
qreal scale = qMin( scaleW, scaleH );
int thumbW = static_cast<int>( imageSize.width() * scale );
int thumbH = static_cast<int>( imageSize.height() * scale );
thumbnail = thumbnail.scaled( thumbW, thumbH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
}
painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
if (thumbnail.hasAlphaChannel()) {
painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns.
}
painter->fillRect( innerRect, QBrush(thumbnail) );
}
painter->restore();
}
示例5: onGradientClicked
void ContextPaneWidgetRectangle::onGradientClicked()
{
if (ui->colorGradient->isChecked()) {
m_hasGradient = true;
QLinearGradient gradient;
QGradientStops stops;
stops.append(QGradientStop(0, ui->colorColorButton->convertedColor()));
stops.append(QGradientStop(1, Qt::white));
gradient.setStops(stops);
ui->gradientLine->setEnabled(true);
ui->gradientLine->setGradient(gradient);
}
}
示例6: loadComposite
void StyleLoader::loadComposite(QLinearGradient& value)
{
int coordinateMode;
int spread;
QPointF start;
QPointF finish;
QGradientStops stopPoints;
load("start", start);
load("finish", finish);
load("coordinateMode", coordinateMode);
load("spread", spread);
load("stopPoints", stopPoints);
value = QLinearGradient(start, finish);
value.setSpread((QGradient::Spread) spread);
value.setCoordinateMode((QGradient::CoordinateMode) coordinateMode);
value.setStops(stopPoints);
}
示例7: parseGradient
QLinearGradient PropertyReader::parseGradient(const QString &propertyName, bool *isBound) const
{
if (!m_doc)
return QLinearGradient();
*isBound = false;
for (UiObjectMemberList *members = m_ast->members; members; members = members->next) {
UiObjectMember *member = members->member;
if (UiObjectBinding* objectBinding = cast<UiObjectBinding *>(member)) {
UiObjectInitializer *initializer = objectBinding->initializer;
const QString astValue = cleanupSemicolon(textAt(m_doc,
initializer->lbraceToken,
initializer->rbraceToken));
const QString objectPropertyName = objectBinding->qualifiedId->name.toString();
const QStringRef typeName = objectBinding->qualifiedTypeNameId->name;
if (objectPropertyName == propertyName && typeName.contains(QLatin1String("Gradient"))) {
QLinearGradient gradient;
QVector<QGradientStop> stops;
for (UiObjectMemberList *members = initializer->members; members; members = members->next) {
UiObjectMember *member = members->member;
if (UiObjectDefinition *objectDefinition = cast<UiObjectDefinition *>(member)) {
PropertyReader localParser(m_doc, objectDefinition->initializer);
if (localParser.hasProperty(QLatin1String("color")) && localParser.hasProperty(QLatin1String("position"))) {
QColor color = QmlJS::toQColor(localParser.readProperty(QLatin1String("color")).toString());
qreal position = localParser.readProperty(QLatin1String("position")).toReal();
if (localParser.isBindingOrEnum(QLatin1String("color")) || localParser.isBindingOrEnum(QLatin1String("position")))
*isBound = true;
stops.append( QPair<qreal, QColor>(position, color));
}
}
}
gradient.setStops(stops);
return gradient;
}
}
}
return QLinearGradient();
}
示例8: paintEvent
void paintEvent(QPaintEvent *pEvent)
{
QPainter painter(this);
QLinearGradient gradient;
QGradientStop point1(0, m_color1);
QGradientStop point2(0.5, m_color);
QGradientStop point3(1, m_color2);
QGradientStops stops;
stops << point1 << point2 << point3;
gradient.setStops(stops);
if (orientation() == Qt::Horizontal)
{
gradient.setStart(rect().left() + 1, 0);
gradient.setFinalStop(rect().right(), 0);
}
else
{
gradient.setStart(0, rect().top() + 1);
gradient.setFinalStop(0, rect().bottom());
}
painter.fillRect(pEvent->rect(), gradient);
}
示例9: parseGradient
QBrush XMLParseBase::parseGradient(const QDomElement &element)
{
QLinearGradient gradient;
QString gradientStart = element.attribute("start", "");
QString gradientEnd = element.attribute("end", "");
int gradientAlpha = element.attribute("alpha", "255").toInt();
QString direction = element.attribute("direction", "vertical");
float x1, y1, x2, y2 = 0.0;
if (direction == "vertical")
{
x1 = 0.5;
x2 = 0.5;
y1 = 0.0;
y2 = 1.0;
}
else if (direction == "diagonal")
{
x1 = 0.0;
x2 = 1.0;
y1 = 0.0;
y2 = 1.0;
}
else
{
x1 = 0.0;
x2 = 1.0;
y1 = 0.5;
y2 = 0.5;
}
gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
gradient.setStart(x1, y1);
gradient.setFinalStop(x2, y2);
QGradientStops stops;
if (!gradientStart.isEmpty())
{
QColor startColor = QColor(gradientStart);
startColor.setAlpha(gradientAlpha);
QGradientStop stop(0.0, startColor);
stops.append(stop);
}
if (!gradientEnd.isEmpty())
{
QColor endColor = QColor(gradientEnd);
endColor.setAlpha(gradientAlpha);
QGradientStop stop(1.0, endColor);
stops.append(stop);
}
for (QDomNode child = element.firstChild(); !child.isNull();
child = child.nextSibling())
{
QDomElement childElem = child.toElement();
if (childElem.tagName() == "stop")
{
float position = childElem.attribute("position", "0").toFloat();
QString color = childElem.attribute("color", "");
int alpha = childElem.attribute("alpha", "-1").toInt();
if (alpha < 0)
alpha = gradientAlpha;
QColor stopColor = QColor(color);
stopColor.setAlpha(alpha);
QGradientStop stop((position / 100), stopColor);
stops.append(stop);
}
}
gradient.setStops(stops);
return QBrush(gradient);
}
示例10: parseGradient
bool SvgParser::parseGradient(const KoXmlElement &e, const KoXmlElement &referencedBy)
{
// IMPROVEMENTS:
// - Store the parsed colorstops in some sort of a cache so they don't need to be parsed again.
// - A gradient inherits attributes it does not have from the referencing gradient.
// - Gradients with no color stops have no fill or stroke.
// - Gradients with one color stop have a solid color.
SvgGraphicsContext *gc = m_context.currentGC();
if (!gc)
return false;
SvgGradientHelper gradhelper;
if (e.hasAttribute("xlink:href")) {
QString href = e.attribute("xlink:href").mid(1);
if (! href.isEmpty()) {
// copy the referenced gradient if found
SvgGradientHelper *pGrad = findGradient(href);
if (pGrad)
gradhelper = *pGrad;
} else {
//gc->fillType = SvgGraphicsContext::None; // <--- TODO Fill OR Stroke are none
return false;
}
}
// Use the gradient that is referencing, or if there isn't one, the original gradient.
KoXmlElement b;
if (!referencedBy.isNull())
b = referencedBy;
else
b = e;
QString gradientId = b.attribute("id");
if (! gradientId.isEmpty()) {
// check if we have this gradient already parsed
// copy existing gradient if it exists
if (m_gradients.find(gradientId) != m_gradients.end())
gradhelper.copyGradient(m_gradients[ gradientId ].gradient());
}
if (b.attribute("gradientUnits") == "userSpaceOnUse")
gradhelper.setGradientUnits(SvgGradientHelper::UserSpaceOnUse);
// parse color prop
QColor c = gc->currentColor;
if (!b.attribute("color").isEmpty()) {
m_context.styleParser().parseColor(c, b.attribute("color"));
} else {
// try style attr
QString style = b.attribute("style").simplified();
const QStringList substyles = style.split(';', QString::SkipEmptyParts);
for (QStringList::ConstIterator it = substyles.begin(); it != substyles.end(); ++it) {
QStringList substyle = it->split(':');
QString command = substyle[0].trimmed();
QString params = substyle[1].trimmed();
if (command == "color")
m_context.styleParser().parseColor(c, params);
}
}
gc->currentColor = c;
if (b.tagName() == "linearGradient") {
QLinearGradient *g = new QLinearGradient();
if (gradhelper.gradientUnits() == SvgGradientHelper::ObjectBoundingBox) {
g->setCoordinateMode(QGradient::ObjectBoundingMode);
g->setStart(QPointF(SvgUtil::fromPercentage(b.attribute("x1", "0%")),
SvgUtil::fromPercentage(b.attribute("y1", "0%"))));
g->setFinalStop(QPointF(SvgUtil::fromPercentage(b.attribute("x2", "100%")),
SvgUtil::fromPercentage(b.attribute("y2", "0%"))));
} else {
g->setStart(QPointF(SvgUtil::fromUserSpace(b.attribute("x1").toDouble()),
SvgUtil::fromUserSpace(b.attribute("y1").toDouble())));
g->setFinalStop(QPointF(SvgUtil::fromUserSpace(b.attribute("x2").toDouble()),
SvgUtil::fromUserSpace(b.attribute("y2").toDouble())));
}
// preserve color stops
if (gradhelper.gradient())
g->setStops(gradhelper.gradient()->stops());
gradhelper.setGradient(g);
} else if (b.tagName() == "radialGradient") {
QRadialGradient *g = new QRadialGradient();
if (gradhelper.gradientUnits() == SvgGradientHelper::ObjectBoundingBox) {
g->setCoordinateMode(QGradient::ObjectBoundingMode);
g->setCenter(QPointF(SvgUtil::fromPercentage(b.attribute("cx", "50%")),
SvgUtil::fromPercentage(b.attribute("cy", "50%"))));
g->setRadius(SvgUtil::fromPercentage(b.attribute("r", "50%")));
g->setFocalPoint(QPointF(SvgUtil::fromPercentage(b.attribute("fx", "50%")),
SvgUtil::fromPercentage(b.attribute("fy", "50%"))));
} else {
g->setCenter(QPointF(SvgUtil::fromUserSpace(b.attribute("cx").toDouble()),
SvgUtil::fromUserSpace(b.attribute("cy").toDouble())));
g->setFocalPoint(QPointF(SvgUtil::fromUserSpace(b.attribute("fx").toDouble()),
SvgUtil::fromUserSpace(b.attribute("fy").toDouble())));
g->setRadius(SvgUtil::fromUserSpace(b.attribute("r").toDouble()));
}
// preserve color stops
//.........这里部分代码省略.........
示例11: main
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QMainWindow* mw{new QMainWindow()};
mw->setWindowTitle("scroll event compression test case");
QToolBar* toolbar{mw->addToolBar("toolbar")};
QAction* sleep_a_lot_action{new QAction("Sleep A Lot", mw)};
sleep_a_lot_action->setCheckable(true);
sleep_a_lot_action->setChecked(false);
sleep_a_lot_action->setToolTip("When activated, the main thread will attempt to sleep for 999ms intervals with 1ms gaps between naps.\n"
"Shortcut: spacebar");
sleep_a_lot_action->setShortcut(QKeySequence(Qt::Key_Space));
sleep_a_lot_action->setShortcutContext(Qt::WindowShortcut);
QTimer* sleep_a_lot_timer{new QTimer(mw)};
QObject::connect(sleep_a_lot_action, &QAction::toggled, [&](const bool& is_on){
if(is_on) sleep_a_lot_timer->start();
else sleep_a_lot_timer->stop();});
sleep_a_lot_timer->setSingleShot(false);
sleep_a_lot_timer->setInterval(1);
QObject::connect(sleep_a_lot_timer, &QTimer::timeout, [&](){
std::this_thread::sleep_for(std::chrono::milliseconds(999));
if(sleep_a_lot_action->isChecked()) sleep_a_lot_timer->start();
});
toolbar->addAction(sleep_a_lot_action);
toolbar->addSeparator();
QGraphicsScene* gs{new QGraphicsScene(mw)};
QGraphicsSimpleTextItem* sti{gs->addSimpleText("Move the mouse wheel a few clicks up or down with various combinations of:\n"
"* \"Sleep A Lot\" enabled/disabled (shortcut to toggle: spacebar).\n"
"* Mouse held still/mouse moved about.\n\n"
"Questions whose answers differ for the same version of Qt on various\n"
"platforms (eg, Win32, X11, Aqua):\n"
"* With \"Sleep A Lot\" active, are mouse wheel events compressed?\n"
"* With \"Sleep A Lot\" active, does moving the mouse and the mouse wheel\n"
" cause scroll wheel events to be thrown away by compression that are not\n"
" thrown away when just the mouse wheel is manipulated?\n"
"* With \"Sleep A Lot\" active, is it necessary to move the mouse in order\n"
" to provoke processing of buffered mouse wheel events?")};
sti->setBrush(QBrush(Qt::black));
sti->setPen(QPen(Qt::white));
{
QFont f(sti->font());
f.setBold(true);
f.setPointSize(36);
sti->setFont(f);
}
gs->setSceneRect(sti->boundingRect());
QGraphicsEllipseItem* mc{gs->addEllipse(0, 0, 25, 25, QPen(QColor(0, 255, 0, 200)), QBrush(QColor(0, 0, 0, 100)))};
mc->setFlag(QGraphicsItem::ItemIgnoresTransformations);
QGraphicsSimpleTextItem* mcsti{gs->addSimpleText("0\n0")};
mcsti->setBrush(QBrush(Qt::white));
mcsti->setParentItem(mc);
mcsti->moveBy(23, 22);
QGraphicsDropShadowEffect* mcstige{new QGraphicsDropShadowEffect(gs)};
mcstige->setBlurRadius(10);
mcstige->setOffset(0, 0);
mcstige->setColor(Qt::black);
mcsti->setGraphicsEffect(mcstige);
TestView* gv{new TestView(gs, mw)};
QOpenGLWidget* gl_widget = new QOpenGLWidget;
{
QSurfaceFormat fmt;
fmt.setRenderableType(QSurfaceFormat::OpenGL);
fmt.setProfile(QSurfaceFormat::CompatibilityProfile);
fmt.setVersion(2, 1);
fmt.setSamples(4);
gl_widget->setFormat(fmt);
}
gv->setViewport(gl_widget);
QObject::connect(gv, &TestView::mouse_moved, [&](const QPointF& scene_pos){
mcsti->setText(QString("%1\n%2").arg(scene_pos.x()).arg(scene_pos.y()));
qreal zoom{gv->transform().m22()};
qreal delta{12.5 / zoom};
mc->setPos(scene_pos.x() - delta, scene_pos.y() - delta);
});
gv->setDragMode(QGraphicsView::ScrollHandDrag);
{
QLinearGradient g;
g.setSpread(QGradient::ReflectSpread);
QGradientStops grad_stops;
grad_stops.push_back(QGradientStop(0, QColor(Qt::red)));
grad_stops.push_back(QGradientStop(0.5, QColor(Qt::green)));
grad_stops.push_back(QGradientStop(1, QColor(Qt::blue)));
g.setStops(grad_stops);
QBrush b(g);
gs->setBackgroundBrush(b);
}
mw->setCentralWidget(gv);
mw->resize(2045, 838);
mw->show();
return app.exec();
}