Simple Qt4 and PostgreSQL book catalog application

For this current project we shall create a basic book storage application using the QT toolkit and PostgreSQL.Download and install QT  it comes with its own IDE qtcreator which we shall use for the actual work although any text editor can be used.Though that would unnecessarily prolong the length of the project.

Download and install the PostgreSQL database and configure it for use gives a good rundown if you are working on the linux platform.

Step1.Select the creation of  a new project.In the projects selection choose Qt4 Gui Application.Select the location and name of the project and in the required modules include the QtSql module.

Step2.Select forms from the Navmenu and open up the mainwindow.ui for editing.Design the layout of your app by using the available widgets.In this case we select LineEdit for the input.change their object names to something meaningful.Select pushbuttons and drag onto the workspace , give them useful names.

Step3.The design phase is now over and we shall proceed to coding the internals.Switch to Signal/Slot edit mode(F4).Direct the signals from the buttons to the main layout.In the configure connection pop-up select edit and add 3 new slots one each for clear , add and search.Link the clicked() action to our newly created slots.

Step4.Select the headers and in mainwindow.h add private slots addpub() , clearpub() and searchpub().In the protected section we shall add an auxillary function openDB() for establishing the database connection.In the private section we add a QSqlDatabase object db.

private slots:
void addpub();
void clearpub();
void searchpub();

void changeEvent(QEvent *e);
bool openDB();

Ui::MainWindow *ui;
QSqlDatabase db;

Step5.We define the functions from step 4 , establishing the connection and inserting records with addpub() , searching for a particular record with searchpub() and clearing the form entries using clearpub().

-openDB function to connect to the database:

bool MainWindow::openDB()
db  = (QSqlDatabase::addDatabase(“QPSQL”));
bool ret =;
return ret;

-addpub function to insert from the form to the database

void MainWindow::addpub()

QString args = “INSERT INTO shelf VALUES (‘ “;;
bool ret = false;
bool lck = false;

args.append(ui->titleline->text()); args.append(“‘,'”);
args.append(ui->authorline->text()); args.append(“‘,'”);
args.append(ui->publisherline->text()); args.append(“‘,'”);
args.append(ui->isbnline->text()); args.append(“‘,'”);
args.append(ui->genreline->text()); args.append( ” ‘); “);

lck = MainWindow::openDB();
ui->resultline->setText(“Pub insertion starting”);
if( lck )
ui->resultline->setText(“Pub is being inserted”);
QSqlQuery query;
ret = query.exec(args);

qDebug() <<“Pub has been inserted”;
ui->resultline->setText(“Pub has been inserted”);



-searchpub function to search for a stored record:

void MainWindow::searchpub()
bool ret = false;

ret = MainWindow::openDB();

QString outLine;
QString findr;
QSqlQuery que;
findr = “select * from shelf where “;   findr += ui->srcq->currentText();   findr += ” like ‘%”;
findr += ui->searchline->text();        findr += ” %’ ;”;
if (ret)


outLine = que.value(0).toString();  outLine +=”\n”;
outLine.append(que.value(1).toString());    outLine +=”\n”;
outLine.append(que.value(2).toString());    outLine +=”\n”;
outLine.append(que.value(3).toString());    outLine +=”\n”;
outLine.append(que.value(4).toString());    outLine +=”\n”;

ui->textEdit->setText(“No result found”);


Step6.At this point you are ready to compile and run your application.

To download the project files used in this example

1 thought on “Simple Qt4 and PostgreSQL book catalog application”

  1. My husband and i ended up being quite lucky Peter could carry out his homework from your ideas he was given from your blog. It’s not at all simplistic to simply happen to be giving out helpful hints which the others have been trying to sell. And now we see we now have the writer to be grateful to for that. The entire illustrations you made, the simple blog navigation, the relationships you will make it easier to engender – it’s all incredible, and it’s really leading our son in addition to the family consider that this idea is cool, which is certainly seriously mandatory. Thanks for the whole thing!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s