How to resize a CTabCtrl to fit its contents - c++

I have a CTabCtrl in my program and 3 "child" *CFormView*s that I show/hide whenever the user changes the active tab.
Instead of using a fixed size, I'd like to resize the enclosing CTabCtrl to match the size of the largest CFormView.

You can use this:
GetDlgItem(IDC_YOURCONTROL)->MoveWindow(xPosInScreen, yPosInScreen, Width, Height);
Hope it helps.

Related

How to draw Qt widgets in a QTabWidget in non-selected tabs?

My QTabWidget has 2 tabs on launch.
Tab 1 is the one that's opened upon launch.
Tab 2 contains some widgets of which I take the QSize to later draw similar elements in newly opened tabs.
The problem is that currently the size of the elements is 640x480 (apparently the default value), if tab 2 was not shown yet. Once tab 2 is selected, the elements are "drawn" and get their sizes depending on resolutio, window size etc.
How can I force the drawing of the elements in the background without opening tab 2, so that I can obtain the sizes the elements would have if the tab was opened? repaint and update do not do that.
My current workaround is to launch with tab 2 open and switch to tab 1 in main.cpp between myWindow.show() and myApp.exec() but that seems a bit dirty.
For performance reasons, a QWidget's size() method is not guaranteed to return its expected value until after the QWidget has actually been laid out and shown.
If you need to know in advance what a QWidget's size would be, you can call its sizeHint() method instead, and that will force the calculation to be performed right away, so that the correct size can be returned even if it hasn't already been computed.
according to Document resizeEvent was never called when the widget is not visible and also paintEvent. this is a normal routine for decrease processing effort and reduce the rendering process. if you want to know when a tab1 resized, reimplement(override) resizeEvent and say to tab2 must be resized.

How to I resize an item in a QListWidget upon the resize of the window?

I am using a QStyledItemDelegate to change the appearance of the QListWidgetItems in my QItemWidget. I want to change the height of the items depending on the size of the text. I am using sizeHint() which works fine on the initial creation. The problem is when the window resizes, the text reformats and leaves empty spaces. How do I resize the items to remove the empty space?
setResizeMode to QListView::Adjust instead of QListView::Fixed.

GTK3 No resize limits

Currently if I make a window in GTK3
For example 300x300
And I put a button at the bottom, right hand corner, I can not shrink my window
Size because this button is preventing me is there a function in gtk3 that can allow me to ignore all widgets, and resize to anything even 0x0
And this is the user doing this with the window resize, drag and click
And is there a way where I can set this resize limit myself, and not have this dependent on whats in my window
If you initially use set_size_request() to set the window to 300x300, then it won't shrink below that. To allow users to shrink below an initial value, use set_default_size(). I seem to have read that the minimum size of a widget is 1x1, which seems logical, as, at 0x0 you wouldn't be able to resize it anymore. If you want less than 1x1, you can use hide() and just hide the contents.
But if you have any widgets inside the window, then the minimum size is determined by the widgets! (Called the 'natural size')
To allow a window smaller that than the one determined by the widgets, you can maybe use a Gtk.ScrolledWindow.
Also, recall that the outer border is drawn by the window manager, NOT by Gtk. However, you can disable the outer border by using set_decorated(). Not that this may not work - depending if the window manager respects this (not Gtk's fault).

Initial size for QMdiSubWindow

Is there a way to define an initial size for a child window widget (inherited from QMdiSubWindow)? I don't want to necessary limit the minimum size or prevent it from being resized, but just to show the window at the first time with a given size.
I've tried to reimplement sizeHint and to define different size policies, but even with those changes the autoAjust call seem to make the window very small (size 200 x 200) when it is first displayed. The window contains a widget with this hierarchy: QVBoxLayout -> QScrollArea -> QLabel. The QLabel is used to show an image with the size 512 x 512.
EDIT: Correct the class to QMdiSubWindow.
you can use resize(int w, int h).
It will not set the maximum and minimum size.
It will just change the initial size of the child window.
Well actually, what it really does is: it changes the "current" size of the child window. But the first current size is the "initial" size. So basically its the same.
To my knowledge, there's no such thing in Qt as a QSubWindow.
However, a call to setGeometry on a QWidget will set its size.
After much searching for an answer, and experimenting, I found this to work for me.
child->parentWidget()->resize(900, 700);
child->parentWidget()->updateGeometry();
child->show();
// child->showMaximized();
You can use showMaximized() in place of show() and the (900, 700) will still be used if the window is later changed to normal.

In Qt, how do I make a dialog un-resizeable, yet automatically adjusting its size to the contents?

I have an instance of QDialog, populated by widgets using code generated by uic. The dialog contains a few labels laid out vertically, and I am popping the dialog from time to time to show some text in these labels. The text can be multi-line and its length is not pre-determined. I set the vertical size policy to fixed, so the user can't drag it (doesn't make sense), but I also want the dialog to change its size before being shown to accomodate for the current size of the labels.
To this end, I was calling QWidget::adjustSize() on the QDialog before displaying it, but it doesn't work as expected. When the dialog is shown, it seems to retain the (wrong) size from the previous displaying, but when I click the mouse in the (disabled) vertical resize mode, the dialog suddenly "snaps" to the (correct) adjusted size.
Is there any way to make my dialog appear correctly?
EDIT: I tied rubenvb's advice, and ended up with this:
QSizePolicy free(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
QSizePolicy fixed(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
dialog->setSizePolicy(free);
dialog->adjustSize();
dialog->setSizePolicy(fixed);
dialog->show();
Unfortunately, that didn't seem to change anything.
This isn't the answer you're hoping for, and it may not apply to what you're trying to do, however, the only way that I was able to adjust the dimensions of a QWidget at run-time was by handling the object's resizeEvent(..) method. This allowed me to calc the size of items based upon the font being used, number of lines, available space, etc., and then adjust their size accordingly before passing the 'event' on to the base resizeEvent(..) method.
My approach used a single QWidget container within a window, below a header, above a footer status area, and to the right of a column of menu buttons. The widget container, inside the resizeEvent() call, would look at the objects it was going to display, calculate the font heights being used, and then resize some items according to their dimensions (because of how the style sheet selected fonts and colors, etc) and then adjust the sub-widget dimensions before allowing the container widget to get the resizeEvent() message.
So I wasn't so interested in setting a window size, but I think the container QWidget might work the same way? I was more interested in setting the dimensions to some asthetically pleasing size, depending upon the dimensions of the display.
Hope you find that helpful.
Do everything in the right order:
Dialog is not shown. Dialog is resizeable.
Calculate new size, set new size.
Set dialog to not-resizeable.
Show Dialog.
Hide dialog, go to step one.