Remove Row Padding In QTableWidget? - c++

After filling a QTableWidget table I call:
table->resizeRowsToContents();
This makes the rows narrower but there is still a lot of padding between each row, as you can see here:
Looking at the documentation I found I could set the hight of each row with:
table->verticalHeader()->setDefaultSectionSize(16);
This removes the padding and gives me the tight spacing I want:
However, I'm concerned about using an absolute size in pixels. If the the system font is set to something large the text might not fit in the row.
Is there a way to remove the padding between rows while ensuring the text will always fit?

Use a custom delegate with the QAbstractItemDelegate::sizeHint() method implemented in a way that it will return a correct size according to the font used.
To compute a correct height, you can use a QFontMetrics provided with the option parameter of the sizeHint() method.
int height = option.fontMetrics.height();
or more precisely
int height = option.fontMetrics.boundingRect(index.data().toString()).height();
EDIT: How to use a delegate:
CMyDelegate* delegate = new CMyDelegate(tableView);
tableView->setItemDelegate(delegate);

Related

Is there anyway I could set width and layout width to be the same in Directwrite TextLayout?

Whenever I render a text using text layout object, it's apparent that actual width of text is not same as the width of layout depending on enable option in text wrapping.
I would like to know if there is anyway I could do to set width and layout width to be the same?
So When I pass in width and height in a creation of text layout object, I want it to render the text in the exact dimension I provided.
Initial layout dimensions are used to control word wrapping or trimming, if enabled. So it depends on what you want to achieve, normally you set desired layout box, and text does not have to fit or fully fill to any degree. When rendering you might be interested in effective rectangle that your text fits in fully, you need to call GetMetrics for that, and use returned DWRITE_TEXT_METRICS struct fields. Metrics data will contain actual rectangle sizes for your text, regardless of what you specified on layout creation.

How to make fixed width of column with Helvetica font?

Usually, I use iomanip and setw to make a column with a fixed width for output.
But it seems to be that it doesn't work with Helvetica font.
What to do with it here?
Helvetica is a proportional font which means that the letters have different widths.
BTW, for most GUIs, you'll need something more specialized than std::cout.
In order to make a fixed width column you will need to pad to get to the next column. This means adding up the widths of the characters and the spacing. Subtract this from the column width and this becomes your padding.
Many GUIs have functions for determining the pixel widths of a string of text.
An easier route, IMHO, is to use a grid type widget in your GUI. These have column capabilities that can be adjusted. Some have functions that will resize a column based on its contents. You only need to put the text into that column.
Another GUI technique is to use vertical "boxes" (sizers) for each column. Place your text into the box. Let the box figure out its alignment and padding.

DirectWrite align text center

I am trying to align center the text I have to draw. I use ID2D1RenderTarget::DrawTextLayout method. The problemn is that if I set the text horizontal alignment to DWRITE_TEXT_ALIGNMENT_LEADING (the default value) the text is drawn proberly, but if I change this value to DWRITE_TEXT_ALIGNMENT_CENTER the text is shifted right.
The example string is
Internal Amazing
Scupper
Following are the outcomes (the first is alignment leading):
My comment as an answer (yeah, guessed right :)):
Just a quick guess: Did you check that the maxwidth your layout box
isn't too broad, so the center would end there at the right?
The IDWriteTextLayout used by ID2D1RenderTarget::DrawTextLayout method defines a maximum width of the layout box, which determines where the text is centered. It can be manipulated by the methods of the interface (GetMaxWidth and SetMaxWidth).
When you create the text layout, you cannot change the width and height to something greater afterwards. You should use max screen coords when creating the layout then change the max width and height to the desired size. You should use render_tgt->DrawText(...) method for this example or release and recreate the layout interface every time the text, font name, and a lot of other various things like typography are changed. I created an array of layout and typography events that can be reapplied to the layout interface every time it is recreated. You do not need to recreate the layout for font size, since you can resize the text or individual characters with the layout interface
// Set the layout to maximum screen size
FLOAT maxX = (FLOAT)GetSystemMetrics(SM_CXFULLSCREEN); // * XD2D::pix_to_dips.x;
FLOAT maxY = (FLOAT)GetSystemMetrics(SM_CYFULLSCREEN); // * XD2D::pix_to_dips.y;
XS_DWRITE_DEV_FACTORY->CreateTextLayout
(
dstring,
dlength,
(*pp_txt_format),
maxX,
maxY,
pp_txt_layout
);
// Resize to the requested size or minimum allowed size
(*pp_txt_layout)->SetMaxWidth(max(req_xsize, (*pp_txt_layout)->GetFontSize()));
(*pp_txt_layout)->SetMaxHeight(max(req_ysize, (*pp_txt_layout)->GetFontSize()));

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.

How to set minimum width for the first column of the QFormLayout?

How to set minimum width for the first column of the QFormLayout? I can do it for QGridLayout by using QGridLayout::setColumnMinimumWidth(int, int), but cannot find a method to do that for QFormLayout.
It's pretty simple to do. Just set a minimum width on any of the widgets that you've inserted into the first column.
How to set minimum width for the first column of the QFormLayout?
You cannot (off-hand). It was not designed for that as this sounds more like a QGridLayout feature. You could either refactor your design to use QGridLayout, or you could potentially set the size for the rows as you wish, but you would execute that on the row content rather than the layout itself.