PDA

View Full Version : QStyleOptionProgressBar doesn't display right in OSX



xgoan
15th June 2007, 13:28
Hi,

Anyone has tied this style in a Mac? The bar is always disabled and no text appears

Thanks

jpn
15th June 2007, 13:41
Does a plain QProgressBar work ok? Maybe an important setting was forgotten from the style option? See QProgressBar sources for reference how to fill it properly?

wysota
15th June 2007, 13:43
What style? QStyleOptionProgressBar is a little class that holds information about the parameters of an element that is to be drawn. If you want to use it, you have to fill this object with parameters.

xgoan
15th June 2007, 14:03
Sorry the question was drawing a progress bar delegate in OSX, like the Qt Torrent example one (that fails too).

ATM I have solved it using a QPlastiqueStyle that's multiplatform.

Thank's

jpn
15th June 2007, 14:10
But does a plain QProgressBar get drawn ok? I mean the widget, not custom drawing.

chuckshaw
27th June 2007, 19:07
an example of something you would use in the paint method of your delegate would be this:



QStyleOptionProgressBarV2 progressOptions;

progressOptions.rect = option.rect;
progressOptions.minimum = 0;
progressOptions.maximum = 100;
progressOptions.progress = 50;
progressOPtions.text = QString("%1%").arg(50);
progressOptions.textVisible = true;

QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressOptions, painter, 0);


Where all of the values you would pull out of your model instead of hard coding them like i did in this example. The last line (setting textVisible to true) is what tells it to display text. If you don't set that to true it will only draw the physical progress bar without any text. I hope that helps you out.

jpn
27th June 2007, 19:32
The torrent example (http://doc.trolltech.com/4.3/network-torrent.html) 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.
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);

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:


void QProgressBar::initStyleOption(QStyleOptionProgress Bar *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);
}
}

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?