Connecting to Database using .ui files.



  • I am having trouble connecting or configuring a single tableView between two or more connections. I am sure this can done but my main problem is configuring each ui->pushButton to connect to model tableView.
    Project description:
    A mainwindow consisting of three pushButtons and one TableView.
    Which is connected to a single database with three tables.
    I would like to be able to press each pushButton and have the tableView reflect the matching table.
    pushButton_1 to view table1
    pushButton_2 to view table2
    pushButton_3 to view table3
    I could hard code the connections but I would rather like to use the Designer ui's.
    And I am also trying to make this happen without using any other dialogs besides mainwindow.

    If this is possible, please point me to the correct documentation.

    Thank you,
    Jay From Seattle



  • I don't think you can do that. The ui files are declarative file related to the GUI, while what you need is something that is not tied to the GUI (database connection). Of course you can pass forth and back the database connection to the gui (ui forms) but you have to make it in the main or somewhere else. Moreover you don't have to hardcode connection parameters, you can get them from settings or as line arguments.



  • It cannot be done out of the box, as Qt Designer isn't even aware of the Qt SQL module. The only solution would be a custom widget wrapped in a Qt Designer plugin. But I doubt it's worth the effort.



  • Using the UI file and connecting variables to the form's variables

    So far I have found out that:

    After I have created my UI form "someForm"
    and If I add the following statement before my class definition:

    "namespace Ui {
    class SomeClass;
    }"

    And I declare the UI form privately in SomeClass by:

    private:
    "Ui::someForm *ui;"

    This will enable me to access each UI element of the form by passing the values:
    ui->someWidget;

    (The following function calculates an hourly wage and puts the calculation into a total QSpinBox)

    "double SomeClass::updateTotal()
    {
    double total = (ui->rateSpinBox->value() * ui->hourSpinBox->value());

    ui->totalSpinBox->setValue(total);
    

    }"

    Now I have to figure how to use this in conjunction with a QTableView.

    Thanks,

    Jay


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.