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. Default agument missing?
Forum Updated to NodeBB v4.3 + New Features

Default agument missing?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 3.2k Views 1 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.
  • JulianJ Offline
    JulianJ Offline
    Julian
    wrote on last edited by
    #1

    alt text

    but if I do:

    explicit BaseDatosPrincipal(QWidget *parent = 0, QSqlDatabase db1=0);
    

    or

    explicit BaseDatosPrincipal(QWidget *parent = 0, QSqlDatabase db1=NULL);
    

    could not convert 0 from int to QSqlDatabase

    So. What value goes there?

    E 1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      So. What value goes there?

      A valid instance of QSqlDatabase

      BaseDatosPrincipal(QWidget *parent = 0, QSqlDatabase db1=QSqlDatabase());
      
      1 Reply Last reply
      1
      • S Offline
        S Offline
        StoatPatronus
        wrote on last edited by
        #3

        Or change the parameter from type from QSqlDatabase to QSqlDatabase*. I don't know much about the specifics of QSqlDatabase but I don't think you can assign an int or NULL value to an object.

        1 Reply Last reply
        0
        • JulianJ Julian

          alt text

          but if I do:

          explicit BaseDatosPrincipal(QWidget *parent = 0, QSqlDatabase db1=0);
          

          or

          explicit BaseDatosPrincipal(QWidget *parent = 0, QSqlDatabase db1=NULL);
          

          could not convert 0 from int to QSqlDatabase

          So. What value goes there?

          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @Julian See http://en.cppreference.com/w/cpp/language/default_arguments:

          int f(int = 1, int); // error, assuming there's no previous declaration of f
          

          because "In a function declaration, after a parameter with a default argument, all subsequent parameters must
          have a default argument supplied in this or a previous declaration;[...]"

          You can write

          explicit BaseDatosPrincipal(QSqlDatabase db1, QWidget *parent = 0);
          

          if you don't want the database to have a default value. If you give QWidget *parent = 0 first, all subsequent parameters must have default arguments.

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            @mpergand This is an invalid QSqlDatabse object like explained in the constructor documentation.

            @Julian Are you using multiple database connections in your software ?

            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
            1
            • M Offline
              M Offline
              mpergand
              wrote on last edited by
              #6

              This is an invalid QSqlDatabse object like explained in the constructor documentation.

              You're right, the object is invalid, but the instance is valid/created, this is what the method parameter need and it's what I want to show.
              But if the default constructor create an invalid object, why is this constructor public ?

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                There's nothing wrong with a constructor creating an invalid object. This just means that you have to do some more work to have a valid object.

                But usually with QSqlDatabase, you don't proceed like that.

                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
                1
                • M Offline
                  M Offline
                  mpergand
                  wrote on last edited by
                  #8

                  It's obvious that Julian wants to declare a pointer instead, but it's not what he did and asked for :)

                  1 Reply Last reply
                  1
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Two possibilities:

                    1. Only one connection used in the app
                    2. Several different connections

                    For 1, there's absolutely no need to copy around QSqlDatabase objects, just enjoy the default connection and its benefits.

                    For 2, pass the name of the connection around and use QSqlDatabase::database to get the corresponding QSqlDatabase object when needed.

                    No need for any QSqlDatabase pointers or member variables.

                    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
                    2
                    • JulianJ Offline
                      JulianJ Offline
                      Julian
                      wrote on last edited by Julian
                      #10

                      @SGaist Are you using multiple database connections in your software ?


                      Yes, a lot.


                      @mpergand It's obvious that Julian wants to declare a pointer instead, but it's not what he did and asked for :)


                      I want to pass a pointer or by copy (like the frist post) to an open database to do not have to open it again, with other connection.

                      Pass a pointer it's more faster than pass the value (by copy)?

                      I what @mpergand said in the first post work. But I want to do in the fastest way.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Then do as I suggest for option 2. Once a connection is opened, it stays opened until either

                        • You close it
                        • There's server/network problem if you are using a remote database. i.e. not SQLite.

                        Having copies of QSqlDatabase objects lying around everywhere in your code doesn't make it any simpler and you are going to see a lot of warning saying the you are destroying a database while connections are still in use.

                        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
                        1

                        • Login

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