Insert data in QSqlTableModel based on checkbox choices



  • I have this dialog opened by the main window:
    @void MainWindow::on_pushButton_clicked()
    { //Add

    DiagAddProf dAddProf;
    if (dAddProf.exec() == QDialog::Accepted){
        dAddProf.setRoleProf(dataRole);
    }
    

    }@

    "dataRole" is a QSqlTableModel that i'm trying to pass to the dAddProf dialog, so it can use the setRoleProf method.
    The setRoleProf method verifies if every single checkbox, 9 of them, "isChecked()", then it adds the EditText typed text and the roles selected on the checkboxes in a row of dAddProf. It adds a row in the database for each relation (name, role).
    However, it seems i'm not able to do that. Main problem is, I tried to use signals and slots too, but it seems I can't just declare QSqlTableModel as a parameter for the setRoleProf function in the .h file, even importing QSql. Is there any other way to do this?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What's the exact error you get ? What is setRoleProf signature ?



  • I'm so sorry for not answering this earlier, I'm getting back to this project right now.

    The error for diagaddprof is:
    diagaddprof.cpp:23: error: prototype for 'void DiagAddProf::setRoleProf(QSqlTableModel)' does not match any in class 'DiagAddProf'

    setRoleProf is declared as:
    @void DiagAddProf::setRoleProf(QSqlTableModel tableRole)@

    And for the diagaddprof.h I have the header for the setRoleProf method:
    @public:
    QString getNameProf();
    void setRoleProf(QSqlTableModel *tableRole);
    explicit DiagAddProf(QWidget *parent = 0);
    ~DiagAddProf();@

    Note that #include <QSql> is in this header.


  • Lifetime Qt Champion

    You have a signature mismatch.

    In your header you have a pointer to a QSqlTable model but not in your implementation



  • I corrected that, but there's another error now.
    mainwindow.cpp:98: error: no matching function for call to 'DiagAddProf::setRoleProf(QSqlTableModel&)'

    diagaddprof.h:17: candidates are: void DiagAddProf::setRoleProf(QSqlTableModel)*

    mainwindow.cpp:98 refers to:
    @dAddProf.setRoleProf(dataRole);@

    as seen on the first post, being dataRole declared as:
    @dataProf = new QSqlTableModel(this);@


  • Lifetime Qt Champion

    How did you corrected that ? Adding a * in you implementation or removing the * from your header ?



  • I corrected that, but there's another error now.
    mainwindow.cpp:98: error: no matching function for call to 'DiagAddProf::setRoleProf(QSqlTableModel*&)'

    diagaddprof.h:17: candidates are: void DiagAddProf::setRoleProf(QSqlTableModel)

    mainwindow.cpp:98 refers to:
    @dAddProf.setRoleProf(dataRole);@

    as seen on the first post, being dataRole declared as:
    @dataRole = new QSqlTableModel(this);@


  • Lifetime Qt Champion

    @void setRoleProf(QSqlTableModel *tableRole)
    void DiagAddProf::setRoleProf(QSqlTableModel *tableRole)
    @

    Is the correct thing to have



  • Oh, thanks! Right, and added to that, that #include <QSql> is quite wrong, its QtSql... I just corrected that. It seemed strange that QSqlTableModel wasn't highlighted in the header...

    So now I got a pointer to the table. How can i effectively use the actual table?


  • Lifetime Qt Champion

    I would say that using the global includes are bad (they are useful when doing a quick test application or jumpstarting the development). They pull in all other headers that will get parsed and it'll slow down compilation. Just include what you use at the right place.

    Well... Just call the functions you want/need on it. I don't know what you want to do with it so I can't really answer that question.



  • Thanks a lot for all the tips and help (:


  • Lifetime Qt Champion

    You're welcome !

    If all your questions are answered don't forget to update the thread's title to solved so other forum users may know a solution has been found :)


Log in to reply
 

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