A way to connect checkbox state to widget visibility in Qt Designer - qt

Does anyone know if there is a way to connect a state of a checkbox in Qt to an object visibility in Qt Designer? I somewhat new to Qt and and prefer to work with Qt Designer rather than messing with the UI code manually. Qt version I'm using is 4.8.6
Any help is appreciated.

In code (old Qt 4 way):
connect(checkbox, SIGNAL(toggled(bool)), widget, SLOT(setVisible(bool)));
or (recommended Qt 5 way):
connect(checkbox, QCheckBox::toggled, widget, YourWidgetType::setVisible);
In Designer: open the Signals And Slots Editor, connect signal and slot above.
Noticed that signals and slots from Designer are reset sometimes, don't know why. Maybe it is more reliable to connect them in code on widgets setup.

Yes, you can connect the checkbox's toggled(bool) signal to the widget's setVisible(bool) or setHidden(bool) slot in Qt Designer - either in "Edit Signals/Slots" mode using drag-and-drop, or in the "Signal/Slot Editor" dock.
If using the drag-and-drop interface to add the connection, you'll need to turn on "Show signals and slots inherited from QWidget", else setVisible(bool) and setHidden(bool) will not be available for the target widget.

Related

Why std::thread can't open a QT Dialog?

I want to open a simple QT dialog inside a std::thread, but it crashes after the dialog open success.
std::thread([&](){
DialogWarning* dlg=new DialogWarning();
dlg->setModal(true);
dlg->exec();
delete dlg;
}).detach();
What is the problem with this code?
UI components can only be opened from the main thread (aka the GUI thread).
From Threading basics | Qt 5.13:
The Qt GUI must run in this [the main] thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads.
The main reason that you cannot open the dialog in a different thread has been correctly mentioned by #MarkoPacak.
However, what you can do to fix the problem is to emit a signal in your thread to be captured by a slot in the main thread. Then, in your slot, you can show the dialog.

How can i change QStackWidget page index with button in Qt Designer?

I am currently working in Qt Designer and i am trying to make button change the index of QStackWidget with click signal.
I entered signal editor mode and connected Button to QStackWidget, This is what i got:
As you see in the picture, setCurrentIndex(int) is grayed out, If i choose any signal from QPushButton, there is nothing associated with changing page.
Sorted Question:
How can i change the page in QStackedWidget using a button? (In Qt Designer).
That is not possible directly with Qt Designer, because QButton click signal doesn't send any index or argument, and setCurrentIndex(int) need an argument to chose change the index.
You have to do it using signal/slot in C++ code, or use an other widget like QSpinBox which emit a signal with an integer argument.

Signals and Slots Editing Buttons Are Disabled (Qt Designer 4.8)

I want to add custom slots to my design via Qt Designer (4.8), but editing buttons on "Configure Connection" window are disabled. How can I solve it?
You can only add custom signals/slots to subclasses of Qt classes.
As a demonstration of this, make a connection between pushButton and the top-level widget. When the connection dialog is shown, you will see that the right-hand edit button is now enabled. This is because the top-level widget will usually be a subclass of QWidget, QMainWindow, or QDialog that is defined by the application.
To add custom signals/slots to child widgets, you would need to use widget promotion, so that you can specify a subclass that will be supplied by your application. See this answer for how to promote widgets in PyQt.

Qt creator - make a dynamic interface

I am working on a small UI with QT Creator and its drag-n'-drop tool (QT Designer). Everything was fine till I realized I had to make some widgets dynamic. For example, I have a checkbox and I want some button to appear only if this checkbox is checked by the user or I would like to change the appearance of another widget when the user clicks on some button.
Is it possible to do it only with QT Designer?
Yes, but it's very limited.
Qt designer have signal&slot editor. Yes, you can connect signal clicked(bool) to setVisible(bool) slot on button and make button visible only when checkbox is checked (see screenshot).
But when You need more complex dynamic interface (e.g. creating buttons), designed will not help You.

Signal and slot connection in .ui

I have started to play a little with Qt 4. And then I have come across a problem with the Qt Designer.
In the Signal/Slots editor I can only setup the connections that are listed there, and not all the slots are listed.
If I try to add it manualy in the .ui file, the connection would not work.
If I add it in the ui_*.h file it works fine, but then the connection is deleted when I change the design.
Does anyone have any good tips to how I can get around this bug? Or to ask another way:
How can I make the Qt Designer list all the available slots?
By default not all signals/slots are shown. You could try checking the "show signals and slots inheritied from ...." checkbox in the lower left hand corder of the "Configure Connection" dialog that comes up when you try to create a signal.
Beyond that, you can either do what Marcin said and use auto-connections, or manually write connect statements in the constructor of the object that uses the ui.
You might try to use uic's autoconnecting feature.
However you won't be able to see all available slots but if you use the same name in both Designer and code - they should automatically be connected.

Resources