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

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.

Related

How to set a Slot receiving every Widget action in Qt

Suppose I've a Qt app based on QWidget, in which I'd like to obtain a "feedback" everytime I press a QPushButton, I rotate a QDial, I press a mouse button. My question is: how can I set a slot that informs me everytime something happened inside my app built for a touchscreen, in which I need to know when user does something on it.
MousePressEvent partially solves my problem; so when user touches the screen, MousePressEvent warns me about it. Problem is related to widgets as QPushButtons or QDials.
At the moment, I've already arranged my widgets with a lot of subwidgets; number of pushbutton is about 300. How can obtain triggering signal when one of them is pressed without re-edit every single button?
You can use an "event filter" that is installed on QApplication instance. This filter will receive all the events in your application, afterwards you can check event type and object type to pick the events you are interested in. See my answer to another question here for details.

Remap context menu call on qwidget

I have my custom widget inherited from QWidget, and I've connected the widget's menu-calling signal to my slot.
connect(m_ontologyView, SIGNAL(customContextMenuRequested(QPoint)), SLOT(showContextMenuSlot(QPoint)));
Now I want user to be able to change button calling the context menu. Normally it's called with right mouse button, but how do I tell the widget to call the menu with a button of my choice?
I'm on Qt 5.4.0
Instead of using QWidget::customContextMenuRequested, you will need to reimplement the widgets mouse event functions, QWidget::mousePressEvent, QWidget::mouseReleaseEvent and QWidget::mouseMoveEvent. Inside of these events, you can then show you menu using QMenu::popup. (The point can be extracted from the mouse events).

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.

Calling a form from a pushbutton using only QtDesigner

I have created a form. I have many 2 push buttons. On clicking on a pushbutton I want to call another custom form. I am using only QtDesigner. I am NOT using QtCreator. Using QtCreator, there are so many examples on how I can do it. But using only QtDesigner 4 there are no examples. I have also tried creating a MainWindow and then having pushbuttons in that. I want to call a new pop up window when I click on a button (which is a custom form). I am using Eclipse CDT as the IDE. I have installed Qt plugin so that I can do both C++ and Qt development. The problem is I cannot use 'Form' to declare my custom form in header file of the mainwindow.
I read in few posts that this is not possible to do using only QtDesigner and also read it can be done using QObject::connect. Please can anyone help me to confirm if we can do it and if yes please can you provide me an example?
Yes, it's definitely possible with C++. You'll need to connect() pushbutton's clicked() signal with a slot in your first form:
connect(pushButton, SIGNAL(clicked()), this, SLOT(show2ndForm()));
The good place to connect is in your first form constructor.
In that slot just show your second form (for example, using QDialog::exec()):
void FirstForm::show2ndForm()
{
static SecondForm *form = 0;
if(!form)
form = new SecondForm(this);
form->exec();
}
You'll probably need to inherit your second form from QDialog to use this method and also create header and source files for your second form.
For modeless form instead of modal, use form->show() instead of exec().
This is not complete possible if you need to customize a slot, but for simple operation where the existing slots are available, you can just use the signal-slot edit as below.
You can select the receiver object and then the correponding slot. I am providing further screenshot to show it is done for customized slots as well.
Right click with your mouse in the middle of the main window and the change signals and slots
Select the change signals and slot option
Use the Add button to add a new slot
Click on the OK button to conirm it once you chose the desired name
In the signal-slot editor double click on the desired object's slot, and it will show the available slots including your new custom slot.
Select your custom slot and you are done from the designer parts. Do not forget to actually implement that in your C++ code.
If you do not need a custom slot, and a built-in will suffice, you can select that off-hand without the previous steps. Those are provided for completeness.

QAction vs QToolButton and when to override the Basic class?

I've recently been studying Qt, and have the following questions:
What is the difference between QAction and QToolButton?
How do I know when to override QPushButton? For example, should I override in order to be informed when the mouse enters a QPushButton's bounds? I don't need to in order to get the signal click().
Question 1:
QActions are used to define tasks performed by an application in a way which can be understood by a number of different user interface objects. Using an example from the Qt docs for context:
A QAction is defined; it is given an icon image, a text description, a keyboard shortcut and a longer tooltip description, as well as being linked to a user defined function.
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
Later in the implementation, the QAction is added both to a textual menu bar...
fileMenu->addAction(newAct);
... and an icon-based tool bar:
fileToolBar->addAction(newAct);
The menu bar uses the text description to create an entry in the menu corresponding to the action. Similarly the toolbar uses the icon set on the QAction to create an icon in the toolbar which corresponds to the action. Selecting the menu item, clicking the toolbar icon or pressing the keyboard shortcut will all result in the same effect: that defined by the linking of QAction::triggered() to newFile().
So to directly answer your question: a QAction is an abstract way of defining the various parameters and behaviour of a particular task performed by the application. A QToolbarButton is a UI object (derived from QWidget) created by a QToolbar in response to QToolbar::addAction()
Question 2:
Yes, QPushButton has a clicked() signal inherited from QAbstractButton, but it does indeed lack a way to inform when the mouse has entered its bounds. You have a couple of options in order achieve this, but first you need to first set the mouseTracking property to be enabled. This will allow you to receive mouse move events on the QPushButton even if no mouse buttons are pressed. With that done you need to explore one of the following options:
As you suggest, you could subclass QPushButton and reimplement mousePressEvent in order to respond to the mouse position.
You could install another widget as an eventFilter on the QPushButton, and watch for events of type (QEvent::MouseMove).

Resources