Set horizontal scroll of CListBox row back after resetting content - c++

I got a multiple selection CListBox with horizontal scroll bar enabled and showed correctly. Problem is, that when I use function
lst.ResetContent() and fill it back, I can't find way to scroll text in the rows back to the same position. I tried to use
lst.SetScrollPos(SB_HORZ, horizScroll, TRUE); , where horizScroll = lst.GetScrollPos(SB_HORZ); This works correctly on scroll bar itself, but
text in the row stays not scrolled (manual scrolling functions OK).
Structure of my program is:
CListBox lst;
int horizScroll;
/*Periodically doing code bellow*/
//Get current scroll position
horizScroll = lst.GetScrollPos(SB_HORZ);
//Reset current content
lst.ResetContent();
//Add item into CListBox (UNICODE in my application)
lst.AddString(L"Some longer text then width of CListBox");
//Calculate horizontal extent and set it through
lst.SetHorizontalExtent(calculatedWidth);
//Try to scroll text (scrolls only scroll bar, not text itself)
lst.SetScrollPos(SB_HORZ, horizScroll, TRUE);
UpdateData(FALSE);
Thanks in advance!
EDIT:
As "rrirower" answered correctly,
lst.PostMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, 250), 0);
message does the job. Scroll position from horizScroll works perfectly. I suggest posting this message twice, because if you do it only once, text is re-scrolled visually from beginning to the wanted position. When you post it twice, text visually stays at the correct position and scroll bar just quickly comes to the right place.

If I understand you correctly, you're trying to scroll the text in the list box horizontally using the program code. If you use Spy++, you'll see that when you manually scroll, using the mouse, a series of WM_HSCROLL messages is posted to the list box control. You can accomplish the same thing by doing this...
lst.PostMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, 250), 0);
You need to calculate the position (I used 250 above), but, the above code should move the text and the scroll bar horizontally.

After some reading it seems that Invalidate should do the trick. Since as I understand you have one text line this should be fine, however if the painting itself is complex and requires resources you can use ScrollWindowEx and then InvalidateRect on the rectangle returned by the latter to repaint only the changed area.

Related

How can I overlap qwidgets while using the grid layout and positioning overlapping widgets a particular distance from the window border?

I am programming a game and I have a tab widget which takes up the majority of the window. I want to use the extra space in the tab bar for buttons. I have the tab widget in a grid layout. To accomplish this, I use the code below in order to remove and add back the button widgets to the desired areas (the solution to someone else's question).
ui->centralLayout->removeWidget(ui->exitButton);
ui->centralLayout->removeWidget(ui->ResizeButton);
ui->centralLayout->addWidget(ui->ResizeButton,0,4, Qt::AlignTop|Qt::AlignRight);
ui->centralLayout->addWidget(ui->exitButton,0,4, Qt::AlignTop|Qt::AlignRight);
This does not work for me; however, because I would like the second widget-- the resize button-- to be just to the left of the exit button. What is occurring is that it instead overlaps the exit button. I simply need to move it 21 pixels to the left and have no idea how!
I tried putting both buttons in a frame and then removing and adding the frame the way I did the buttons. Unfortunately the same functions I used do not exist for the qt frame object.
Here are some pictures of my window.
https://docs.google.com/document/d/17w5USWQcCtb6OdcRShdcYcRjXTcdVpmdrG5TWLX71y8/edit?usp=sharing
you are using void QGridLayout::addWidget(QWidget * widget, int row, int column, Qt::Alignment alignment = 0) overload.
2-nd and 3-rd parameters are row and column of a grid. And you put 2 widgets in the same cell so they are overlaping each other.
I solved my problem. Earlier when I was trying to add them to a frame and reposition it I could not but using a widget as the container for my buttons let me place them the way I was earlier attempting to individually place the buttons.

QTableWidget show scroll bar

I would like the horizontal scroll bar to appear whenever there is text eliding. Such that the user won't have to resize the whole GUI. How would I do this?
This is what I have coded:
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(1,QHeaderView::Stretch);
ui->tableWidget->resizeColumnsToContents();
I also tried enabling scrollbar to appear always, but scrolling to the very right doesn't do anything.
If I set textElideMode to ElideNone , the text from the 2nd column is partially hidden and no scrollbar appears.
QHeaderView::Stretch will stretch the column width to the available space. Use QHeaderView::ResizeToContents to make the column wide enough to display the content, resulting in a horizontal scroll bar if necessary.
This will have a couple of side effects of which I'm not sure you want them.
There will probably be no more ellipsis in the elided text.
If all of the values in your Hash column are very small, then that column will be very thin, so there might be 'empty' space next to that column.

Prevent a QTextEdit widget from scrolling when there is a selection

I've researched this extensively but haven't found a satisfactory solution yet:
How do I append text at the end of QTextEdit widget without triggering a scroll to the bottom of the widget when either of these conditions is met:
The user has selected some text.
The user has scrolled away from the bottom.
(In all other cases, a scroll to the bottom of the QTextEdit widget should be triggered.)
Here is the code I'm currently using to append text at the bottom of a QTextEdit widget:
const QTextCursor old_cursor = widget.textCursor();
widget.moveCursor(QTextCursor::End);
widget.textCursor().insertText(text);
if (old_cursor.hasSelection())
widget.setTextCursor(old_cursor);
else widget.moveCursor(QTextCursor::End);
This partially takes care of condition 1: the problem is that the view will still scroll until only the last line of the selection is visible, at which point it will indeed stop scrolling.
Condition 2 is not taken care of at all: some posts suggest to save the position of the vertical scrollbar and restore it after the text was appended, however I don't think this is correct since the scrollbar should move upward when text is appended, even though the view stays still.
Note that I'm using QTextCursor::insertText() instead of QTextEdit::append() because I need to adjust the color of the text being appended, regardless of whether the user has selected text or not.
Update: Here is the code I ended up with, thanks to Pavel's answer:
const QTextCursor old_cursor = widget.textCursor();
const int old_scrollbar_value = widget.verticalScrollBar()->value();
const bool is_scrolled_down = old_scrollbar_value == widget.verticalScrollBar()->maximum();
// Move the cursor to the end of the document.
widget.moveCursor(QTextCursor::End);
// Insert the text at the position of the cursor (which is the end of the document).
widget.textCursor().insertText(text);
if (old_cursor.hasSelection() || !is_scrolled_down)
{
// The user has selected text or scrolled away from the bottom: maintain position.
widget.setTextCursor(old_cursor);
widget.verticalScrollBar()->setValue(old_scrollbar_value);
}
else
{
// The user hasn't selected any text and the scrollbar is at the bottom: scroll to the bottom.
widget.moveCursor(QTextCursor::End);
widget.verticalScrollBar()->setValue(verticalScrollBar()->maximum());
}
Saving and restoring scrollbar position is quite correct and works perfectly. When document's length is increased, scrollbar's maximum value is increased. But its value is still equal to number of pixels above the viewport. So when you add contents to the document and set the same scrollbar value repeatadly, scrollbar's handle will move to the top, but the content will remain immobile.
It seems that you already know how to check if the user has selected some text. To check if the user has scrolled away from the bottom, you should simply compare vertical scrollbar's value with its maximum.

Wrong stem position in balloon-style tooltips in report list-view

I'd like to change the default style of the list-view control's tooltips to balloon.
I first called ListView_GetToolTips() to get the HWND of the list-view's tooltips control, and then used GetWindowLongPtr()/SetWindowLongPtr() to add the TTS_BALLOON style.
I handle LVN_GETINFOTIP to customize the tooltip for the items (first column) in the list-view: the tooltip texts that appear for the first column items are actually a copy of the text of the third column. The other columns (subitems) are managed automatically by the list-view.
The balloon-style tooltips for the first column items seem OK; their stems are correctly positioned:
But the tooltips for the second column seem drawn wrongly, e.g. the balloon is drawn as if it was referred to a subitem in a row below the actual row pointed by the mouse cursor.
In the following picture, the "star" indicates the position in which the mouse cursor was when the tooltip appeared, but the tooltip's stem points to a row below, marked with an ellipse:
The strange thing is that the tooltips for the third column seem drawn correctly.
Is this a bug in the list-view control? (I'm using Windows 7.)
Or what am I missing here?
The ListView uses a tracking tooltip and positions it to unfold and reveal the hidden text in a column that's too small. It's not expecting its tooltip to be a balloon and so doesn't compensate for that.
You'd need to sub-class the tooltip itself, watch for TTM_TRACKPOSITION messages from the ListView, and adjust the coordinates.
Your second question - the shaded background comes from the system theme. You should be able to get it by calling SetWindowTheme on the tooltip (I'm not sure why the ListView disables themes for the tip).

CListCtrl: How to maintain scroll position?

I have a CListCtrl (report style) where I clear the list and repopulate it at certain times. I'd like to maintain the vertical scroll position when doing this. I see there are a couple methods that look promising:
EnsureVisible()
GetScrollPos()
SetScrollPos()
GetScrollInfo()
GetTopIndex()
Scroll()
I'm trying GetScrollPos() and then SetScrollPos() but it doesn't appear to be working. What is the simple correct way to save a scroll position and then later restore it?
UPDATE
Actually it seems I can get to save the scroll position GetScrollPos() and then SetScrollPos() to restore it, however it literally just seems to set the scroll bar position and does not actually scroll the items of my CListCtrl.
UPDATE 2
The Scroll() method seems to correctly scroll the scrollbars and the contents. However it takes a CSize object as it's argument. So the question would be how to translate between the CSize and the output of either GetTopIndex or GetScrollInfo/Pos.
I've done that in the past. IIRC, the trick consisted in:
int topIndex= m_List.GetTopIndex();
RenewContents();
m_List.EnsureVisible(m_List.GetItemCount() - 1); // Scroll down to the bottom
m_List.EnsureVisible(topIndex);// scroll back up just enough to show said item on top
Another way to do it is like so:
CRect r;
m_lcList.GetItemRect(0, r, LVIR_BOUNDS);
int scrollPos = m_lcList.GetTopIndex() * r.Height();
RenewContents();
m_lcList.Scroll(CSize(0, scrollPos));