QT Unable to use an autosize QLineEdit & Layout - c++

I have created a custom widget extending QLineEdit. I want this is 'autosize'. Ok I get it using
connect(this,SIGNAL(textChanged(QString)), this, SLOT(resizeLineEditToContents()));
I have 2 of this widgets. The idea is that a resize of widget 1 is used by the horizontal layout where I have placed the two widgets to place rightly the widget 2.
But it is impossible, I have played with all the policy possible value, without success.
The layout tries always to adjust the size of my QLineEdit widget .... (It happens also with a standard QLineEdit).
Any help?
Thanks

Actually, it's not necessary to have the signature of signals and slots to be the same, and Qt actually asks in the documentation to not put 'const' and '&' on it because of the 'normalization'. a Signal can have zero to N parameters, a slot can bave zero to Signal-Number-Of-Parameters, so that's why the connect works.
but back to your problem, you need to use setFixedWidth, so it will know to not resize back to the old width just as soon as you stretch it, then set Layout to prefered, so it will respect your widget's hints.

Related

Qt correct way to set z or z-index (Programmatically)

I want to ensure that objects are layer correctly, I've read online that the way to achieve this is by setting the z-index style.
However so far I haven't been able to do this, I want to set the style programmatically.
I've tried:
pobjWidget->setStyleSheet("z-index: 5");
and
pobjWidget->setStyleSheet("z: 5");
Both of these result in "Unknown property z-index" or "Unknown property z" being displayed in the console.
pobjWidget is an a pointer to a QWidget.
The reason I need this is that I'm rendering widgets with a transparent background onto live video widgets and I need to ensure that the widgets on top do not flicker, this is an attempt to illuminate the flicker.
I assume you have a handle to every widget you want to order. If raise and lower are too crude for you, you can use
void QWidget::stackUnder(QWidget *w)
Quoting from the docs: "Places the widget under w in the parent widget's stack.
To make this work, the widget itself and w must be siblings."
The important bit is that they must be siblings, i.e. need to have the same QObject as parent.

Qt - Make QMainWindow to non resizible in Qt Designer

Is it there a way to set a QMainWindow to be non-resizible in Qt Designer? I am seeing lots of coding examples but I want to do as much UI customization in Qt Designer as I can. So far I can only get this by setting the minimum size and maximum size to be equal, but still there is the resize arrow in the corner of the window and a "maximize" button on the top of the window.
When you select the QMainWindow, the properties of the object does contain a field for sizePolicy, both horizontal and vertical, as mentioned by the answer of #jester and you can set those to fixed.
I have found that doesn't always work and was never sure why (perhaps to due to layouts), but as you've found out, if you set the minimumSize and maximumSize fields to the same value, it does what you want.
As for the resize arrow and maximize button, I have never been able to do that from Qt Creator (designer), so I would say it's not possible. However, just one line of code is all you should need in the constructor of your class: -
setWindowFlags(Qt::Window | Qt::CustomizeWindowHint);
By default, the window flags include Qt::WindowMaximizeButtonHint. By setting the above flags, you're stating that you want to customise the window to include the specified elements. This will also remove the minimise button, so if you want that too, you should add Qt::WindowMinimizeButtonHint
I am not using Qt Designer; I am just writing a subclass of QMainWindow made from scratch. The solution I found for having a non-resizable window is to call setFixedSize on the QMainWindow after you have set up all your widgets and layouts. If you have set things up well, then you don't have to pick a size manually; you can just get the size from sizeHint. The line of code I used inside my subclass of QMainWindow is:
setFixedSize(sizeHint());
I tested this in Qt 5.5 on Windows 8.1 and everything looks fine: the maximize button gets disabled but the other buttons are still there, and the cursor does not indicate the possibility of resizing when the user moves it to the border.
use the setFixedSize property for the QMainWindow. In designer, if I remember correctly, you can set the horizontal and vertical sizePolicy to Fixed.
Sadly there is no option to do that; in VS you may find an option in the editor, to remove the mouse trigger that resize the window; but for some reason, QT5 does not have one.
I tried to use sizePolicy and set it as fixed for mainWindow; but this does nothing for both the horizontal and vertical policy.
To solve the problem, I did set my mainwindow minimum and maximum size as the same values; and when you run the application, the mouse cursor won't be enabled for resizing.
It is an ugly way to do something so simple, but this is the only way I found in QT designer, without use code.
I solved the problem of Qt5 which is displaying mouse arrow resize window even with window having fixed size, but in Python, but you can make modifications for C++.
MainWindow.setWindowFlags(QtCore.Qt.MSWindowsFixedSizeDialogHint)
It's an old post but I want to help if someone need it.
I found a way (not so beautiful) but it works directly from QTDesigner.
You can lock the resize by writing the values of height and width also inside "MinimumSize" and "MaximumSize" property.
Oviously also set "Fixed" on vertical and horizontal as told by other users.
This will remove the button to enlarge the window.

QWidget Transformation

I am writing an application using Qt and would like to have a "Metro Style" interface. Everything is finished except I don't know how to make the widget appear and disappear. For instance, in WPF you can animate (UIElement.RenderTransform).(TranslateTransform.Y) so that Y becomes zero or negative. How can I move the QWidget in a QGridLayout so that it can hide?
Example:
After doing some research I figured out a way to do this. Instead of letting Qt do the layout I simply handled it myself via move and set width/height functions. Overriding resizeEvent made it so I could update the values in case the window was resized. Additionally I used setMask to ensure that the widget did not leak over to unwanted locations in the UI.

Qt - dynamically add QLineEdit in a panel

this is a question for programming with Qt/C++. I have a combo box with two items. If current index for selection is 0, then no QLineEdit should be displayed in layout below the combo box. If it is 1, a QLineEdit should appear. It should disappear again if index is 0 again.
Notably, other elements in the layout should not be affected by the change. Values already entered by user in other QineEdit should remain in place.
Is it possible to dynamically modify widget? How did you procede?
Kind regards.
All QWidget objects have a function called hide().
You can attach a signal to the currentIndexChanged signal of the combo box, and in that function you implement whatever logic you have in mind and invoke the method hide of your QLineEdit.
The only problem with this approach is that a Qt Widget, when hidden, doesn't occupy any space on the screen, and this can lead to layout changes (depending on how you've programmed your layout, some other widgets can move a bit, for example). To prevent that you can make another Widget appear where the QLineEdit were (perhaps invoking the show() function, and placing the 'placeholder' on the same container that the LineEdit was), only to occupy its space and keep it there, or you can use a QStackedWidget add the two Widgets there and change its index.
I would recommend that you read the following example, it has some useful insight on dynamically changing things: Qt Extension Example.
Also, when in doubt, take a look in the other examples, they are really well documented and cover a lot of important topics on Qt.
Good luck with your code :)

Unable to get height/width of a widget - gtkmm

I'm writing an application using gtkmm.
I wrote a simple widget class, that I want to display in the application's main window only in some cases. Otherwise, I would like a Label "disabled" to be visible.
To achieve that I packed both the widget and the label into one VBox, and I show() and hide() them in order to swap them.
However, the custom widget is far larger than the label, so I need to resize the label a bit.
I know I can use label.set_size_request(x,y), and it works, when I type the dimensions manually. But I am aware, that the widget may differ in size a bit, depending on the Gtk theme one uses, etc.
I'd love to set label exactly the same size *as the widget has. However, using widget.get_height()* does not work - it always returns 1.
Maybe a clue will be that I do this from within the main window's constructor.
I haven't done much in gtkmm, more in pygtk. But I think to get meaningful values from get_width/get_height, a widget must be realized, which is an X-window speak for visible. And I guess your widgets are not yet visible in window constructor.
I'd propose you to use a gtk.Notebook instead, with its tabs hidden. Then gtk.Notebook will control size of both your label and your custom widget.