Frage

I have an example application using Qt and a multiple documents interface. In the main window there are actions added to the menu bar, for example an action to save the current document, as soon as the user activates this action. There is a document class which has to be told to save the document. Unfortunately the current document may change (its a MULTIPLE document interface), therefore it is not easy to use the signal-slot-mechanism. I don't know to which document instance I should connect the signal to.

The example application here connects the signal to a dummy slot in the main window and this slot redirects the request to the respective method in the document class, by querying the current document and calling the corresponding method of that instance. This causes a lot of boilerplate code.

Is there a more elegant way to solve this issue?

Here some lines of code to make the issue clearer:

Mainwindow:

connect( action, SIGNAL( activated() ), this, SLOT( onSaveFile() ));

void MainWindow::onSaveFile()
{
getCurrentDocument()->save();
}

Document:

void Document::save()
{
...
}

Thank you very much!

Bye, Benjamin

War es hilfreich?

Lösung

If you are using the QMdiArea, you could listen to the subWindowsActivated signal and reconnect your signals and slots there.

connect(QMdiArea, SIGNAL(subWindowsActivated(QMdiSubWindow*), this, SLOT(subWindowChanged(QMdiSubWindow*))

void MainWindow::subWindowChanged(QMdiSubWindow* newSubWindow) {
  // Update the signals to point to the new subwindow
}

Andere Tipps

I suppose your method "getCurrentDocument()" is elegant enougha and presented in http://www.informit.com/store/c-plus-plus-gui-programming-with-qt4-9780132354165

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top