Resize QToolButton with text - c++

I have a QToolButton with a text using toolButton->setText(....). However, the text is truncated when the button is rendered. I have tried increasing the width of the button using resize() and setFixedSize but the text is centered and still truncated. Any ideas how to make the button follow the width of the text ?

You can use QFontMetrics to calculate the minimum size needed to display the whole text. The boundingRect method returns a QRect corresponding to the size of your text. You can specify flags like Qt::AlignHCenter.
http://qt-project.org/doc/qt-5.0/qtgui/qfontmetrics.html#boundingRect-4
You can subclass QToolButton and reimplement the setText() method to include a call to resize() or manage the size when you call setText().

Try to set the same minimumSize(w,h) and maximumSize(w,h) with correct values, I mean, for example:
In Design, in the property window of your button:
width and height in geometry - 80x88, minimum and maximum must the
same.
It works for me in my case. And pay attention on icon size of the button, if it is.

Related

How to I resize an item in a QListWidget upon the resize of the window?

I am using a QStyledItemDelegate to change the appearance of the QListWidgetItems in my QItemWidget. I want to change the height of the items depending on the size of the text. I am using sizeHint() which works fine on the initial creation. The problem is when the window resizes, the text reformats and leaves empty spaces. How do I resize the items to remove the empty space?
setResizeMode to QListView::Adjust instead of QListView::Fixed.

Get size of QScrollArea viewport before showing

I have a custom QDialog comprised of a QStackedWidget with QScrollArea widgets for each page of the stacked widget.
I want to set the size hint for the QDialog such that the dialog is just large enough that the scroll bars for the scroll area are not visible when the dialog is first shown (i.e. ensure size of QScrollArea viewport = size hint of child widget in scroll area). Currently, the default sizeHint() implementation for the QDialog has insufficient height, which causes the vertical scroll bar to be shown when first loaded.
I thought this could be achieved by re-implementing sizeHint() for the QDialog, whereby the size hint of the dialog would be adjusted by the amount required for the size of QScrollArea viewport to equal the size hint for child widget in the scroll area (for the first page of the stacked layout). Unfortunately, in sizeHint(), the size of the QScrollArea viewport is set to the default size of QStackedWidget (640x480), and only updates to the correct size once the QDialog is shown.
Is there some way to get the correct size of the QScrollArea viewport before it is shown, or another way to achieve the desired effect of adjusting the size hint of the dialog to prevent scroll bars from being shown when it is first displayed (aside from hard-coding the dialog size).
With the composition of your dialog as:
I have a custom QDialog comprised of a QStackedWidget with QScrollArea
widgets for each page of the stacked widget.
The tricky part is to answer:
Is there some way to get the correct size of the QScrollArea viewport
before it is shown?
Well, before switching to certain page you can estimate the scroll area viewport if it is either correctly set or you can just measure the content going inside the scrollarea. I usually force the widget to demand certain height from the scroll area like that:
wdgetInScrollArea->setMinimumSize( widgetInScrollArea->sizeHint() );
wdgetInScrollArea->adjustSize(); // sometimes it is needed
The the scroll area viewport hint is then more 'adequate':
qDebug() << scrollArea->viewPortSizeHint(); // report
I don't see the code but usually it is not even required to do any custom event handling here, just prepare all the nested widgets like that.

QScrollArea vertical sizePolicy - Fit contents up to maximum height

I've had a real challenge getting QScrollArea to take the minimum space possible up to a maximum height.
My GUI model is as follows: A QScrollArea contains a vertical layout which is populated with a widget of class TableRow. I want this class TableRow to take up the minimum height possible. It has a widget at the top which is always visible, and a QScrollArea below which has a label inside it whose visibility can be toggled. The label is for notes which may be 0 characters or may be infinite in length (hardware limitations aside).
I've found that for a label in class TableRow setting the vertical sizePolicy to Fixed will actually take up exactly how much it needs to fit all the contents (see: Qt Layout, resize to minimum after widget size changes). However this doesn't appear to work with QScrollArea. In fact every sizePolicy I've tried keeps the QScrollArea at a fixed height; except for Ignore, but then the QScrollArea goes to a height of 0, regardless of its contents.
I've created a git branch producing a simplified version of this problem.
Here is the result of applying a fixed vertical sizePolicy:
What I'm expecting from this test case:
The first widget's height should be almost 30px (the height of the upper widget) only showing the borders for the QLabel and QScrollArea
The second widget's height should be shorter than 130px (the maximum height of the QScrollArea being 100px) but large enough to show the label without scrolling
The third widget's height should be 130px, and the scrollbar should appear (this part is correct in every case I've tried except for when the vertical sizePolicy is set to Ignored )
I understand I may need to override some things to make this work, as by itself it's not obvious why a QScrollArea's height might be dependent on its child widgets (which is probably why it was not designed to make this easy, or at least it seems like it wasn't).
However, I think the case I'm trying to make is common enough, and my current approach is justifiable. If there's another/better way to make an individual widget scroll after it reaches a maximum height I'm open to that as an answer, provided it meets the three conditions I'm expecting.
This feels more like a hack than a solution, but it does work for me, at least in the short term. Because the text for lblNotes does not change runtime, I was able to add the following code in the constructor of my TableRow widget:
// Hack to resize QScrollAreas
ui->lblNotes->adjustSize(); // Otherwise lblNotes will think its height is still ~0px
int height = ui->lblNotes->height() + 12; // Borders and margins add up to 12px
if (height > 100) { height = 100; } // Cap the height at desired maximum value
ui->scrollArea->setFixedHeight(height);
Should I have to deal with the case of dynamically set text, I could wrap this in a function to be called anytime the text of lblNotes is set.
I'm still open to solutions that involve using the layout features Qt has natively as I believe that would be preferred if a solution exists. Some QScrollArea contents may not be as straight-forward to determine the height from in the future.

Qt: How to create a scrollable, owner-drawn widget

I need to create a scrollable, owner-drawn widget that behaves a lot like QPlainTextEdit with word-wrapped text, in the sense that the height depends on the width - as the content width decreases, the content height increases.
What is the best approach to do it? I was thinking about putting my QWidget-derived class inside a QScrollArea, but QPlainTextEdit is derived from QScrollArea instead, should I go that route?
Also, I want to paint only the visible area in paintEvent(), it would be wasteful otherwise.
Right now I'm examining the code of QPlainTextEdit, but it is rather complex and not easy to read, so if anyone knows of a code example that's simpler on the web, you can give me a link, it would help a lot.
I'll post the solution I came up with. It's not the best, but it mostly works.
I did not derive from QAbstractScrollArea in the end, instead I simply embedded my widget in a QScrollArea with a vertical layout, which worked well-enough.
I implemented resizeEvent() (I saw this from QPlainTextEdit implementation), and each time the width changes, I recalculate the height, and I set the widget's minimum height to that. I set the minimum height because of how the layout works.
void MyWidget::resizeEvent(QResizeEvent *e)
{
// If the widget's width has changed, we recalculate the new height
// of our widget.
if (e->size().width() == e->oldSize().width()) {
return;
}
setMinimumHeight(calculateHeightFromWidth(e->size().width()));
}
For drawing only the visible area see Get visible area of QPainter

In Qt, how do I make a dialog un-resizeable, yet automatically adjusting its size to the contents?

I have an instance of QDialog, populated by widgets using code generated by uic. The dialog contains a few labels laid out vertically, and I am popping the dialog from time to time to show some text in these labels. The text can be multi-line and its length is not pre-determined. I set the vertical size policy to fixed, so the user can't drag it (doesn't make sense), but I also want the dialog to change its size before being shown to accomodate for the current size of the labels.
To this end, I was calling QWidget::adjustSize() on the QDialog before displaying it, but it doesn't work as expected. When the dialog is shown, it seems to retain the (wrong) size from the previous displaying, but when I click the mouse in the (disabled) vertical resize mode, the dialog suddenly "snaps" to the (correct) adjusted size.
Is there any way to make my dialog appear correctly?
EDIT: I tied rubenvb's advice, and ended up with this:
QSizePolicy free(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
QSizePolicy fixed(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
dialog->setSizePolicy(free);
dialog->adjustSize();
dialog->setSizePolicy(fixed);
dialog->show();
Unfortunately, that didn't seem to change anything.
This isn't the answer you're hoping for, and it may not apply to what you're trying to do, however, the only way that I was able to adjust the dimensions of a QWidget at run-time was by handling the object's resizeEvent(..) method. This allowed me to calc the size of items based upon the font being used, number of lines, available space, etc., and then adjust their size accordingly before passing the 'event' on to the base resizeEvent(..) method.
My approach used a single QWidget container within a window, below a header, above a footer status area, and to the right of a column of menu buttons. The widget container, inside the resizeEvent() call, would look at the objects it was going to display, calculate the font heights being used, and then resize some items according to their dimensions (because of how the style sheet selected fonts and colors, etc) and then adjust the sub-widget dimensions before allowing the container widget to get the resizeEvent() message.
So I wasn't so interested in setting a window size, but I think the container QWidget might work the same way? I was more interested in setting the dimensions to some asthetically pleasing size, depending upon the dimensions of the display.
Hope you find that helpful.
Do everything in the right order:
Dialog is not shown. Dialog is resizeable.
Calculate new size, set new size.
Set dialog to not-resizeable.
Show Dialog.
Hide dialog, go to step one.