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.3k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #39

    Hi
    Do NOT make new instances of the main window class like you do
    alt text

    that is just plain wrong.

    You are already in an instance. So no need to ever make new ones.
    you can simply use the ch_topic variable.
    Its already declared in the MainWindow you are using already.

    D 1 Reply Last reply
    1
    • mrjjM mrjj

      Hi
      Do NOT make new instances of the main window class like you do
      alt text

      that is just plain wrong.

      You are already in an instance. So no need to ever make new ones.
      you can simply use the ch_topic variable.
      Its already declared in the MainWindow you are using already.

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

      @mrjj
      Sir, that's what I did before but whenever I do that it says 0_1556214114582_f2ded59c-c3a3-4d57-aae1-428aaa89e276-image.png

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

        well
        just use the variable directly. no need to use function.
        im not sure what get_ch_tipic is as you dont have () so its not function call.

        However, if you do

        QString MyX; ( in .h as you shown)

        then anywhere you can just use it like

        MyX = "";
        or read it.

        alt text
        is just bad syntax.
        as you say get_ch_Topic is a object with the function getTopic and its not true.

        if you gave MainWindow a function called getTopic
        then just call it
        qDebug() << getTopic();

        Its important to understand that all you add to MainWindow class ( in .h)
        can be access from any functio that also a memebr .
        that is
        MainWindow::SomeFunction() {
        any variable from .h you can use here. no special syntax. just use it.
        }

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

          Thank you sir. I just don't know but still it doesn't work it just return "" not "WHILE". :(

          mrjjM 1 Reply Last reply
          0
          • D dvlpr.bernard

            Thank you sir. I just don't know but still it doesn't work it just return "" not "WHILE". :(

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

            @dvlpr.bernard
            hi
            check code where you set it.
            Maybe you create other instance you set it on ?

            D 1 Reply Last reply
            0
            • mrjjM mrjj

              @dvlpr.bernard
              hi
              check code where you set it.
              Maybe you create other instance you set it on ?

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

              @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 1 Reply Last reply
              0
              • 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

                                          • Login

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