PDA

View Full Version : how to mask my x::widget to the background of a shape



saman_artorious
25th February 2012, 22:48
hi there,

I gotta draw a rounded rectangle n fit MultiSlider widget inside of it. I was told I'd be able to present it with setmas().
here's the code, please add the required code for me.



void QtMultiSlider::paintEvent(QPaintEvent * event)
{
Q_UNUSED(event)

if (!isVisible())
return;

QPainter painter;
painter.begin(this);

painter.setPen(Qt::blue);
painter.setBrush(Qt::SolidPattern);
QRect rect(50, 50, 240, 100);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize);

int spacing = 30;
int labelHeight = 0;

int valueBarWidth = 300;

// Calculating the maximum string size

int valueBarX = spacing;
int valueBarBottomY = 0;

QSizeF originalSize = m_rendererValueBarTop->defaultSize();
QSizeF targetSize = originalSize;
targetSize.scale(QSizeF(valueBarWidth, originalSize.height()), Qt::KeepAspectRatio);
qreal scaleRatio = targetSize.width() /
originalSize.width();

QSize valueBarBottomSize = m_rendererValueBarBottom->defaultSize() * scaleRatio;

valueBarBottomY = height() - labelHeight - 20
- valueBarBottomSize.height();

m_valueBarRightRect = QRect(QPoint(valueBarX, 20),
QSize(valueBarWidth, valueBarBottomY));

m_rendererValueBar->render(&painter, m_valueBarRightRect);

int filledPixels = ((double)value()) / maximum() * m_valueBarRightRect.height();
QRect filledRect = m_valueBarRightRect;
filledRect.setTop(filledRect.bottom() - filledPixels);
m_rendererValueBarFilled->render(&painter, filledRect);


painter.end();

}

AlexSudnik
27th February 2012, 10:25
Heh , asking someone to do the job for you doesn't sound right (at least on this forum). As you were told you can use greyscale images to mask desired areas.
Look at the void QWidget::setMask ( const QBitmap & bitmap ) description for full info.