Auto resizing column widths in a CListCtrl - mfc

How can I make a CListCtrl to resize the width of its columns automatically? Usually, when an item in the list gets too long, the back end disappears from view and the user manually has to resize the width of the corresponding column.
Is there any way to do this by code?

Resizing the columns automatically is easy:
for(int i = 0;i < pListCtrl->GetHeaderCtrl()->GetItemCount();++i)
pListCtrl->SetColumnWidth(i,LVSCW_AUTOSIZE_USEHEADER);
This will optimize the columns.

Do you have the "No Scroll" option on? By default ("No Scroll" option off), if an item got too long a scroll bar would appear.

I assume you mean a list control in report mode? Unfortunately there is no way to automatically resize columns. What you can do (what I do in some places) is calculate the width of columns as you enter items, then handle WM_SIZE and resize the columns. However this causes changes that the user made to be lost, so you may need a better algorithm like tracking if the user made any changes, if he did don't resize. It's not very nice from a UX perspective, I only use it in a select amount of circumstances where the behavior makes sense in the context of the rest of the UI.

Related

Auto-resize columns of QTableView programmatically

I have a QTableView in my programm which I want to resize automatically. I used the functions
headerViewHorizontal->setSectionResizeMode(QHeaderView::ResizeToContents);
headerViewHorizontal->setSectionResizeMode(3, QHeaderView::Stretch);
to resize it and it works for when I resize the window. But then I need to insert a row in the table which could need to resize the columns. My problem is here. When I insert a row, one of the fields it too larg and the system write it with tree dots (ex. : "12.03..." instead of "12.03.2020"). I need to resize my window to make the system resize the columns.
Is there a slot which I can call to force an update of the columns width ?
Thank you in advance !
PS I'm using Qt5 with c++.
Thanks to #Scheff, my question is answered.
The solution is to use resizeColumnToContent() to one of the columns which has not the stretch resize mode. Each other will be updated.
Edit : This solution works only when the column on which you call resizeColumnToContent() has to change its width. Otherwise you have to call resizeColumnToContent(int column) on each other.

setAutoScroll with QTreeView and custom model does not work

I have made a QTreeView to display a very large and continuous data set. Since the data set is continuous, I delete initial rows when the total number of rows is greater than a specified amount.
I have used a custom model for this purpose
The whole system is working correctly and displaying data.
But I want it to autoscroll to the bottom to display latest data. If i use scrollToBottom at row addition it completely slows down the entire view-model. But If i use m_pTreeView->setAutoScroll at the start, it has not effect.
Moreover if i click on the view, it completely slows down.
I am using Qt 4.7.1
How should I auto scroll to the bottom without compromising on performance?
And show I remove the lag/drastic performance hit when I click on the view?
the whole code is available at this repo:
https://github.com/daniyalyasin93/qt_qtreeview_hugedata/

How to check if the length or width of Row in a window is greater than the width of its parent window C++

I wanted to wrap the existing text of checkbox into multiline if the width of row exceeds the width of its parent window. I am not really sure how to do that.
The image I want to show the checkbox string
The image where the string is cropped and only shows if window is resized or maximized
You are going to have a problem with this I am afraid.
The setting for making a checkbox multiline is ES_MULTILINE and if you look here you will see that it states:
To create an edit control using the CreateWindow or CreateWindowEx function, specify the EDIT class, appropriate window style constants, and a combination of the following edit control styles. After the control has been created, these styles cannot be modified, except as noted.
So, it would seem to me that you have three ways forward, depending on what you feel is the best or most elegant for you.
Set your control in the resource editor as multiline anyway. Then it doesn't matter and will wrap. No need to have to change the setting.
Implement the needed functionality to limit the size the window can be reduced to. I can show you how if you are interested. This way, if you set the control resize properties correctly it can resize larger but only reduce down the a known dimension (ie: the dimensions you created it in the resource editor).
Possibly have two controls in the same place, one as multiline and one as single. And when you decide which you want to show, swap the visibility. But I think this is a bad idea, bit of a headache, and not worth the hassle.
IMHO I would do both ideas 1 and 2 and I would happily extend my answer to provide more information.
Update
Having looked at your images and the comments about translations then there is a fourth idea. If you use a third party application to manage the translations and use satellite DLL files then you can adjust the resources on a language by language basis. I sometimes have to make the default width for some windows wider due to their verbose nature.
I have set BS_MULTILINE for the checkbox. The minimum size of the window is fixed but I just want the checkbox to fit in that. I expect it to show at least one word in the same line as other labels and remaining words in second line. So I am checking if the total width of the first row is greater than the width of window then show the string with \r\n in it else show normal string. However, I want to align first line or the first word of the checkbox with the checkbox and remaining words should come below the first word. Currently, the checkbox is in between two lines which looks weird. Is there anyway I can do this?

How to provide a custom column width calculation for CListCtrl?

I'm using a CListCtrl with my own "DrawItem" to draw some custom graphics into the first column, in front of the text. The text is moved ~20 pixels to the right for this. That part works.
If the user double-clicks the column divider in the header, Windows calculates the best column width. But of course Windows doesn't know my custom drawing. So the result is ~20 pixels too small for the first column.
How can I correct that?
Found a workaround:
I can trick MFC into thinking that the list control uses checkboxes:
pMyList->SetExtendedStyle(pMyList->GetExtendedStyle() | LVS_EX_CHECKBOXES);
The user will never ever see the system's checkboxes (because of my custom drawing), but this gives me just the space that I need.

Infragistics XamGrid with different row heights

I have a XamGrid that I've got displaying several dynamically generated columns, and one of those columns can have a listbox in it that sizes itself according to how many items are in it (but has a maximum height constraint). I want each row of the XamGrid to basically only be as large as it needs to be - which they are at first, but after scrolling up and down the contents of the grid a bit I find that rows that had hardly any values in the listbox (so should be fairly small in height) are now the same height as the largest rows I've scrolled into view previously. The result is a lot of wasted space, so I'm wondering if anyone knows of a way to make it so that the rows don't automatically resize themselves to match the largest row that has been loaded. I've tried taking a look at the control template but all I see in it are 3 parts and can't see any way to edit or look inside those parts.
PS I would have posted this on Infragistics own support forums but I can't for the life of me actually find any way to post new topics on there even when signed in with my IG account... great design.
Thanks
Chris
Set the RowHeight property to Dynamic. That should take care of the issue for you.
<ig:XamGrid RowHeight="Dynamic" ... >