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 do classes work in Qt?
Forum Updated to NodeBB v4.3 + New Features

How do classes work in Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.9k 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.
  • R Offline
    R Offline
    rocklionmba
    wrote on last edited by
    #1

    Hi! I'm really new to Qt, but i've been picking it up at a decent pace. I've been trying to use a class I added into the header file that was created in the program and make an array, but I couldn't get the class array to work. I tried changing it into a class , and that didn't work either. I did some snooping around in google and the documentation, but was unable to find anything. How does classes, and class arrays, work in Qt?

    Class in the header file:

    class int_paint {
        QString ID; //no usage other than programming
        QString unit;
        QString substrate; // area that is being painted
        QChar UOM; // unit of measure
        int quantity;
        double lf_conversion; //linear feet conversion
        int sq_per_gal; //square feet per gal
        double coats;
        bool f_or_s;
        double flat_per_unit;
        double semi_per_unit;
        int multiplier; //dependant on number of apartments
        double subtotal_flat;
        double subtotal_semi;
        int waste;
        double waste_total_flat; //subtotal * waste (waste must be entered as an int representing a %, conv it to decimal)
        double waste_total_semi;// ^
        double total_flat;//waste_total * multiplier
        double total_semi;// ^
    };
    

    This is the fragment of the code I was working on (I made it a dynamic array in the main)

    void MainWindow::insert_to_database(){
        int_paint.substrate = ui->Substrate->text();
        int_paint.quantity =static_cast<int>(ui->Quantity->text());
        QString UOM_info = ui->UOM->currentText();
        if(UOM_info == "Sq Ft"){
            int_paint.UOM = 'S';
        } else if (UOM_info == "Ln Ft"){
            int_paint.UOM = 'L';
        } else if (UOM_info == "Ea"){
             int_paint.UOM = 'E';
        }
    
        int_paint.coats = ui->Coats->value();
        //int_paint.sq_per_gal= ui->Material_type->; figure out the route to get the sq_per_gal
        calculate();
    } //
    

    Thank you for helping!

    mrjjM 1 Reply Last reply
    0
    • R rocklionmba

      Hi! I'm really new to Qt, but i've been picking it up at a decent pace. I've been trying to use a class I added into the header file that was created in the program and make an array, but I couldn't get the class array to work. I tried changing it into a class , and that didn't work either. I did some snooping around in google and the documentation, but was unable to find anything. How does classes, and class arrays, work in Qt?

      Class in the header file:

      class int_paint {
          QString ID; //no usage other than programming
          QString unit;
          QString substrate; // area that is being painted
          QChar UOM; // unit of measure
          int quantity;
          double lf_conversion; //linear feet conversion
          int sq_per_gal; //square feet per gal
          double coats;
          bool f_or_s;
          double flat_per_unit;
          double semi_per_unit;
          int multiplier; //dependant on number of apartments
          double subtotal_flat;
          double subtotal_semi;
          int waste;
          double waste_total_flat; //subtotal * waste (waste must be entered as an int representing a %, conv it to decimal)
          double waste_total_semi;// ^
          double total_flat;//waste_total * multiplier
          double total_semi;// ^
      };
      

      This is the fragment of the code I was working on (I made it a dynamic array in the main)

      void MainWindow::insert_to_database(){
          int_paint.substrate = ui->Substrate->text();
          int_paint.quantity =static_cast<int>(ui->Quantity->text());
          QString UOM_info = ui->UOM->currentText();
          if(UOM_info == "Sq Ft"){
              int_paint.UOM = 'S';
          } else if (UOM_info == "Ln Ft"){
              int_paint.UOM = 'L';
          } else if (UOM_info == "Ea"){
               int_paint.UOM = 'E';
          }
      
          int_paint.coats = ui->Coats->value();
          //int_paint.sq_per_gal= ui->Material_type->; figure out the route to get the sq_per_gal
          calculate();
      } //
      

      Thank you for helping!

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

      @rocklionmba said in How do classes work in Qt?:

      ui->Material_type->

      What is ui->Material_type ?

      maybe
      int test = ui->Material_type->currentText().toInt();

      1 Reply Last reply
      1
      • R Offline
        R Offline
        rocklionmba
        wrote on last edited by rocklionmba
        #3

        @mrjj Material_type is a QComboBox that holds a list of paint. What I need to do there is, once the user listed the paint, it goes into the class of that paint and gets the square feet per gallon. I haven't done it yet because I wanted to test my program and to get it running before I continued.

        mrjjM 1 Reply Last reply
        0
        • R rocklionmba

          @mrjj Material_type is a QComboBox that holds a list of paint. What I need to do there is, once the user listed the paint, it goes into the class of that paint and gets the square feet per gallon. I haven't done it yet because I wanted to test my program and to get it running before I continued.

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

          @rocklionmba
          Ok. just ask away if needed.

          • How does classes, and class arrays, work in Qt?
            same way as in plain C++;

          However, all Widgets type ( GUI elements) are non copyable. ( QObject parent)

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rocklionmba
            wrote on last edited by rocklionmba
            #5

            @mrjj
            but how come when I do int_paint.substrate = ui->Substrate->text();,
            I get the syntax error:

            C:\Users\\Documents\test\mainwindow.cpp:130: error: C2143: syntax error: missing ';' before '.'
            C:\Users\\Documents\test\mainwindow.cpp:130: error: C2059: syntax error: '.'?

            E 1 Reply Last reply
            0
            • R rocklionmba

              @mrjj
              but how come when I do int_paint.substrate = ui->Substrate->text();,
              I get the syntax error:

              C:\Users\\Documents\test\mainwindow.cpp:130: error: C2143: syntax error: missing ';' before '.'
              C:\Users\\Documents\test\mainwindow.cpp:130: error: C2059: syntax error: '.'?

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

              @rocklionmba You should have an object of type int_paint. Now you're trying to use the class name, it doesn't work. Have you programmed in javascript before? Because it's object-based and when you create an object it's a usable object immediately, while in C++ your write classes and you have to create objects out of them before you can use them.

              R 1 Reply Last reply
              1
              • E Eeli K

                @rocklionmba You should have an object of type int_paint. Now you're trying to use the class name, it doesn't work. Have you programmed in javascript before? Because it's object-based and when you create an object it's a usable object immediately, while in C++ your write classes and you have to create objects out of them before you can use them.

                R Offline
                R Offline
                rocklionmba
                wrote on last edited by
                #7

                @Eeli-K I completely forgot about that. I do C++ and I remember how to do that, but it just slipped over my head until you "pointed" it out. (mark it as my first programming pun.) Thank you so much!

                1 Reply Last reply
                0
                • thamT Offline
                  thamT Offline
                  tham
                  wrote on last edited by
                  #8

                  @Eeli-K tell one of the problem, another problem I found is all of the data member of your class are private, you cannot access them like that.

                  Study a great c++ book can help you a lot, it definitely worth your times if you want to leverage power of Qt, I recommend Programming -- Principles and Practice Using C++ for any level of the programmer, it is a book written by the father of c++.

                  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