RSS

Monthly Archives: September 2010

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   http://qt.nokia.com/products/developer-tools/  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 http://www.yolinux.com/TUTORIAL/LinuxTutorialPostgreSQL.html 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();

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

private:
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”));
db.setHostName(“localhost”);
db.setDatabaseName(“storagebox”);
db.setUserName(“noob”);
bool ret = db.open();
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( ” ‘); “);

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

if(ret)
{
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;

if(!db.isOpen())
{
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)
{

que.exec(findr);

if( que.next())
{
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(outLine);
}
else
{
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  https://bitbucket.org/ar119/dbproject/get/f1dbf7bca3cd.zip

Advertisements
 
1 Comment

Posted by on September 8, 2010 in code

 

Tags: , , ,

Mapping the Internet

When surfing the most annoying thing that ive had the displeasure of running into are the “nationwalls” that seem to be popping up with annoying frequency.Content that is on the web and essentially pat of a “network of networks” is increasingly starting to look like a fragmented postcolonial map or ?s that precolonial?Anyway something bad is happening and if it continues as it has there is he very likely possibility that what the internet will look like 10 years from now is not something we will like the look of.
The problem as i see it is borders and nationalism, from Wikipedia:

“Borders define geographic boundaries of political entities or legal jurisdictions, such as governments, sovereign states, federated states and other subnational entities. Some borders–such as a state’s internal administrative borders, or inter-state borders within the Schengen Area–are open and completely unguarded. Other borders are partially or fully controlled, and may be crossed legally only at designated border checkpoints.”

That mentality is increasingly being applied to the internet by a whole host of governments in what obviously an attempt to co-opt and establish de -facto controls on what should be an unfettered public resource.And by public i mean everyone from the US President to the littlest goat herder in the outback(yeah wireless net is where it’s at ).Media content providers like Hulu have been operating under this model for some time now , restricting content to geographically based ips resulting in the inevitable torrenting.For the most part it’s not Hulu’s fault , more like the content creators who are yet to realise that the game has changed and unless they change with it they may not survive.

The internet by connections

Solution?Some years ago in a pc mag i saw a map of what the writers of the time envisioned what the maps of the future would look like.Less of lines in the sand and more geographically dispersed nodes of similar interests.
Neither 19th-century balance-of-power politics nor 20th-century power blocs are useful in understanding this new world. Instead, we have to look back nearly a thousand years, to the medieval age in which cities such as Cairo and Hangzhou were the centers of global gravity, expanding their influence confidently outward in a borderless world. When Marco Polo set forth from Venice along the emergent Silk Road, he extolled the virtues not of empires, but of the cities that made them great. He admired the vineyards of Kashgar and the material abundance of Xi’an, and even foretold — correctly — that no one would believe his account of Chengdu’s merchant wealth.

Internet communities

And finally Einstein:
“we should be on our guard not to overestimate science and scientific methods when it is a question of human problems; and we should not assume that experts are the only ones who have a right to express themselves on questions affecting the organization of society”
We all have a stake in what happens next and if the debates about net neutrality in the States are any indication its a matter that cannot be left to the hands of either business interests or government bureaucracy because in either case the losers in the end will be the users.

 
Leave a comment

Posted by on September 2, 2010 in Internet & networks

 

Tags: , , ,