#include <QtDebug>#include <QtGui>#include <QSqlDatabase>#include <QSqlQueryModel>#include <QSqlError>#include <QTableView> int main(int argc, char *argv[]){ QApplication app(argc, argv); QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("localhost"); db.setDatabaseName("testdb"); db.setUserName("andrei"); db.setPassword("letmein"); if (!db.open()) { qDebug() << QObject::trUtf8("Бд не найдена") << db.lastError().text(); return -1; } QSqlQueryModel * model = new QSqlQueryModel(0); model->setQuery("SELECT * FROM music"); model->setHeaderData(0, Qt::Horizontal, QObject::trUtf8("Автор")); model->setHeaderData(1, Qt::Horizontal, QObject::trUtf8("Альбом")); model->setHeaderData(2, Qt::Horizontal, QObject::trUtf8("Произведение")); model->setHeaderData(3, Qt::Horizontal, QObject::trUtf8("Год выхода")); QTableView * view = new QTableView(0); view->setModel(model); view->setWindowTitle(QObject::trUtf8("Музыкальный каталог")); view->show(); return app.exec();}
TEMPLATE = appQT += sql TARGET = DEPENDPATH += .INCLUDEPATH += .SOURCES += main.cpp
cd $QTDIR/src/plugins/sqldrivers/psqlqmake "INCLUDEPATH+=/usr/include/pgsql" "LIBS+=-L/usr/lib -lpq" psql.promake