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.
Related
I'm rebuilding a project I had already done in Qt, but using gktmm just for fun.
The projects contains several tables, which get more and more columns and rows as days pass. Eventually it starts being necessary to scroll horizontally, and I'd like my gtk::TreeView to have headers on its rows, so they are visible all the time while scrolling horizontally and vertically
This is the new project (left) vs the old project (right)
As you can see the old one has headers on the rows (i forgot to put them on columns too, i know) and that's what I'd like to achieve. (As well as coloring)
If i simply set up the first column and write the names there, it will go out of sight when scrolling horizontally
I don't think that this is actually implemented in gtk::TreeView. To get that functionality you might have to override the scroll method(s) and hide/show columns instead of scrolling the view.
Gnumeric uses the GocCanvas widget from the goffice library to achieve the desired effect, but I haven't seen C++ bindings.
Well, so far the closest working solution I've found is creating a side treeview besides the original one and populate the headers into it.
This will require the following
to connect both scrolled windows so they vertically scroll at the same time
connect the rows so whenever I expand a row on either treeview, the corresponding row will expand on the other treeview
connect the rows whenever a row is selected on either treeview, the corresponding row will be selected on the other treeview
I really really hope this is somehow possible, but I've just figured this out and I'm yet to try the connections
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.
I am using Qt 4.2. I have a window with two separate tables using the QTableView and QAbstractTableModel. The tables seem to have extra space below the rows ending, and to the right of the columns ending. My solution up to this point with other windows has been to calculate the size of the table (based on cell size, plus a little extra for the border) and set the fixed width and height. This solution is a pain for tables that have different size columns. I am looking for a solution where my QTableView class does not have to set its own fixed size, but rather the table just ends at the last row and column. I do not want to use setStretchLastColumn(). Any suggestions to get these tables to behave properly without having to calculate and set a fixed size would be very helpful. Thanks.
I need to create a ListControl in MFC, each row having different number of columns.
How can I do this?
That's not possible. The workaround is trivial, just don't put any text in the sub-item.
You may refer to my guru's(Chris Maunder-Codeproject founder) article:
http://www.codeproject.com/KB/miscctrl/gridctrl.aspx
You could use custom-draw List Control. Then you can specify maximum columns for the control, but draw only specified number of columns in each row. You can fill unwanted columns with a background color. Here's a sample of how you could create custom-draw list control.
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.