Important: Please read the Qt Code of Conduct -

How to address the parent

  • I don't understand what I'm doing wrong. My intention is to start the application and show the Main window. Then, to check for the existence of the SQL drivers, the existence a database file and create it if it's not there, then check for the required tables and create then if not there, and finally, if there's no data in the tables, allow the user to add sample data. My thought is that these checks and operations should be in a separate class. With that thought in mind, my mainwindow looks like this:

    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        QTimer::singleShot(0, this, SLOT(checkDataBase()));

    The timer allows the opening window to be shown before any potential error messages are shown. I also want those error messages to be contained withing the boundaries of this opening window. So far, so good. I created a file called dbcontroller.cpp and the associated .h file.
    In the dbcontroller file is this function:

    bool checkDrivers()
        // If we don't have the correct drivers, we can't continue.
        bool rv;
        if ( ( rv = ( QSqlDatabase::drivers().contains( "QSQLITE" ) ) ) ) {
            QMessageBox msgBox;
            msgBox.setIcon( QMessageBox::Critical );
            msgBox.setAutoFillBackground( true );
            msgBox.setGeometry( 175, 200, 450, 400 );
            msgBox.setText( "Unable to load Database! This program needs the SQLITE driver." );
            msgBox.setInformativeText( "Press the OK button to Exit." );
            msgBox.setDefaultButton( QMessageBox::Close );
        return( rv );

    (And yes I know I have the test backwards - for testing the message) This function works except for the commented line setting the parent of the messagebox that is commented out. When it runs, it pops up over in the corner of the screen, not within the confines of the program window. I simply cannot figure out how to tell messagebox who the parent is! I've gone through the docs and videos and have only gotten more confused, so I'm. once again, asking for help.

  • Is your dbController.cpp is a type of QWidget? The parent has to be a class of QWidget type (i.e either QWidget or any class inherited from QWidget)

    And by the way, QMessageBox can be constructed with the parent directly using QMessageBox(QWidget* parent);

  • No, the controller is plain C++ code. If I move the messagebox code to the mainwindow.cpp file, I can use "this" in the setparent() line. I'm thinking if I can determine what the pointer is to to mainwindow, I can use that. In the mainwindow file, I've created a QWindowList using QGuiApplication::topLevelWindows() but can't seem to get any info out of it. I've tried using .contains() with everything I can think of. I've looked at parentWidget, but of course, it shows 0.

  • Perhaps I'm trying the wrong method. Would my question be better put as: How can I force a messagebox to stay within the confines of the program?

  • Lifetime Qt Champion

    If you just need a parent, maybe something like

    QMainWindow* getMainWindow()
        foreach(QWidget *widget, qApp->topLevelWidgets())
            if (QMainWindow *mainWindow = qobject_cast<QMainWindow *>(widget))
                return mainWindow;
        return NULL;

  • That looks like it should work, however:

    I get ```
    /home/bart/develop/C++/Ideas/DockedComponent/mainwindow.cpp:91: error: no matching function for call to ‘qobject_cast(QWidget*&)’
    if (QMainWindow *mainWindow = qobject_cast(widget))

    I tried, one at a time,adding the includes I found in a sample page of Qt

    #include <QtWidgets/QMenu>
    #include <QtWidgets/QVBoxLayout>
    #include <QtWidgets/QFrame>
    #include <QtWidgets/QLabel>
    #include <QtWidgets/QFileDialog>

    #include <QtNfc/qndefnfcurirecord.h>
    #include <QtNfc/qndefnfctextrecord.h>
    #include <QtNfc/qndefrecord.h>
    #include <QtNfc/qndefmessage.h>
    #include <QtNfc/qnearfieldmanager.h>
    #include <QtNfc/qnearfieldtarget.h>

    #include <QSharedPointer>

    to no avail.  Where the heck is it?

  • Lifetime Qt Champion


    qobject_cast is a template function.

    QMainWindow *mainWindow = qobject_cast<QMainWindow *>(widget);

Log in to reply