Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to address the parent
Forum Updated to NodeBB v4.3 + New Features

How to address the parent

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • bart.hollisB Offline
    bart.hollisB Offline
    bart.hollis
    wrote on last edited by
    #1

    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) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        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.setParent(this);
            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 );
            msgBox.exec();
        }
        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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sivan
      wrote on last edited by Sivan
      #2

      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);

      1 Reply Last reply
      0
      • bart.hollisB Offline
        bart.hollisB Offline
        bart.hollis
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • bart.hollisB Offline
          bart.hollisB Offline
          bart.hollis
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi
            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;
            }
            
            1 Reply Last reply
            4
            • bart.hollisB Offline
              bart.hollisB Offline
              bart.hollis
              wrote on last edited by
              #6

              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?
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                qobject_cast is a template function.

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved