Skip to content
  • 0 Votes
    4 Posts
    3k Views
    T

    @artwaw

    Thanks a lot ! It works.

    I don't know how I could miss this one. It seems that every time I restart the server, the IP changes. I will have to set a fixed IP adress.

  • 0 Votes
    3 Posts
    1k Views
    D

    So I tried the suggestions by @jwernerny. I dumped the QSqlQueryModel and went with a QStandardItemModel. TableView now displays correctly. Code now looks like this:

    void MainWindow::on_btnLoadTable_clicked() { conn = new Connection("LOCALHOST\\SQLEXPRESS", "Plants"); stdModel = new QStandardItemModel(this); conn->init(); //Set driver and connection options conn->open(); qry1 = new QSqlQuery(conn->db); const int COL_COUNT = 3; if (qry1->exec("spBatchesPerPlant")) { qDebug() << "Executing query..."; int row = 0; //used for counting rows in the query while (qry1->next()) //Loop through the results { for (int col = 0; col < COL_COUNT; col++) { stdItem = new QStandardItem(qry1->value(col).toString()); stdModel->setItem(row, col, stdItem); } row++; } ui->tableView->setModel(stdModel); } }

    Motto of the story: stored procedures generate forward only queries. QSqlQueryModel does not work for forward only queries, so use a standard item (or custom) model.

  • 0 Votes
    10 Posts
    2k Views
    O

    @jsulm You were right. I finally understood what you meant by getting the db at any time. Thank you very much for your help!

    This is my working code:

    #include "f1_system.h" #include <QtSql/qsqldatabase.h> #include <QtSql/qsqlquery.h> #include <QtSql/qsqlerror.h> #include <QtSql/qsqldriver.h> F1_System::F1_System(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "dataB"); QString connectionString = "Driver={SQL Server};Server=DESKTOP-4K9MAS2\\SQLEXPRESS01;Database=Formula1_BD;Trusted_Connection=yes;"; db.setDatabaseName(connectionString); connect(ui.loginPushButton, SIGNAL(clicked()), this, SLOT(validateLogin())); } F1_System::~F1_System() { QSqlDatabase::database("dataB").close(); } void F1_System::validateLogin() { if (QSqlDatabase::database("dataB").open()) { QString selectUser("SELECT Username FROM Users WHERE Username = '" + ui.usernameInputField->text() + "' AND Userpassword = '" + ui.passwordInputField->text() + "';"); QSqlQuery qry(selectUser, QSqlDatabase::database("dataB")); if (qry.next()) ui.label->setText("Status: You are logged in!"); else ui.label->setText("Status: Wrong credentials!\nTry again!"); } else ui.label->setText("Failed to connect to the database!"); }
  • 0 Votes
    4 Posts
    2k Views
    Andy314A

    @Ovidiu_GCO
    {SQL Server Native Client 17.0 };
    What is with the blank in front of "}"

  • 0 Votes
    3 Posts
    4k Views
    R

    @SGaist , thanks

  • 0 Votes
    10 Posts
    7k Views
    A

    @SGaist Thanks for your help.. it's successfuly got connected finally.. i changed localhost\sqlexpress to localhost and it got connected.. what ever sqlexpress is there for. Thanks again