How to define an OnClick event handler for a button from within Qt Creator? - qt

In visual studio, when designing a windows form, I can easily add an OnClick event handler for a button by double clicking on it. Is it possible to do the same in QtCreator? How should I handle the Click event of a button? Is manually writing the required code from scratch the only option?

In the designer,
add Push Button to the form
right click the Push Button
select "Go to slot..."
select "clicked()" signal
done
The terms are different from .NET, so in this case we are talking about signals and slots, and the signal emitted when QPushButton is clicked is called clicked() instead of OnClick.
Reading the Qt's documentation about signals and slots is recommended.

In header file:
private slots:
void exit_app();
in xyz.cpp:
connect(ui.button_name, SIGNAL(clicked()), this, SLOT(exit_app()));
define the exit_app() func that SLOT calls.
void QtTest2::exit_app()
{
QApplication::exit();
}

Related

How to handle QTableView long press?

I am developing embedded application for device with touch screen and need to handle separately single clicks and long presses on QTableView items. Single clicks should open editing dialog, long presses should only select item. The problem is that there is only 'pressed' signal in Qt, and I don't know what is the right way to handle long presses. Could anybody suggest how to do it?
Instead of using QTableView directly, subclass it and then implement the virtual functions: -
void mousePressEvent(QMouseEvent *)
void mouseReleaseEvent(QMouseEvent *)
You can then decide how you want to handle the events, creating a timer to see if a certain time has passed before the release event.
Install an event filter on the view's viewport() widget and process its mouse events. Use view->indexAt() to find out which item was clicked. See also Event Filters.

Detecting initial/entry click on QT QPlainTextEdit widget

I'd like to detect a click into a QPlainTextEdit widget. The ultimate goal is to launch a virtual keyboard widget. BTW, the QPlainTextEdit widget is described in a QT ui file and loaded by QUiLoader. I've tried installing an event handler for the QPlainTextEdit widget in order to catch QEvent::FocusIn events. Unfortunately this event is fired repeatedly, and not just once.
Note: I'm using QT 4.8.
Update
I've found a tutorial/example that shows how to launch a software input panel for a text input widget. Unfortunately, the input panel closes immediately after opening. Basically, the event QEvent::CloseSoftwareInputPanel is fired soon after QEvent::RequestSoftwareInputPanel.
If you want to detect just mouse press event, install event filter on the widget's viewport() widget. See QAbstractScrollArea docs for more details.
Usually FocusIn event must not happen repeatedly. May be you have called installEventFilter multiple times for one widget?
May be you still can use FocusIn event and ignore repeating events somehow.
The solution is to trigger a QEvent::RequestSoftwareInputPanel event (see also)
void InputPanel::saveFocusWidget(QWidget *oldFocus , QWidget *newFocus )
{
// check if newFocus is not null or top-level widget or previous widget
// check if newFocus is QPlainTextEdit
QEvent event( QEvent::RequestSoftwareInputPanel );
QApplication::sendEvent( newFocus , &event );
}

qt create dialog

I've made my first qt window. Now I'd like to make my first dialog, using qt. I have just finished creating the dialog, which is basically made of a QDialogButtonBox, and now I'd like to connect it to the window. I have two beginner's questions:
How can I retrieve how the dialog was closed (ok pressed or cancel pressed) from the window.cpp, which creates a new dialog, and then calls dialog->show() ?
Where and how to destroy the dialog pointer ?
If you use dialog->show() then I assume it's non-modal dialog.
If you have created QDialogButtonBox and connected its signals with accept() and reject() slots of your dialog as documentation shows, then your dialog will emit finished(int) and additionally accepted() or rejected() signals by which you can determine how it was closed.
If you need more customized behavior, then you can reimplement closeEvent(QCloseEvent *event) or create your own singnals.
If you need to delete your dialog you can use setAttribute(Qt::WA_DeleteOnClose, true);, which will delete instance on close.
you can use one flag, and signal and slot.
when put OK flag=1 , and when put cancel then flag=-1; and then use signal.
in in the window.h write code how to handle that flags with 1 simple slot.
for destroying the pointer you can use signal and slot in your Dialog and tell when user push
Ok, or Cancel , or exit (up- right (red cross)) go to slot in call the Destructer of dialog
and also you that you better set parent of dialog to window.
First Question:
When you want to show the dialog,just construct it,using myDialog *d = new myDialog(this)(the this pointer will make sure that you havn't to delete the pointer you created 'cause Qt will handle this if you specified the dialog's parent). And use d->exec() if you need a modal dialog, or d->show() to make it non-modal;
Second Question:
Once your specified the dialog's parent object, all u need is just use it and leave alone the memory managent,Qt will do this for you. Also you can use d->setAttribute(Qt::WA_DeleteOnClose,true) to make it destroy itself when it is closed.
Remember to link the QDialogButtonBox to your dialog's actions.

QT creating push buttons that add text to a text edit box

New to QT just playing around with it to see if its something I will enjoy using and if so would like to go on and learn the program in depth.
Struggling a bit with the button concept. I have created a button and a textedit area. I want to add a string of text into the textedit window when the button is pressed.
I can't seem to find anything on google or the QT wiki to achieve this. Can someone point me in the direction so I can at least get started and have a play with this great tool.
In Qt signals and slots are being used to communicate between the objects. This should provide you with the necessary information to get you started.
A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal. Qt's widgets have many pre-defined slots, but it is common practice to subclass widgets and add your own slots so that you can handle the signals that you are interested in.
So, in your particular case you need to connect the QPushButton clicked() signal with your custom slot that does what is needed (add the text to the textarea):
QPushButton * btn = new QPushButton("Button", this);
connect(btn, SIGNAL(clicked()), this, SLOT(onBtnClicked()));
And we need to declare our slot in the header:
private slots:
void onBtnClicked();
And define it:
void MySpecialWidget::onClick()
{
// Do what is to be done
}
If you have done everything correctly it should work... Otherwise have a look at the console to see if there are any messages looking like:
Object::connect: No such slot MySpecialWidget::onClick() in ...
or
Object::connect: No such signal ....
They should give you a hint about what is going on.
Finally I recommend to have a look at the broad set of Qt examples.

How to generate a window (widget) on button press in qt

I have designed a GUI through Qt creator on Linux. This design consists of some fields, text edit and some push buttons.
When I press on the push button I want to display another window. Is there any GUI option for this or any hard code?
You need signals and slots.
You have to connect the clicked signal to a custom slot, created by you, of your main widget.
Corrected Code, based in the comments of Patrice Bernassola and Job.
In the class definition (.h file) add the lines:
Q_OBJECT
private slots:
void exampleButtonClicked();
private:
QDialog *exampleDialog;
The macro Q_OBJECT is needed when you define signals or slots in your classes.
The variable exampleDialog should be declared in the definition file to have access to it in the slot.
And you have to initialize it, this is commonly done in the constructor
ExampleClass::ExampleClass()
{
//Setup you UI
dialog = new QDialog;
}
In the class implementation (.cpp file) add the code that does what you want, in this case create a new window.
void ExampleClass::exampleButtonClicked()
{
exampleDialog->show();
}
And also you have to connect the signal to the slot with the line:
connect(exampleButton, SIGNAL(clicked()), this, SLOT(exampleButtonClicked()));
Your question is somehwat basic, so I suggest to read a basic tutorial, in this way you can make progress faster, avoiding waiting for answers.
Some links to tutorials that were useful to me:
http://zetcode.com/tutorials/qt4tutorial/
http://doc.qt.io/archives/qt-4.7/tutorials-addressbook.html
on click event of button you create another widget and show.
another option is Stacked widget, http://doc.trolltech.com/4.6/qstackedwidget.html

Resources