QProgressDialog: How to adjust the width to show the whole progress bar? - qt

I create a QProgressDialog, set a QProgressBar to it. Set the format
"Processing section %v of %m. Percentage completed: %p."
to the QProgressBar. But the text is cut, not the whole progress bar is displayed on the dialog.
How to adjust the width to show the whole progress bar?

Here is an example which uses QFontMetrics to get the width of the progress bar's text, and then adds 100px to this for the progress bar itself. When the dialog is shown, it is resized to this width.
auto dialog = new QProgressDialog();
dialog->setWindowTitle("Progress");
dialog->setLabelText("Test progress dialog");
auto bar = new QProgressBar(dialog);
bar->setTextVisible(true);
bar->setValue(50);
bar->setFormat("Processing section %v of %m. Percentage completed: %p");
dialog->setBar(bar);
// Use QFontMetrics to get the width of the bar text,
// and then add 100px for the progress bar itself. Set
// this to the initial dialog width.
int width = QFontMetrics(bar->font()).width(bar->text()) + 100;
dialog->resize(width, dialog->height());
dialog->show();
This allows you to avoid hardcoding a width, although it's a bit complicated. I think just setting a reasonable minimum width like below is probably a better/simpler solution:
dialog->setMinimumWidth(300);

Related

How do I size a Firefox Add-on SDK popup/panel? Firefox Add-on SDK popup/panel too small

I followed the tutorial on creating a popup for an add-on in Firefox, and it worked great.
The issue I'm having now is that the popup it creates doesn't change size to fit the content I add to it. Instead, it adds scroll bars.
So, how do I change the size of a Firefox Add-on SDK popup to show all content?
You do have to deal with that yourself.
If you already know the desired size when creating the panel, you can pass the height and width as properties of the object to the Panel() constructor.
After a panel is created, its size can be changed by setting the height and width properties on it.
height and width values are numbers in pixels.
See https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/panel for a full documentation of how to use panels.
Though the tutorial and docs never say it, the height and width of a panel always defaults to the same small size.
To change it you can use the height and width parameters for the panel class.
If you want it to change automatically you can use this code:
//index.js
var panel = require("sdk/panel").Panel({
height: 200, //choose your default
width: 200,
contentURL: data.url("popup.html"),
contentScriptFile: data.url("auto_resize_popup.js")
});
panel.port.on("resize", function (size) {
console.log(size);
panel.resize(size.width, size.height);
});
...
//auto_resize_popup.js
var content = document.body;
var size = {};
size.width = content.offsetWidth + 2; //2 extra pixels get rid of scroll bars
size.height = content.offsetHeight + 2;
self.port.emit("resize", size);

Title in AppBar grows too large

On some views I show the name of a selected item as the title of the AppBar. But when the name is very long, the VBoxcontaining the title label grows to the prefWidth of the label. You can see the result on the first picture.
As a workaround I add a styleClass to the appBar, which sets the maxWidth for the label.
view.setOnShown(e -> getAppBar().getStyleClass().add("myStyleClass"));
Although it's working, it's bad practice to set the maxWidth like that, because it can't be adjusted to different screen sizes.
How can I achieve the result on the second picture, and make the title's width adapt to different screen sizes?
A quick and dirty workaround to set the max width of the label based on the scene dimensions could be, on a View subclass:
#Override
protected void updateAppBar(AppBar appBar) {
appBar.setTitleText("Very very very long title");
((Label) appBar.getTitle()).maxWidthProperty().bind(appBar.getScene().widthProperty().subtract(100));
}
where the amount to subtract will depend on the number of buttons (nav icon, action icons or menu button) you have in that view.

Lower text in a QMessagebox

I have a QMessagebox with a custom background image. Since there is some stuff on the top side of the background image I want to see, the text of the messagebox should be lowered. Does anybody know how I can do this? I already tried throwing in some white lines using br, so:
popup.setText("<font size =5 color =white ><br>""<br>""<br>""Are you sure you
want to erase the memory</font> ");
but this screws up the background picture. Is there any way I can move the "box" that contains the text to a lower position?
You could try to get the QMessageBox' layout, get the label which holds your text and increment the labels margin. This probably is a hack and might make your project unportable. Construct your QMessageBox, call hack and then exec the box.
void hack(QMessageBox* pMessageBox)
{
QGridLayout* grid = qobject_cast<QGridLayout*>(pMessageBox->layout());
if (grid)
{
QLabel* label = qobject_cast<QLabel*>((grid->itemAtPosition(0,1))->widget());
if (label)
{
label->setMargin(label->margin()+5); // whatever is suitable
}
}
}

How can I autoscale the font size to fit the contents of a div?

I have a div with some text:
<div style="white-space:nowrap;overflow:none;width:50px;">
With some text in it
</div>
How can I scale the font size of the text so all of the text is visible?
Contrary-wise. You could wrap the text in an interior DIV, measure its width with JavaScript. Test if that width is wider than the parent DIV. Get the current font size, and incrementally move it down 1px at a time until inner DIV's width is less than or equal to the outer DIV's width.
I've been doing something like this, to set the text scale relative to the parent (or window) width / height. You can avoid jQuery by using offsetWidth and offsetHeight instead of width.
var setBodyScale = function () {
var scaleSource = $(window).width(), // could be any div
scaleFactor = 0.055,
maxScale = 500,
minScale = 75; //Tweak these values to taste
var fontSize = (scaleSource * scaleFactor) - 8; //Multiply the width of the body by the scaling factor:
if (fontSize > maxScale) fontSize = maxScale;
if (fontSize < minScale) fontSize = minScale; //Enforce the minimum and maximums
$('html').css('font-size', fontSize + '%'); // or em
}
Short Answer: You don't.
You would have to try a size, render it, see if it fits, try another size, render it see if it fits, etc. Then you have to handle the case where the calculated font size is so small no one can read the text.
There are other options, if the text doesn't fit, add an ellipsis (...) to the end of the text, when you mouse over it, the div could expand, you could use a popup window or tooltip with the full text, or put the full text in a larger area of the screen.
Find another way.
Came across this JQuery plugin in my quest to find the same.
Github
Demo
Also came across this Jquery script when I was looking for the same thing. It has the added benefit over the others, as far as I quickly tell, is that it also adjusts for height as well as width.
Comes from here: http://www.metaltoad.com/blog/resizing-text-fit-container
function adjustHeights(elem) {
var fontstep = 2;
if ($(elem).height()>$(elem).parent().height() || $(elem).width()>$(elem).parent().width()) {
$(elem).css('font-size',(($(elem).css('font-size').substr(0,2)-fontstep)) + 'px').css('line-height',(($(elem).css('font-size').substr(0,2))) + 'px');
adjustHeights(elem);
}
}

How to make QLineEdit expand inside QScrollArea

I have a QLabel and a QLineEdit inside a QWidget. When I have the widget inside a QScrollArea, the line edit does not expand to occupy the excess width of the window. When the widget is not inside the scroll area, it does expand.
I've tried setting the size policy of the line edit and the widget, to expand horizontally, but it doesn't occupy the excess space. I suspect the sizeHint() of the widget is compacted when inside a scroll area. Any ideas how to make this work?
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, None)
self.setWindowTitle('Test Window')
self.resize(500, 250)
scrollArea = QtGui.QScrollArea()
scrollWidget = QtGui.QWidget()
scrollWidget.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Maximum)
layout = QtGui.QGridLayout(scrollWidget)
label = QtGui.QLabel("Name:")
layout.addWidget(label, 0, 0)
lineEdit = QtGui.QLineEdit("Value")
lineEdit.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Maximum)
layout.addWidget(lineEdit, 0, 1)
scrollWidget.setLayout(layout)
scrollArea.setWidget(scrollWidget)
self.setCentralWidget(scrollArea)
I believe I have solved your problem.
Make the following addition to your code and it should behave correctly:
...
scrollArea.setWidget(scrollWidget)
scrollArea.setWidgetResizable(True) #add this
self.setCentralWidget(scrollArea)
...
From the docs,
widgetResizable : bool
This property holds whether the scroll area should resize the view widget.
If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.

Resources