# Using MainWindow in an Extension

**URL:** <https://discuss.avogadro.cc/t/using-mainwindow-in-an-extension/2103>\
**Category:** Development\
**Created:** [July 8, 2011, 3:07pm UTC](https://discuss.avogadro.cc/t/using-mainwindow-in-an-extension/2103 "2011-07-08T15:07:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodolf\_Meyer](https://discuss.avogadro.cc/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Rodolf\_Meyer](https://discuss.avogadro.cc/u/Rodolf_Meyer)\
**Post date:** [July 8, 2011, 3:07pm UTC](https://discuss.avogadro.cc/t/using-mainwindow-in-an-extension/2103/1 "2011-07-08T15:07:40Z")

</div>

Hi all,

I’m currently working on an Avogadro extension. It should be able to control  
most of the Avogadro functionnalities (camera, open file, copy/paste, select  
a tool, add hydrogen, …) with an external controller.  
I found the Navigate class to use the camera, and it is ok. But for some  
other points, I need help:  
I think I can call every other functionnalities by using the MainWindow  
instance, am I correct?  
I tried to get the MainWindow instance by QObject\_casting the parent() of  
the extension in a loop in the constructor. The QObject\_cast  
works, but the casting in MainWindow fails.

if (!m\_mainwindow) {  
QObject _mainwindow = this;  
while (mainwindow && !qobject\_cast\<MainWindow_\>(mainwindow)) {  
mainwindow = qobject\_cast\<QWidget\*\>(mainwindow-\>parent());  
}  
m\_mainwindow = qobject\_cast\<MainWindow\*\>(mainwindow);  
}

The error is :  
In function `Avogadro::MainWindow* qobject_cast<Avogadro::MainWindow*>(QObject*)': /usr/include/qt4/QtCore/qobject.h:366: undefined reference to`Avogadro::MainWindow::staticMetaObject’

MainWindow is a QObject, I don’t understand why the QObject\_cast doesn’t  
work.  
Do you know why? (or maybe you know a better idea to resolve my problem)

Regards,

RM

---

<div class="post-metadata">

**Author:** ![allison.vacanti](https://discuss.avogadro.cc/user_avatar/discuss.avogadro.cc/allison.vacanti/32/112_2.png) [@allison.vacanti](https://discuss.avogadro.cc/u/allison.vacanti)\
**Post date:** [July 8, 2011, 3:20pm UTC](https://discuss.avogadro.cc/t/using-mainwindow-in-an-extension/2103/2 "2011-07-08T15:20:52Z")

</div>

On Fri, Jul 8, 2011 at 11:07 AM, Rodolf Meyer [rodolf.meyer@gmail.com](mailto:rodolf.meyer@gmail.com) wrote:

> Hi all,  
> I’m currently working on an Avogadro extension. It should be able to control  
> most of the Avogadro functionnalities (camera, open file, copy/paste, select  
> a tool, add hydrogen, …) with an external controller.  
> I found the Navigate class to use the camera, and it is ok. But for some  
> other points, I need help:  
> I think I can call every other functionnalities by using the MainWindow  
> instance, am I correct?  
> I tried to get the MainWindow instance by QObject\_casting the parent() of  
> the extension in a loop in the constructor. The QObject\_cast  
> works, but the casting in MainWindow fails.  
> if (!m\_mainwindow) {  
> QObject _mainwindow = this;  
> while (mainwindow && !qobject\_cast\<MainWindow_\>(mainwindow)) {  
> mainwindow = qobject\_cast\<QWidget\*\>(mainwindow-\>parent());  
> }  
> m\_mainwindow = qobject\_cast\<MainWindow\*\>(mainwindow);  
> }

I’ve used this method to get the QMainWindow object in the  
crystallography extension, but only to get access to the dockwidget  
methods, which are implemented in QMainWindow (not  
Avogadro::MainWindow). I do not think what you are trying to do is  
possible (at least not in a reliable way) since the  
Avogadro::MainWindow class is not available to extensions, as it is  
not in libavogadro and its headers are not installed.

However, some of the functionality you want is possible in other ways:

Files can be opened by using Avogadro::MoleculeFile to create a  
Molecule instance, which can be loaded in the main window by emitting  
the moleculeChanged(Molecule\*) signal from your extensions.

Copy/paste can be implemented in an extension by modifying/reading the  
molecule that is passed to the extension in the  
Avogadro::Extension::setMolecule slot and using QClipboard and  
associated classes.

AFAIK, tool selection is not possible from within an extension, though  
it certainly would be useful!

“Add hydrogen” and most other molecule modification can be performed  
by handling the setMolecule virtual function to store the pointer to  
the current molecule, and then modifying it. Calling  
Molecule::update() when you’re done modifying the molecule will force  
the GL widgets that are drawing the molecule to refresh.

Hope this helps,

Dave

> The error is :  
> In function `Avogadro::MainWindow* qobject_cast<Avogadro::MainWindow*>(QObject*)': /usr/include/qt4/QtCore/qobject.h:366: undefined reference to `Avogadro::MainWindow::staticMetaObject’  
> MainWindow is a QObject, I don’t understand why the QObject\_cast doesn’t  
> work.  
> Do you know why? (or maybe you know a better idea to resolve my problem)  
> Regards,  
> RM
> 
> * * *
> 
> All of the data generated in your IT infrastructure is seriously valuable.  
> Why? It contains a definitive record of application performance, security  
> threats, fraudulent activity, and more. Splunk takes this data and makes  
> sense of it. IT sense. And common sense.  
> [http://p.sf.net/sfu/splunk-d2d-c2](http://p.sf.net/sfu/splunk-d2d-c2)
> 
> * * *
> 
> Avogadro-devel mailing list  
> [Avogadro-devel@lists.sourceforge.net](mailto:Avogadro-devel@lists.sourceforge.net)  
> [avogadro-devel List Signup and Options](https://lists.sourceforge.net/lists/listinfo/avogadro-devel)

---

<div class="post-metadata">

**Author:** ![ghutchis](https://discuss.avogadro.cc/user_avatar/discuss.avogadro.cc/ghutchis/32/456_2.png) [@ghutchis](https://discuss.avogadro.cc/u/ghutchis)\
**Post date:** [July 8, 2011, 3:40pm UTC](https://discuss.avogadro.cc/t/using-mainwindow-in-an-extension/2103/3 "2011-07-08T15:40:50Z")

</div>

On Jul 8, 2011, at 11:20 AM, David Lonie wrote:

> However, some of the functionality you want is possible in other ways:

Right, and we’d be open to adding more signals/slots if needed to access the main window.

> AFAIK, tool selection is not possible from within an extension, though  
> it certainly would be useful!

Sure it is. For example:  
widget-\>toolGroup()-\>setActiveTool(“Navigate”);

The tool selection is handled by the widget, not the MainWindow class.

Cheers,  
-Geoff

---

<div class="post-metadata">

**Author:** ![Rodolf\_Meyer](https://discuss.avogadro.cc/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Rodolf\_Meyer](https://discuss.avogadro.cc/u/Rodolf_Meyer)\
**Post date:** [August 25, 2011, 9:32am UTC](https://discuss.avogadro.cc/t/using-mainwindow-in-an-extension/2103/4 "2011-08-25T09:32:16Z")

</div>

Hi,

Thanks for your answers, it helps me a lot.

2011/7/8 Geoffrey Hutchison [geoff.hutchison@gmail.com](mailto:geoff.hutchison@gmail.com)

> Right, and we’d be open to adding more signals/slots if needed to access the main window.

I think this option could be very useful. The extension I’m working on  
should allow to use Avogadro with an external controller in order to  
improve the usability. By accessing the main windows, my goal was to  
make any option of the menu bar, the tool bar and maybe the dock  
widgets available to the extension. And this way give the ability to  
bind the controller buttons to some functionalities of Avogadro.

I’ll give you more information later depending on the project progress.

Regards,

RM

---

<div class="post-metadata">

**Author:** ![ghutchis](https://discuss.avogadro.cc/user_avatar/discuss.avogadro.cc/ghutchis/32/456_2.png) [@ghutchis](https://discuss.avogadro.cc/u/ghutchis)\
**Post date:** [August 29, 2011, 3:44pm UTC](https://discuss.avogadro.cc/t/using-mainwindow-in-an-extension/2103/5 "2011-08-29T15:44:14Z")

</div>

> I think this option could be very useful. The extension I’m working on  
> should allow to use Avogadro with an external controller in order to  
> improve the usability. By accessing the main windows, my goal was to  
> make any option of the menu bar, the tool bar and maybe the dock  
> widgets available to the extension.

Well, this is off the top of my head – I haven’t checked code yet. I’d be curious if QtScript or the Qt accessibility framework make this easier.

1. Tool bar is easy – you can already switch tools with code.

foreach(Tool \*tool, GLWidget::current-\>toolGroup()-\>tools())  
{  
qDebug() \<\< tool-\>identifier();  
}

// or something like this  
GLWidget::current()-\>toolGroup()-\>setActiveTool(“Manipulate”);

1. Accessing the MainWindow and Dock widgets can occur through qApp():

All widgets can be accessed through the Qt meta-object system:

[http://doc.qt.nokia.com/4.7/qmetaobject.html#methodCount](http://doc.qt.nokia.com/4.7/qmetaobject.html#methodCount)  
[http://doc.qt.nokia.com/4.7/qmetaobject.html#invokeMethod](http://doc.qt.nokia.com/4.7/qmetaobject.html#invokeMethod)

If you can tell us a little more about what you’re planning in the extension, it might be easier for us to give specifics.

Hope that helps,  
-Geoff
