qt status bar center align? - qt

How can I center align the text in QStatusBar?
By default it is always left aligned.
I know I can add a QLabel and set alignment, but I want to use plain text, and .showMessage(QString, int) method, so I can add the timeout value.

A QStatusBar has three functions of note here:
addPermanentWidget - Places a widget right aligned
addWidget - Places a widget left aligned which can be obscured by status messages
showMessage - Displays a status message
These are well established standards for status bars. While you could hack away to get what you're looking for, I'd suggest you reconsider your needs. Perhaps your QLabel should be placed with addPermanentWidget instead?
Take a look at the docs for more info: http://doc.qt.io/qt-5/qstatusbar.html

If you just want to center the message itself in the whole statusBar, go like this:
QLabel* statusLabel = new QLabel("Your Message");
statusBar()->addWidget(statusLabel,1);
This additional parameter 1 stretches your Label to the complete width of the statusbar.
Greetings

Related

How to show several QTableViews(without scrollbar) in QListWidget(or other similar widgets)?

I have a problem with showing multiple tables(without their own scrollbars) under one scrollbar. Is there any workaround or a good way to resolve this issue in Qt?
I've tried to do what you ask, and found this. So, here is a solution:
add QScrollArea to a form
set the property widgetResizable to true
put QWidget to scroll area
right click on widget -> Set ancestor -> [your scroll area]
add vertical layout to a widget
scroll area will collapse, epand it with a mouse
insert into the widget as many tables as you want
set vertical size policy for each table to Minimum and set minimal vertical size.
Here is how it looks:

Init QListWidget to show its all content (with no scroll bars)

I was playing with the Config Dialog Example, all was fine. Then I changed something in the right part of the dialog.
Then I found the contentsWidget (QListWidget) in the left part of the dialog became smaller and showed the scroll bars (both Horizontal and Vertical).
I want the QListWidget to show all its content so that no scroll bars are needed.
All items are added at the beginning and fixed. No dynamic.
I guess there is a simply method to let the QListWidget expand to show all its content at the beginning.
Could anyone help me and tell me the magic word?
Here is the code:
contentsWidget = new QListWidget;
contentsWidget->setViewMode(QListView::IconMode);
contentsWidget->setIconSize(QSize(96, 84));
contentsWidget->setMovement(QListView::Static);
contentsWidget->setMaximumWidth(128);
contentsWidget->setSpacing(12);
//contentsWidget->setMinimumWidth(contentsWidget->sizeHintForColumn(0));
//contentsWidget->setMaximumWidth(contentsWidget->sizeHintForColumn(0));
//contentsWidget->adjustSize();
//qDebug()<<contentsWidget->sizeHintForColumn(0);
createIcons();
contentsWidget->setCurrentRow(0);
QHBoxLayout *horizontalLayout = new QHBoxLayout;
horizontalLayout->addWidget(contentsWidget);
horizontalLayout->addWidget(pagesWidget, 1);
I tried contentsWidget->sizeHintForColumn(0), but it didn't work. It was 0. I tried some other methods but nothing worked.
I think you should try:
contentsWidget->setMinimumWidth(128);
This will ensure that no matters what, the size of contentsWidget will always be at least 128, hence large enough to contains the icons.

QTextEdit: scroll down automatically only if the scrollbar is at the bottom

There's a QTextEdit that displays quite a lot of text. It is NOT editable. Suppose I want to read something around the beginning, scroll up, but then a new line is added and the scrollbar automatically goes to the bottom. I experience similar problems when using various programs (regardless of the language they were written in). How does one deal with this problem?
The behavior I want when a new line is added to the text:
if the scrollbar is at the bottom, scroll down automatically.
if the scrollbar is elsewhere, don't scroll
I suppose that
ensureCursorVisible()
is not the solution, since the QTextEdit is not editable, the user won't click inside it, and the position of the cursor is not the same as the position of the vertical scrollbar.
I would make Scroll bar position listener, which will remember position on scrolling (and also check is it at the bottom or not).
Then, when new line is added, check is it at bottom, if is scroll down, if is somewhere else then scroll back to that position.
Check this QScrollBar, you can grab it from QTextEdit via horizontalScrollBar() and verticalScrollBar().
More concrete, I would connect slot with signal from QScrollBar - valueChanged(int value) and play with values as it is described here.
It is not necessary to connect a scrollbar listener. Just query the scrollbar before appending text:
QScrollBar *scrollbar = textedit->verticalScrollBar();
bool scrollbarAtBottom = (scrollbar->value() >= (scrollbar->maximum() - 4));
int scrollbarPrevValue = scrollbar->value();
The "minus 4" hack in scrollbarAtBottom is necessary since ensureCursorVisible() does not scroll exactly to the bottom, but some fixed amount above. Check it with your font sizes.
Now you can insert the text:
textedit->moveCursor(QTextCursor::End);
// begin with newline if text is not empty
if (! textedit->document()->isEmpty())
textedit->insertHtml(QStringLiteral("<br>"));
textedit->insertHtml(QStringLiteral("My text here."))
After that operation, either scroll to the bottom, or fix the scrollbar such that it does not move at all:
if (scrollbarAtBottom)
textedit->ensureCursorVisible();
else
textedit->verticalScrollBar()->setValue(scrollbarPrevValue);

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.

QScrollBar Snap to value

I'm wanting to implement a scroll bar that snaps to particular values (like windows can snap to the edge of a screen). The idea is that as I drag the scrollbar down it snaps the bar to values as it approaches them.
My scenario is displaying 3 chapters of text. I would like to be able to snap to the beginning or end of a chapter. Of course, to go to the start of the first chapter the scrollbar one can just scroll to the top and likewise with the end of the third chapter. So I'd like to draw two lines on the scrollbar to represent the start of the second and third chapters and then have the top and bottom of the scrollbar snap to those lines. So I'm really actually wanting to use this within a QTextBrowser but I can control a QTextBrowser with a QSnapScrollBar I just don't really know where to start.
Any help would be greatly appreciated.
You could roll your own custom scrollbar class that inherits from QScrollBar or perhaps QAbstractSlider and override the valueChanged(int) signal. Some basic pseudocode:
if currentValue is within 5 of snapvalue1:
set scrollbar value to snapvalue1
elsif currentValue is within 5 of snapvalue2:
set scrollbar value to snapvalue2
...
update/redraw widget
Though I have a feeling that this might seem jerky to the end user.
You could also investigate the pageStep property (which controls how far it will scroll when using the Page Up/Down keys), maybe that could be used to serve your purposes

Resources