BB 10 C++ Button on click - c++

In QML you can do the on click event. I am writing my qml in C++, however there is no onclick method.
How do you get the on click to work.
Button *btnSave = new Button();
btnSave->setText("Save");
contentContainer->add(btnSave);
Does anyone have a simple example that they could provide to get me started?
Thanks in advance.

To do this in C++, you have to connect a signal (in this case, is the Button's clicked() to a slot of your class). You better take a look here.
Supposing you defined a slot called onSaveButtonClicked() in your header, that will be called when your button is clicked:
public slots:
void onSaveButtonClicked();
in your application class, after create your button, you'd have to do:
connect(btnSave, SIGNAL(clicked()), this, SLOT(onSaveButtonClicked()));
When your button is clicked, it will emit the signal that will call the slot function.

Related

How to avoid Qt to execute QAbstractButton::nextCheckState() when checkable button is clicked?

When you click a checkable button. The checkstate always inverts:
on to off
off to on
This is because when a checkable button is clicked Qt calls the nextcheckstate function.
QAbstractButton::nextCheckState()
Is there a way to avoid that? (I would like to decide check state by myself)
In my mind I would have to do the following steps to avoid that:
Create a MyPushButton Class which inherits QPushButton
Declare in mypushbutton.h an override function which overwrites the function when a checkable button is clicked
Somehow suppress calling QAbstractButton::nextCheckState() in that overriding function
Does somebody know which function to override?
Try:
Checkablebutton->blockSignals(true); // Maybe nextCheckState() is not call from now on until BlockSingls(false)
//some code
Checkablebutton->blockSignals(false);

How to set focus after execution QAction

Main widget steals focus after execution QAction.
I need the focus to be set to popup widget.
QAction *action = new QAction(tr("show popup"), this);
connect(action, &QAction::triggered, this, &MyWidget::showPopup);
addAction(action);
void MyWidget::showPopup()
{
QMessageBox* popup = new QMessageBox(this);
popup->setModal(true);
popup->show();
popup->setFocus();
}
MyWidget inherits from QWidget.
Because you just created popup, it's not 'there' yet in the GUI. Even the show() doesn't instantly show it. After you leave the scope of MyWidget::showPopup(), the GUI event loop will continue looping and be able to process your new popup. Thus the setFocus() call comes too early.
But there is help underway:
QWidget::setFocus() is a slot, so you can invoke it.
If you use a timer (QTimer::singleShot(0, popup, SLOT(setFocus()));), it should work.
Maybe you'll need to use 10ms instead of 0ms.

Using connect method to connect to a slot

I'm building an application that requires a Qdialog, and a few buttons.
I am attempting to use the Command behavioural design pattern in my implementation. My project so far has 4 classes. (Please excuse the rough UML)
Command
+execute()
zoomInAndOut : Command
+execute()
MenuItem
-QPushButton
-command
+clicked()
Dialog
So within the dialog class, I create a menuItem (which has a QPushButton and Command member variable), and what I need to happen is that when the menuItems button has been clicked, it calls the menuItems "clicked" method (which in turn calls the commands execute method). I know that the "connect" function must be used, however after many, many attempts, I cannot get it to work correctly.
Within Dialog the code roughly looks like this
zoomInAndOut zoomCommand;
menuItem *zoom = new menuItem(new QPushButton("Zoom", this), QRect(QPoint(300, 0), QSize(100, 50)), &zoomCommand);
connect(zoom->getButton(), SIGNAL(clicked()), SLOT(zoom->clicked()));
As mentioned before the connect method is completely wrong, but you can see what I am attempting to achieve. How can I make this work?
Thank you in advance for any help.
Change code
connect(zoom->getButton(), SIGNAL(clicked()), SLOT(zoom->clicked()));
to
connect(zoom->getButton(), SIGNAL(clicked()), zoom, SLOT(clicked()));
Make sure your menuItem class contains the Q_OBJECT macro the line after the opening {. And make sure the clicked() method is in the slots section of the class body.

How thread-safe and latency-safe is QApplication::mouseButtons?

When you get get a mouse signal from a model into your slot, the argument passed is a QModelIndex.
QModelIndex does not tell you what button is pressed. So, we can resort to QApplication::mouseButtons. But QApplication::mouseButtons is the current button press, not when model experienced the click.
My thought experiment is saying that, after the right-button is pressed, the underlying view sends the signal to my widget, but just before my widget's slot received the signal, a spurious left-click occurred. So calling QApplication::mouseButtons on receipt of QModelIndex would wrongly associate the row being click with the left mouse button rather than the right button. How possible is this scenario?
When you look at Qt and even QML, it takes a lot of code acrobatics to achieve proper mouse button info on receipt of a QModelIndex. Is it a policy that nokia is striving to promote mouse button agnosticism?
I don't think this is a very possible scenario but it may happen.
A "simple" way to be sure about which button was clicked is to subclass QTableView (or the view you are using and reimplement the mouseReleaseEvent.
void mouseReleaseEvent(QMouseEvent * event)
{
// store the button that was clicked
mButton = event->button();
// Now call the parent's event
QTableView::mouseReleaseEvent(event);
}
By default the mouseReleaseEvent emits the clicked signal if an item of the view is pressed
If a user presses the mouse inside your widget and then drags the
mouse to another location before releasing the mouse button, your
widget receives the release event. The function will emit the
clicked() signal if an item was being pressed.
The trick is to catch the clicked signal in your derived class and emit a new signal which except the model index will contain the button as well.
// Define your new signal in the header
signals:
void clicked(QModelIndex, Qt::MouseButton);
// and a slot that will emit it
private slots:
void clickedSlot(QModelIndex);
// In the constructor of your derived class connect the default clicked with a slot
connect(this, SIGNAL(clicked(QModelIndex), this, SLOT(clickedSlot(QModelIndex)));
// Now the slot just emits the new clicked signal with the button that was pressed
void clickedSlot(QModelIndex i)
{
emit clicked(i, mButton);
}
You can do something similar with the mousePressEvent if you need the pressed signal as well.

Callback for button in Qt Designer?

I just started using QtCreator tonight, and it seems it puts all of the interface stuff inside of the ui file. I followed a tutorial to create a resource for my icons, then I added them to a menu bar at the top.
I need to make a connection when one of them is clicked though, and cannot figure out how to make a callback for it.
Am I going to have to completely create them through code or is there some way to add a callback for them (rather than just making them interact with other objects).
Menu bar items are action objects. To do something when they are clicked, you need to catch the triggered() signal from the action. Read more about signals and slots here.
To do this, you need to declare a new slot in your MainWindow class. Qt also supports doing this automatically, without the need to connect anything, but I prefer doing it myself. If you're not interested, just skip this part.
First, we declare a new slot in your window class:
private slots:
void clickMenuButton();
Then, in your constructor, you need to connect the triggered signal to your new slot:
connect(ui.actionObject, SIGNAL(triggered()), this, SLOT(clickMenuButton()));
The first argument is the object that holds the signal we'll listen to (your menu button). The second is the name of the signal. The third is the object that holds the receiving slot (in this case, our window). The fourth is the slot.
And just like that, clickMenuButton() will be called whenever the action is clicked.
As I said before, Qt can also automatically connect signals to slots. The disadvantage here seems to be that you can't change the slot's name, but you don't need to connect it either.
Qt Creator supports creation of slots for widgets: in the case of your menu action, you should go to the form designer, and you should see a list of actions in your form (if you don't, find the Action Editor). Right click the action you want, and push Go to slot.... There, double click triggered().
Qt Creator will then open the new slot in your code editor, and you can do whatever you want to here!
To do that you'll need to add a QAction, add it to the menu, associate an icon with it and then create a callback for it. I'm using the VS Integration so I don't know the details of how to do it in Creator but it should be possible without creating stuff in code.
There should be somewhere an actions editor. from there you add an action, then right-click it or something to add an icon to it, then drag it do the menu and then possibly double click it to create a slot for it. This is how it works in the VS Integration atleast.