mardi 5 janvier 2016

Fill database sqlite tablewidget

I am writing "SQLite" database and I create the database table like this:

void MainWindow::createdata()
{
    QSqlQuery query;
    query.exec("DROP TABLE messages");
    query.exec("CREATE TABLE messages("
               "id INTEGER PRIMARY KEY AUTOINCREMENT,"
               "IPAddress VARCHAR(20),"
               "date VARCHAR(10),"
               "message VARCHAR(30))");

    query.prepare("INSERT INTO messages(IPAddress, date, message) values(?,?,?)");
    query.addBindValue("192.168.1.1");
    query.addBindValue("jun 3 2016");
    query.addBindValue("hello");

    if (query.exec()) {
        qDebug() << "ok!";
    }
    else
    {
      qDebug() << query.executedQuery();
      qDebug() << query.lastError();
    }
 }

And i create "qtablewidget" like this:

 QTableWidget* table = new QTableWidget();

 table->setRowCount(10);
 table->setColumnCount(4);
 table->setWindowTitle("Received Message");
 table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
  table->setHorizontalHeaderLabels(QString("ID;HostAddress;Date;Message").split(";"));
 table->setStyleSheet("QTableView {selection-background-color: blue;}");
 table->setEditTriggers(QAbstractItemView::NoEditTriggers);
 table->setSelectionBehavior(QAbstractItemView::SelectRows);
 table->setSelectionMode(QAbstractItemView::SingleSelection);


 QSqlQuery query("SELECT * FROM messages");

but i don't know how should i fill the "tablewidget" to show with query. Can anyone please help me?Thanks

Aucun commentaire:

Enregistrer un commentaire