[SOLVED] Function works, can't add it to slot
-
wrote on 20 Apr 2015, 08:16 last edited by Adept
I have a function that gets the current QStackedWidget as a QMdiArea and adds a subwindow to it (this function works), BUT when I add it to a
connect()
/SLOT()
it does nothing.Here is the function to get the current QStackedWidget as a QMdiArea and add a subwindow to it:
void MainWindow::addWindow()
{QMdiArea *a = (QMdiArea *) layout->currentWidget(); a->addSubWindow(new QMdiSubWindow);
}
So again this function works if I call it like this:
addWindow()
BUT if I put it in a connect it does NOT work:
connect(addWindow, SIGNAL(triggered()), this, SLOT(addwindow()));
-
wrote on 20 Apr 2015, 08:34 last edited by
Have you declared the macro Q_OBJECT in your MainWindow implementation ? Have you declared the addWidow method of your MainWindow in the slots section ?
class MainWindow : public QMainWindow { Q_OBJECT ... public slots: void addWindow(); }
By the way, when you are instantiating yourself the QMdiSubwindow, "you must set the Qt::WA_DeleteOnClose widget attribute if you want the window to be deleted when closed in the MDI area. If not, the window will be hidden and the MDI area will not activate the next subwindow."
-
wrote on 20 Apr 2015, 09:22 last edited by Adept
Hey thanks @macgab for the suggestions, unfortunately both of them had been done. Here is the full code to the app.
main.cpp
http://pastebin.com/QeZ90LCSmainwindow.cpp
http://pastebin.com/LBzMYKRVmainwindow.h
http://pastebin.com/e9HEEY2s -
wrote on 20 Apr 2015, 09:52 last edited by mistralegna
It seems your QMdiSubWindow keeps hidden, try this in the addwindow method:
void MainWindow::addwindow() { ... subWindow->show(); }
-
It seems your QMdiSubWindow keeps hidden, try this in the addwindow method:
void MainWindow::addwindow() { ... subWindow->show(); }
wrote on 20 Apr 2015, 10:12 last edited by@macgab ahhh, thank you so much!!! I appreciate you taking your time to help :)
-
wrote on 20 Apr 2015, 10:18 last edited by
You're welcome ! Please don't forget to mark the topic as solved ;-)
-
It seems your QMdiSubWindow keeps hidden, try this in the addwindow method:
void MainWindow::addwindow() { ... subWindow->show(); }
wrote on 20 Apr 2015, 10:22 last edited by@macgab one quick question though:
when I do this:
QMdiArea *mdi = (MdiArea *) getSelectedTabAsWidget();
how do Iassert()
that thegetSelectedTabAsWidget()
is a QMdiArea? If I run it and it's not a QMdiArea, it will crash, so I need toassert()
it. -
wrote on 20 Apr 2015, 10:37 last edited by
You can first retrieve the getSelectedTabAsWidget() as a QWidget, check that it's not null, and if qobject_cast<QMdiArea*>() doesn't return NULL.
Snippet 1:
QWidget* selectedWidget = getSelectedTabAsWidget(); if ( selectedWidget == NULL ) { return; } QMdiArea* mdiArea = qobject_cast<QMdiArea*>(selectedWidget); if ( mdiArea == NULL ) { return; } // Here, we know that mdiArea is indeed a QMdiArea.
Snippet 2:
QWidget* selectedWidget = getSelectedTabAsWidget(); if ( selectedWidget == NULL ) { return; } if ( ! selectedWidget->inherits(QMdiArea::staticMetaObject.classname()) return; // Here, we know that mdiArea is indeed a QMdiArea. QMdiArea* mdiArea = qobject_cast<QMdiArea*>(selectedWidget);
5/8