What SIGNAL is emitted in Qt when pressing the "X" on the top right corner to terminate the Window? - qt

Which SIGNAL is emitted, when pressing the "X" in the top right corner of a Qt Window application?
I have a second QDialog widget beside my MainWindow in my Qt application. And I would like to intercept the press on the "X" in my second QDialog, how?

The QCloseEvent class contains parameters that describe a close event. Close events are sent to widgets that the user wants to close, usually by choosing "Close" from the window menu, or by clicking the X title bar button. They are also sent when you call QWidget::close() to close a widget programmatically.
To ignore it you can call ignore() method:
void YourDialog::closeEvent(QCloseEvent* iEvent)
{
// ignore close event
iEvent->ignore();
}

There is no signal for that, you need to reimplement QWidget::closeEvent() or install an event filter filtering for QCloseEvents.

Related

In Qt, how to make a slider move event fire when using the arrow keys?

I have a slider:
QSlider *mySlider;
And I have a slot wired to it:
void on_mySlider_sliderMoved(int position);
It's wired in setupUI when this is called:
QMetaObject::connectSlotsByName(MainWindow);
When I slide the slider with the mouse, this slot fires correctly and all is well. However, if I click it once with the mouse to "select" it and then use the arrows on the keyboard, I see the slider moving, but the slot never fires.
What am I doing wrong?
QAbstractSlider::sliderMoved signal is emitted when you drag the slider. From the docs: This signal is emitted when sliderDown is true and the slider moves. This usually happens when the user is dragging the slider.
Use QAbstractSlider::valueChanged signal if you want to know every time the value of the slider is changed.

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 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 - Radiobutton in diff dialog(window) -> taking result to the mainwindow into lineedit

my problem looks like that. I got dialog window and i know how to get result from checked radiobutton but only in this window. How to take result into different window(mainwindow).
button(Pobierz) is on mainwindow and close to this button is lineedit2 where i would like to take result from checked radiobutton, but dont know how. I make lineedit in this new Dialog window and its taking result, but i dont know how to take this result into mainwindow. Hope I explain good enough. Thanks for any help.
void Pobierz::on_pushButton_clicked()
{
if(ui->radioButton1->isChecked())
{
ui->lineEdit->setText("K");
}
if(ui->radioButton2->isChecked())
{
ui->lineEdit->setText("S");
}
if(ui->radioButton3->isChecked())
{
ui->lineEdit->setText("I");
}
}
Greetings,Tom.
up1
i tried couple ways but still cant solve this..
In your class for the dialog, declare a signal that passes a QString. Also override the accept function (if you don't already). In the accept function, emit the signal with the appropriate string according to the radio buttons. (Don't forget to call the parent accept function in your own).
In your class for the main window, when you create the dialog, connect the signal from the dialog to a slot that sets the text in the line edit in the main window. When the dialog is accepted, the signal should fire, running the slot in the main dialog, adding the appropriate text to the line edit.
Create a slot in the main window which would get the radio button status from the dialog window. In this slot set your main window's lineedit based on the result from the dialog window radio button checked status.
While creating the radio button on the dialog window, connect the radio button's clicked signal with the main window's slot defined earlier.

qt creator to give control from one window to another

how to give control from one window to another by a pushbutton click
Both windows must be part of the same application. Connect the QPushButton clicked signal to a slot on the other window in which activateWindow() is called.

Resources