user63898
user63898

Reputation: 30895

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:

DataListModel::DataListModel( QObject *parent ) :
             QStandardItemModel( 0, 0, parent ) 

{
// here I like to invoke some QMainWindow method? 
//can I cast somehow the parent ? or use some pointer ?

}

Upvotes: 1

Views: 3019

Answers (1)

Neox
Neox

Reputation: 2004

If the MainWindow is the parent of your DataListModel than you can cast it:

MainWindow *w = qobject_cast<MainWindow*>(parent);
if(w == 0) {
 //error handling here
}

edit

For example this:

#include "mainwindow.h"
#include <QStatusBar>
ScrollBar::ScrollBar(QWidget *parent) :
    QScrollBar(parent)
{
    MainWindow *w = qobject_cast<MainWindow*>(parent);

   if(w != 0) {
       qDebug() << Q_FUNC_INFO;
       QStatusBar *bar = w->statusBar();
       bar->hide();
   }
}

works fine

Upvotes: 6

Related Questions