Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Advice about the application I will be making? (I'm a beginner)
Forum Updated to NodeBB v4.3 + New Features

Advice about the application I will be making? (I'm a beginner)

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
77 Posts 6 Posters 25.4k 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.
  • D dvlpr.bernard

    @mrjj
    Hello sir,
    Where should I declare a global instance / object in my 3 files.
    mainwindow.h, main.cpp, or mainwindow.cpp?
    Ang how whenever I declare one because whenever I declare one e.g. (MainWindow get_ch_Topic;) it display an error "no previous extern declaration for non-static variable"

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #45

    @dvlpr.bernard said in Advice about the application I will be making? (I'm a beginner):

    Where should I declare a global instance / object in my 3 files.

    Nowhere! Avoid global variables especially if those are derived from QObject!
    Why do you need global variables at all?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    D 1 Reply Last reply
    2
    • jsulmJ jsulm

      @dvlpr.bernard said in Advice about the application I will be making? (I'm a beginner):

      Where should I declare a global instance / object in my 3 files.

      Nowhere! Avoid global variables especially if those are derived from QObject!
      Why do you need global variables at all?

      D Offline
      D Offline
      dvlpr.bernard
      wrote on last edited by
      #46
      This post is deleted!
      D 1 Reply Last reply
      0
      • D dvlpr.bernard

        This post is deleted!

        D Offline
        D Offline
        dvlpr.bernard
        wrote on last edited by
        #47

        @jsulm
        Hello sir. So that I can access the string that I declared in other functions.
        How could I do it the right way?

        0_1556355716202_9f195507-308b-4328-a197-c9245cbddf46-image.png

        0_1556355767974_19f63b1f-35ae-4a4f-ab7a-ec88acd016fb-image.png 0_1556355591803_f0274585-e5f9-4e11-8428-9341f8853a75-image.png

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

          Hi
          Put all the variables in the class!

          class MainWindow : public QMainWindow
          {
          Q_OBJECT

          public:
          explicit MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
          ...
          private:
          Ui::MainWindow *ui;
          QString ch_Dif_Lvl; ///// HERE !
          QString fc_Topic;
          };

          then you can just use them

          void MainWindow::on_pushButton_clicked()
          {
              ch_Dif_Lvl = "BEGINNER";
             fc_Topic = "Topic";
          }
          
          
          D 1 Reply Last reply
          3
          • mrjjM mrjj

            Hi
            Put all the variables in the class!

            class MainWindow : public QMainWindow
            {
            Q_OBJECT

            public:
            explicit MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
            ...
            private:
            Ui::MainWindow *ui;
            QString ch_Dif_Lvl; ///// HERE !
            QString fc_Topic;
            };

            then you can just use them

            void MainWindow::on_pushButton_clicked()
            {
                ch_Dif_Lvl = "BEGINNER";
               fc_Topic = "Topic";
            }
            
            
            D Offline
            D Offline
            dvlpr.bernard
            wrote on last edited by
            #49

            @mrjj
            Thank you. God bless your kindness to help other people.

            mrjjM 1 Reply Last reply
            3
            • D dvlpr.bernard

              @mrjj
              Thank you. God bless your kindness to help other people.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #50

              @dvlpr.bernard
              Thank you :)
              I hope the project is progressing fine.

              D 1 Reply Last reply
              2
              • mrjjM mrjj

                @dvlpr.bernard
                Thank you :)
                I hope the project is progressing fine.

                D Offline
                D Offline
                dvlpr.bernard
                wrote on last edited by dvlpr.bernard
                #51

                @mrjj
                Good day sir.

                I have been trying to get the data from my database it is already connected but whenever the query executes qDebug() returns "QSqlQuery::exec: database not open".

                Also I tried to replace this query
                qry.exec("select Question from mydatabase where QQuantity = 4 and Topic = 'FOR' and Difficulty_Level = 'ADVANCED'")
                with variable and string name
                qry.exec("select Question from mydatabase where QQuantity = '"+quantity+"'and Topic = '"ch_topic"' and Difficulty_Level = '"ch_dif_lvl"'")
                but it didn't work also.

                0_1556616460603_2081b023-6b26-4c79-8e38-65ee64c55338-image.png

                0_1556617041288_2d77d958-9ce6-46e9-add1-b2f1cdb76d81-image.png

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

                  Hi
                  I cant see anything wrong.
                  Show how code is before the if ( mydb.open )

                  D 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    Hi
                    I cant see anything wrong.
                    Show how code is before the if ( mydb.open )

                    D Offline
                    D Offline
                    dvlpr.bernard
                    wrote on last edited by
                    #53

                    The code above is inside of this button function
                    0_1556624873643_3c1572d4-d1c4-4bcb-be5b-7c7a6701dfb4-image.png

                    Here's the declaration of the database
                    0_1556624942089_c2a56415-3c65-44f0-bc2b-e108db11ebed-image.png
                    0_1556624974330_fa46ea4d-303f-475f-b64e-b252d5391cfb-image.png

                    JonBJ 1 Reply Last reply
                    0
                    • D dvlpr.bernard

                      The code above is inside of this button function
                      0_1556624873643_3c1572d4-d1c4-4bcb-be5b-7c7a6701dfb4-image.png

                      Here's the declaration of the database
                      0_1556624942089_c2a56415-3c65-44f0-bc2b-e108db11ebed-image.png
                      0_1556624974330_fa46ea4d-303f-475f-b64e-b252d5391cfb-image.png

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #54

                      @dvlpr.bernard

                      • I notice that your Output window shows two occurrences of your message Connected.... Why is that? Are you connecting more than once?? [In a separate issue, in the long run you absolutely will not want to open() and then close() the database for each query. The whole point is to connect once, keep the connection open while you execute all your queries, and close when you are completely done with the database.]

                      • [...] but it didn't work also.

                      What does that mean? Error message? Behaviour? What?? Please help us to help you by supplying useful information.

                      D 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @dvlpr.bernard

                        • I notice that your Output window shows two occurrences of your message Connected.... Why is that? Are you connecting more than once?? [In a separate issue, in the long run you absolutely will not want to open() and then close() the database for each query. The whole point is to connect once, keep the connection open while you execute all your queries, and close when you are completely done with the database.]

                        • [...] but it didn't work also.

                        What does that mean? Error message? Behaviour? What?? Please help us to help you by supplying useful information.

                        D Offline
                        D Offline
                        dvlpr.bernard
                        wrote on last edited by
                        #55

                        @JonB @mrjj
                        if (query.exec( ))
                        always returns a false value thats why the query never gets executed.
                        0_1556682147823_32f7bcf9-5b1b-405b-b851-94dd8fd9cf15-image.png
                        0_1556682115403_04ffb565-381f-4719-8260-9fac15c4cc3b-image.png

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          dvlpr.bernard
                          wrote on last edited by
                          #56

                          I played around with the code and this error return.

                          0_1556684436755_30f80dab-2418-4b5f-8d3a-5ac9047df238-image.png

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dvlpr.bernard
                            wrote on last edited by
                            #57

                            0_1556685583290_a11aff6b-ff9a-4e92-828b-3de115afe4c9-image.png

                            JonBJ KillerSmathK 2 Replies Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #58

                              hi
                              are you sure the table name is mydatabase ?
                              seems wrong. unless the table is called taht.
                              But it seems you used the name of the database.

                              and it even says unknown table.
                              so use table name instead.

                              1 Reply Last reply
                              3
                              • D dvlpr.bernard

                                0_1556685583290_a11aff6b-ff9a-4e92-828b-3de115afe4c9-image.png

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #59

                                @dvlpr.bernard

                                no such table: mydatabase

                                @mrjj wrote:

                                are you sure the table name is mydatabase ?

                                Are you getting your database vs your tables mixed up in your head? You work with one database at a time. In SQLite that corresponds to the file you pass to setDatabaseName(), and is what gets opened via mydb.open(). A database may contain many tables, each with their own name. It is the table name you mention in SQL select (and most other) statements. You previously created your table(s) via some kind of CREATE TABLE name ... statement, and that name is what you need in your query. It would be "unusual" for you to have named a table mydatabase.

                                You might like to do yourself a favour, to help with all the SQLite queries you're trying to write. Download one of the (free) "workbenches" for SQLite. These are GUI applications which will let you see what is in your database; it will also let you construct and play with queries, till you are ready to copy them over to your Qt code. Google for sqlite workbench, there seem to be several, e.g. https://www.sqlite-workbench.com/, https://sqlitestudio.pl, https://alternativeto.net/software/sqlite-manager/. I suggest you take the time to pick one, in the long run I think the effort will be well worthwhile for you.

                                1 Reply Last reply
                                2
                                • D Offline
                                  D Offline
                                  dvlpr.bernard
                                  wrote on last edited by
                                  #60

                                  Good day.
                                  So here is my database named codeanaldb with a table name "mydatabase". I didn't know why qt couldn't recognize it.

                                  0_1556721420905_3aa67a8d-a82e-4456-9548-1500489ec844-image.png

                                  And here is the same query that I copy paste to qt but suddenly didn't work.

                                  0_1556721664803_4baa7258-307d-4a59-9dbb-68db56cb80f8-image.png

                                  JonBJ 1 Reply Last reply
                                  0
                                  • D dvlpr.bernard

                                    Good day.
                                    So here is my database named codeanaldb with a table name "mydatabase". I didn't know why qt couldn't recognize it.

                                    0_1556721420905_3aa67a8d-a82e-4456-9548-1500489ec844-image.png

                                    And here is the same query that I copy paste to qt but suddenly didn't work.

                                    0_1556721664803_4baa7258-307d-4a59-9dbb-68db56cb80f8-image.png

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #61

                                    @dvlpr.bernard
                                    In a word, Qt will "accept" any SQL query you can make work in a workbench. If it isn't something else is wrong, e.g. you're not connected to the database you think you are....

                                    1 Reply Last reply
                                    1
                                    • D dvlpr.bernard

                                      0_1556685583290_a11aff6b-ff9a-4e92-828b-3de115afe4c9-image.png

                                      KillerSmathK Offline
                                      KillerSmathK Offline
                                      KillerSmath
                                      wrote on last edited by
                                      #62

                                      @dvlpr.bernard said in Advice about the application I will be making? (I'm a beginner):

                                      0_1556685583290_a11aff6b-ff9a-4e92-828b-3de115afe4c9-image.png

                                      Are you sure that your database file is Database codeanaldb ? Because in last image you have used Database codeanal.db (with .)

                                      @Computer Science Student - Brazil
                                      Web Developer and Researcher
                                      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                                      1 Reply Last reply
                                      2
                                      • D Offline
                                        D Offline
                                        dvlpr.bernard
                                        wrote on last edited by dvlpr.bernard
                                        #63

                                        Holly cow, after more than a 100 trial and error, I got it guys. So the problem is I didn't set the path correctly but I'm pretty sure I set it correctly earlier but then other code wasn't correct so I played around and tried change the path, I don't know why it is still working even if the path is wrong. So thank you again guys :).

                                        Correct One: - connected and query is worling
                                        mydb.setDatabaseName("C:\Users\bj\Documents\CODEANALL\Database\codeanaldb1.db");
                                        Wrong One: - connected but the query isn't working dunno why
                                        mydb.setDatabaseName("C:\Users\bj\Documents\CODEANALL\Database codeanaldb1.db");

                                        0_1556722834541_bdf923ef-e973-41f0-ae89-f8e84f224c65-image.png

                                        JonBJ KillerSmathK 2 Replies Last reply
                                        0
                                        • D dvlpr.bernard

                                          Holly cow, after more than a 100 trial and error, I got it guys. So the problem is I didn't set the path correctly but I'm pretty sure I set it correctly earlier but then other code wasn't correct so I played around and tried change the path, I don't know why it is still working even if the path is wrong. So thank you again guys :).

                                          Correct One: - connected and query is worling
                                          mydb.setDatabaseName("C:\Users\bj\Documents\CODEANALL\Database\codeanaldb1.db");
                                          Wrong One: - connected but the query isn't working dunno why
                                          mydb.setDatabaseName("C:\Users\bj\Documents\CODEANALL\Database codeanaldb1.db");

                                          0_1556722834541_bdf923ef-e973-41f0-ae89-f8e84f224c65-image.png

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #64

                                          @dvlpr.bernard

                                          Correct One: - connected and query is worling
                                          mydb.setDatabaseName("C:\Users\bj\Documents\CODEANALL\Database\codeanaldb1.db");
                                          Wrong One: - connected but the query isn't working dunno why
                                          mydb.setDatabaseName("C:\Users\bj\Documents\CODEANALL\Database codeanaldb1.db");

                                          I suspect (never having used SQLite) that your existing database is the first path. When you specify the second path to open(), being non-existent SQLite decides to create that for you, as an empty database, which then does not have the table mentioned in the query/error message. Check your disk (directly in the CODEANALL folder) to see if the second one exists, or exists so long as you are running the query?

                                          If that is the case, there "must" be some way of asking to open only an existent database/file from SQLite/Qt which you could use for safety?

                                          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