How do classes work in Qt?

  • 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
    } //

    Thank you for helping!

  • Lifetime Qt Champion

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


    What is ui->Material_type ?

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

  • @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.

  • Lifetime Qt Champion

    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)

  • @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: '.'?

  • @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.

  • @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!

  • @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++.

Log in to reply