Set height of QLineEdit - qt

I'm trying to set the height of my QLineEdit box - to match my button next to it. No matter what I try I can't seem to get it to change.
I've tried changing the sizePolicy to expanding, setting minimum, maximum and baseSize - height to any value, no effect.
I've tried to add a qss rule - with height to any value - no effect.
I've tried to put it in a QFrame wrapper - setting a size on this - and try to make the QLineEdit box expand to fill up this - no effect.
Any ideas?

Ah!
I found the culprit! I had a line of code running: element.setFixedHeight(..)
After removing this - setting sizePolicy to expanding worked wonders!

Related

How can I set a QPushButton to grow vertically?

Background: I need Command Link-like controls. Normally, I would use Qt's builtin QCommandLinkButton, but in this case I need to also support Right-to-Left layouts. Regrettably, QCommandLinkButton doesn't seem to respect Qt::RightToLeft when set explicitly via setLayoutDirection. There also doesn't seem to be a way to override the layout direction via style sheets.
I tried to work around this issue by using QPushButton: I instantiated QPushButton and replaced its contents with a custom layout containing two labels stacked vertically. This did not work because QPushButton refused to expand horizontally.
Failing to make QPushButton instances work, I decided to try sub-classing it. No matter what I tried (overriding sizeHint, setting vertical sizePolicy to expanding, resizeing explicitly, etc) the button refuses to grow more than 30 pixels high.
If I change QPushButton to QWidget, the new widget grows normally.
The question: How can I force a QPushButton to grow vertically and fit its inner widgets? If this is not possible, what are my options for a button-like widget that is both visually and behaviorally consistent with QPushButton and other "native" controls?
I managed to get the button to grow normally by setting the maximum height to 16777214 (instead of the default 16777215), and then setting the content margins of the button to 10.
Perplexingly, the default maximum height of a QPushButton should be plenty, but for some reason it refuses to grow past 30 pixels unless the maximum height is explicitly set to something else.
Place the button inside a layout and set its vertical size policy to Maximum.

How to set Qdialog to have a fixed height as small as possible and an expending width in Qt Creator?

I have a Qdialog with a horizontal layout and a couple of elements. Now, I want the height to be fixed at its minimal possible size (while the width is still resizeable).
There are a couple of properties I can set, for example:
sizePolicy, minimumSize, maximumSize, baseSize, layoutSizeConstraint
I tried to understand how the values interact and a couple of combinations but none got me what I want.
For example, I can enter the smallest Height possible in maximumSize, in my case 178, and set the Vertical Policy in sizePolicy to Fixed. But then it is only fixed to a small range: I can still resize the dialog's height slightly to make it slightly smaller.
This type of tasks can not be done with Qt Designer, the minimum size if you use layouts is the sizeHint(), for example in your case the solution is:
dialog.setFixedHeight(dialog.sizeHint().height());
Try to set minimumSize.Height and maximumSize.Height with same values
or
Just set fixed height in code
Dialog dialog;
dialog.setFixedHeight(dialog.height());
dialog.exec();

QTextEdit::adjustSize() not working?

Setting text for QTextEdit:
te->setPlainText(“Something”) ;
te->adjustSize();
should wrap around “Something” only, instead the QTextEdit is expanding to its maximum Width-Height, can’t fix it..
When I select “Something” on run time, only “Something” is highlighted, no added extra white spaces.
Expectations: when text is small enough to fit on one Line, the text edit shouldn’t expand in height, when the text needs to wrap, only the extra line width should be added not the maximum width.
if adjustSize(); is not called, the text will wrap on the width that was set in the .ui in the Creator, won't dynamically expand horizontally nor vertically..
Some Info:
Horizontal Policy: ExpandingVertical Policy : MinimumExpanding
minimumSize : 2×22maximum Size : 300×100lineWrapMode:
WidgetWidth
Yes, looks like there is no easy way to count lines in QTextEdit.
adjustSize() is made for QWidget and is not reimplemented for QTextEdit, it is based on sizeHint().
You can use your own method to count lines, f.e.
You can use QFontMetrics to calculate width of every word in your text
You can set height to 22 and increment it until maximumHeight hitted or vertical scrollbar dissapears.
You can get some info from sources of QTextEdit itself and subclass it, reimplementing something (adjustSize()?) there.

Enable QLabel to shrink even if it truncates text

How can I get a QLabel to be resized even if it means truncating its containing text? I have a QLabel stretching the whole horizontal space of a Widget. When setting its text I make sure it is correctly truncated, ie getting its FontMetrics and Width and using metrics.elidedText().
But when the user resizes the widget the Label doesn't allow it to shrink any further since it would truncate its text.
Any ideas how to solve this? The simplest solution I think would be to somehow tell the QLabel to always shrink and then catch the resize event and correctly format the text - I just have no idea how to do the first part (different size policies don't help)
Although you mention that setting size policies didn't help, setting the QLabel's horizontal size policy to QSizePolicy::Ignored should tell the containing layout manager to ignore any minimum size hint from the label. An alternative would be to set the QLabel's minimum horizontal size to a non-zero value, like 1. If neither of these work then there is something else interfering.

How can I change the width of a QMainWindow separator without it affecting the QDockWidget

I have a QDockWidget which has been added to a QMainWindow. I set the sizeHint of the DocWidget to be 260px and its the size that I want it to be because nothing else is offering up a size.
However now I want to increase the width of the DockWidget - QMainWindow separator. The only way I have found of doing this is via a StyleSheet:
http://doc.qt.nokia.com/4.7-snapshot/stylesheet-examples.html#customizing-qmainwindow
However when I do this the DockWidget shrinks to < 100px and becomes unusable. You cannot force the QDockWidget to a certain size and its inheriting the MainWindows stylesheet.
I also cannot get any other handle on the QMainWindow::separator.
I had similar problems with QPushButtons, when rendering with stylesheets they has decreased to minimum possible size.
With them i cured that trouble by setting minimum size to needed. Maybe same solution will work for you...
you can use such code:
setStyleSheet("QMainWindow::separator {width: 20px}");

Resources