January 25, 2012

umen242 umen242
Ant Farmer
328 posts

Qt best way to invoke methods of QMainWindow from other widget

 

I have main window from type QMainWindow, then I set some widget and give this widget the QMainWindow as parent in its constructor , the QMainWindow passed as Object. now I need from this widget invoke method from the QMainWindow. what will be the best way to do it ?

this is how the widget looks like:

  1. DataListModel::DataListModel( QObject *parent ) :
  2.              QStandardItemModel( 0, 0, parent )
  3.  
  4. {
  5. // here I like to invoke some QMainWindow method?
  6. //can I cast somehow the parent ? or use some pointer ?
  7.  
  8. }

2 replies

January 25, 2012

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

  1. QMainWindow *mainWindow = qobject_cast<QMainWindow*>(parent());
  2. if(mainWindow != 0)
  3. {
  4.     ...
  5. }

January 25, 2012

Volker Volker
Robot Herder
5428 posts

Or if you always pass a QMainWindow as parent, change the constructor to take only QMainWindow pointers as parent and save the parent in a class attribute.

 
  ‹‹ QDialog out of a console application      What need to install user on platform for using application which has developed in Qt? ››

You must log in to post a reply. Not a member yet? Register here!