Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Qt best way to invoke methods of QMainWindow from other widget
umen242 last edited by
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 ?
lgeyer last edited by
QMainWindow mainWindow = qobject_cast<QMainWindow>(parent());
if(mainWindow != 0)
goetz last edited by
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.