Question

I'm working on an application that uses a MDI. I have a bunch of toolbox and menu bar defined in a main window containing the QMDIArea.

All subwindows are of the same class. In order to connect the buttons to the active sub window, I did the following think:

void MainWindow::zoomOut() {
  QMdiSubWindow* sub_window = central_document_interface->currentSubWindow();

  if (sub_window) {
    PlanWindow* plan_window = (PlanWindow*)(sub_window->widget());
    plan_window->zoomOut();
  }
}

I think it would be smarter to reconnect the signals using the subWindowActivated signal. But the problem I have is that I intend to have several types of sub window (different classes). All signal aren't used by all these classes.

I can't manage to find a clean way to differentiate them and connect or not the signals according to their class. How would you do this ?

No correct solution

OTHER TIPS

I created this code for my project:

void MainWindow::slot_menuEditZoomOut() {
     WindowAreaManagerInterface::instance()->
        LambaOnCurrentCustomWindow<CAbstractZoomAction>([](CAbstractZoomAction *zoom){ zoom->zoomOut(); });
}

Where the WindowAreaManagerInterface is

class WindowAreaManagerInterface : public QMdiArea {
Q_OBJECT
public:
static WindowAreaManagerInterface *instance();

template<class T>
T *currentCustomWindow() {
    QWidget *widget = 0;
    QMdiSubWindow *subWindow = currentSubWindow();
    if (subWindow) widget = subWindow->widget();

    return dynamic_cast<T *> (widget);
}

template<class T, typename Func>
void LambaOnCurrentCustomWindow(Func F) {
    T *window = currentCustomWindow<T>();
    if (window)
        F(window);
}
//other methods follow..
}

Hope this help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top