I have a problem with a QWidget
derivate (NewPayment
). It's a simple window, with some controls and a QDialogButtonBox
. It has 2 slots:
void NewPayment::on_buttonBox_accepted() {
//(some action going in here)
this->close();
}
void NewPayment::on_buttonBox_rejected() {
this->close();
}
When I click either OK or Cancel - the slot is triggered as expected. The problem is, that the window does not close. All the contents dissappears, and an empty window is left (window title is left).
The widget exists as a MDISubwindow, and is created like so:
void HurBudClientGUI::addNewPayment(int direction, int contractorid) {
foreach(QMdiSubWindow* it, this->ui.mainArea->subWindowList()) {
if ( NewPayment* np = qobject_cast<NewPayment*>( it->widget() ) ) {
if (np->getContractorID() == contractorid) {
this->ui.mainArea->setActiveSubWindow(it);
return;
}
}
}
NewPayment* np = new NewPayment(direction, contractorid, this);
np->setAttribute(Qt::WA_DeleteOnClose);
this->ui.mainArea->addSubWindow(np);
np->show();
}
The interesting part is, that when I either:
QMdiArea::closeActiveSubWindow()
from the main windowQMdiArea::closeAllSubWindows()
from the main windowthe window is closed properly. I have overwritten a QWidget::closeEvent(QCloseEvent * event)
for my class:
void NewPayment::closeEvent(QCloseEvent * event) {
qDebug() << "[" << __FUNCTION__ << "]:" << "event: " << event << "; sender:" << sender();
}
and it shows preety much the same event every time - no matter how I try to close it:
[ NewPayment::closeEvent ]: event: QCloseEvent(Close, 0x40bd64, type = 19) ; sender: QDialogButtonBox(0x4dfa7a8, name = "buttonBox") // I hit cancel
[ NewPayment::closeEvent ]: event: QCloseEvent(Close, 0x40b634, type = 19) ; sender: QObject(0x0) // I hit the 'X' in the window corner
[ NewPayment::closeEvent ]: event: QCloseEvent(Close, 0x40b468, type = 19) ; sender: QObject(0x0) // I hit "close active sub window" from parent window
[ NewPayment::closeEvent ]: event: QCloseEvent(Close, 0x40b454, type = 19) ; sender: QObject(0x0) // I hit "close all sub windows" from parent window
The best part is, that when I hit "cancel" (the windows is cleared, but stays open), and then click "X" or whatever - the window is closed, but the control does not pass through my NewPayment::closeEvent
(i have a brakepoint there - and it does not fire) .
It works preety much the same in other windows. What is strange, that I'm preety sure that it worked previously (+- a week ago) for other windows (they closed after clicking OK ant performing all necessary operations) . I guess I will end up analyzyig diff from SVN, but maybe someone had similar issue? I have had very little sleep lately, so maybe I missed something trivial?
I will appreciate any help.
I followed @ddriver 's suggestion and ended up with
void NewPayment::on_buttonBox_rejected() {
if (QMdiSubWindow* psw = qobject_cast<QMdiSubWindow*>(this->parent()) ) {
psw->close();
} else {
this->close();
}
}
Now it works as it was supposed to.