The torrent example does set the text visible. This is how the example fills the style option:
// Set up a QStyleOptionProgressBar to precisely mimic the
// environment of a progress bar.
progressBarOption.
state = QStyle::State_Enabled;
progressBarOption.
direction = QApplication::layoutDirection();
progressBarOption.rect = option.rect;
progressBarOption.
fontMetrics = QApplication::fontMetrics();
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.textVisible = true;
// Set the progress and text values of the style option.
int progress = qobject_cast<MainWindow *>(parent())->clientForRow(index.row())->progress();
progressBarOption.progress = progress < 0 ? 0 : progress;
progressBarOption.
text = QString().
sprintf("%d%%", progressBarOption.
progress);
// Draw the progress bar onto the view.
// Set up a QStyleOptionProgressBar to precisely mimic the
// environment of a progress bar.
QStyleOptionProgressBar progressBarOption;
progressBarOption.state = QStyle::State_Enabled;
progressBarOption.direction = QApplication::layoutDirection();
progressBarOption.rect = option.rect;
progressBarOption.fontMetrics = QApplication::fontMetrics();
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.textVisible = true;
// Set the progress and text values of the style option.
int progress = qobject_cast<MainWindow *>(parent())->clientForRow(index.row())->progress();
progressBarOption.progress = progress < 0 ? 0 : progress;
progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress);
// Draw the progress bar onto the view.
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
To copy to clipboard, switch view to plain text mode
It's interesting that the snippet above doesn't work in Mac. I would be really interested to hear whether an ordinary QProgressBar widget works as expected. QProgressBar fills the style option like this:
{
if (!option)
return;
option->initFrom(this);
if (d->orientation == Qt::Horizontal)
option
->state |
= QStyle::State_Horizontal;
option->minimum = d->minimum;
option->maximum = d->maximum;
option->progress = d->value;
option->textAlignment = d->alignment;
option->textVisible = d->textVisible;
option->text = text();
= qstyleoption_cast<QStyleOptionProgressBarV2 *>(option)) {
optionV2->orientation = d->orientation; // ### Qt 5: use State_Horizontal instead
optionV2->invertedAppearance = d->invertedAppearance;
optionV2
->bottomToTop
= (d
->textDirection
== QProgressBar::BottomToTop);
}
}
void QProgressBar::initStyleOption(QStyleOptionProgressBar *option) const
{
if (!option)
return;
Q_D(const QProgressBar);
option->initFrom(this);
if (d->orientation == Qt::Horizontal)
option->state |= QStyle::State_Horizontal;
option->minimum = d->minimum;
option->maximum = d->maximum;
option->progress = d->value;
option->textAlignment = d->alignment;
option->textVisible = d->textVisible;
option->text = text();
if (QStyleOptionProgressBarV2 *optionV2
= qstyleoption_cast<QStyleOptionProgressBarV2 *>(option)) {
optionV2->orientation = d->orientation; // ### Qt 5: use State_Horizontal instead
optionV2->invertedAppearance = d->invertedAppearance;
optionV2->bottomToTop = (d->textDirection == QProgressBar::BottomToTop);
}
}
To copy to clipboard, switch view to plain text mode
As you can see, the only remarkable difference is QStyleOption::initFrom() which initializes the state, direction, rect, palette, and fontMetrics. I can see from QStyleOption::initFrom() sources that some special tricks are done with state when Q_WS_MAC is in question. Maybe it's because of that?
Bookmarks