Solved Simple sizePolicy Question
-
Hi I have a simple sizePolicy question (configuring those darned things are my greatest weakness! :) I have a QLabel I want to grow with the main window when the window is expanded however I don't want it to grow beyond the current size of the window when I set the text to something longer than the allotted space for the QLabel. So say I set the text to "awefwefawefawefawefwefawefawefawefwefawefawefawefwefawefawefawefwefawefawefawefwefawefawefawefwefawefawef" I don't want the label or main window to resize. How to achieve this?
Thanks! -
Hi,
Have a look at:
https://stackoverflow.com/questions/39533220/prevent-qlabel-from-resizing-parent-widgetIt works, but on my system (Win10 & Qt 5.10.1), the scrolling only works via the keyboard left/right arrow keys; the wheel does not scroll.
-
Thanks Diracsbracket. I might have to use your technique... for now I found another way I've been working with here (see the very bottom post for the method, just a reimplmentation of resizeEvent as below). Only problem is my QLabels sit in a vertical layout and I can't get the width of the layout with which to specify the QLabel max width. Also there are progress meters in the layout which I grabbed the width from (they are always occupying all the horizontal space of the layout) but for some odd reason the original resize upon starting the program grabs a width less than what the progress meters show as.
Is there a dummy widget I can put in the layout to get its width? Or is there a way to grab the width of the layout?
Also any idea why the width grabbed upon starting the program is shorter than the width of the progress meters? Here's the code:
void ReplicatorMainScreen::resizeEvent(QResizeEvent *event) { ui->sourceLabel->setMaximumWidth(ui->fileProgressBar->width()); }
-
I did a singleshot timer in the constructor of the MainScreen widget and now the initial max size of the QLabel is correct. Don't know why I needed this cheap hack but oh well it works.
QTimer::singleShot(100,[this] { ui->sourceLabel->setMaximumWidth(ui->fileProgressBar->width()); } );
-
I've given up on the setMaximumWidth method it was too tricky with not enough progress.
Now I have a problem with the QScrollArea method of embedding a QLabel in the QScrollArea to prevent the parent widget from resizing when the QLabel with its text doesn't fit.
Using Qt Designer, the main QScrollArea widget was automatically created as a QWidget named scrollAreaWidgetContents upon inserting the QScrollArea. I had to rename the QScrollArea main widget type to a QLabel directly in the ui file as mentioned here. However now the scroll area QLabel widget isn't aligned vertically to center, see the attached picture. "Scanning" is a QLabel and the other text is a QLabel contained within a QScrollArea. I tried adding
ui->scrollArea->setAlignment(Qt::AlignVCenter); ui->scanLabel->setAlignment(Qt::AlignVCenter);
to the constructor for the QMainWindow widget with no luck. Any ideas on how to align that QLabel vertically? Thanks.
-
A recent development: the QScrollArea is vertically aligned and also the QLabel as you can see here when I show the QScrollArea border:
and in Qt Designer the "Scanning:" QLabel and the QScrollArea are in a QGridLayout as in red:
Any ideas as to what's going on here? When there was no QScrollArea only a QLabel on the right side to show the scan directory things lined up perfect.
Thanks for the help. -
I found a workaround: I put both the unchanging "Scanning:" QLabel beside the QLabel that shows the actual directory or file being scanned in a QHBoxLayout in the QScrollArea and now they are properly aligned and all is well.