PDA

View Full Version : problem in QSqlQuery



mammaiap
12th December 2011, 13:40
Hi Friends,

i am facing QSqlQuery related problem....i am explaining the issue here...

First i created the Pushbutton.... whenever this pushbutton clicked, i am making the QSqlQuery and display the results in the QTableView....

the problem what i am facing is ...."it works fine at first time within the session...."... but it is not working when user launching the second time(in the same session)...

here is the code snippet...

in the TreeWidget constructor i am creating the "loadcompaignbutton" pushbutton...

loadcompaignbutton = new QPushButton(this);
loadcompaignbutton->setGeometry(QRect(0,0,75,23));
loadcompaignbutton->setText("Load");
row = 0;
noofcampaign = 0;
connect(loadcompaignbutton,SIGNAL(clicked()),this, SLOT(loadLSWTCamp()));



void TreeWidget::loadLSWTCamp()
{
clearLists();
campaignwidget = new QWidget();
campaignwidget->resize(470,500);
campaignwidget->setMinimumSize(QSize(470,500));
campaignwidget->setMaximumSize(QSize(470,500));
campaignwidget->setWindowTitle("Select Campaign");
table = new QTableView(campaignwidget);
table->setGeometry(QRect(10, 10, 448, 421));
QSizePolicy sizePolicy2(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy2.setHorizontalStretch(2);
sizePolicy2.setVerticalStretch(0);
sizePolicy2.setHeightForWidth(table->sizePolicy().hasHeightForWidth());
table->setSizePolicy(sizePolicy2);
// table->setContextMenuPolicy(Qt::ActionsContextMenu);
table->setSelectionBehavior(QAbstractItemView::SelectRows );
okpb = new QPushButton(campaignwidget);
okpb->setGeometry(QRect(290, 450, 75, 23));
okpb->setText("Select");
cancelpb = new QPushButton(campaignwidget);
cancelpb->setGeometry(QRect(375, 450, 75, 23));
cancelpb->setText("Close");
connect(okpb,SIGNAL(clicked()),this,SLOT(getSeries Datas()));
connect(cancelpb,SIGNAL(clicked()),this,SLOT(close CampaignWidget()));
setCampaignQuery(mainwindow->db);
campaignwidget->show();
}
void TreeWidget::setCampaignQuery(const QSqlDatabase &db)
{
db.database("DEFAULT");
QSqlQuery query(db);
query.setForwardOnly(true);
query.prepare("SELECT Nr,Name,AirfoilID,Description FROM Campaign");
query.exec();
if (!query.exec())
qDebug() << query.lastError()<<endl;
if(query.exec()== true)
{
while (query.next())
{
nr.append(query.value(0).toString());
name.append(query.value(1).toString());
airfoil.append(query.value(2).toString());
des.append(query.value(3).toString());
}
}else{
qDebug()<<query.lastError().text()<<endl;
}
// int noofrows = nr.count();
model = new QStandardItemModel(this);
// model->setRowCount(noofrows);
model->setColumnCount(4);
model->setHeaderData(0, Qt::Horizontal, tr("Nr"));
model->setHeaderData(1, Qt::Horizontal, tr("Name"));
model->setHeaderData(2, Qt::Horizontal, tr("AirfoilID"));
model->setHeaderData(3, Qt::Horizontal, tr("Description"));
table->setModel(model);
selectionModel = new QItemSelectionModel(model);
table->setSelectionModel(selectionModel);
for(int i=0;i<nr.count();i++)
{
model->insertRows(row, 1, QModelIndex());
model->setData(model->index(row, 0, QModelIndex()),nr.value(i));
model->setData(model->index(row, 1, QModelIndex()),name.value(i));
model->setData(model->index(row, 2, QModelIndex()),airfoil.value(i));
model->setData(model->index(row, 3, QModelIndex()),des.value(i));
row++;
}
query.finish();
}

I am using QODBC in windows.......

please have a look at the attached image files...

can anyone help me to fix this bug....?

Thanks & Regards,
Muthulingam

mammaiap
14th December 2011, 03:59
Hi Friends,

i have solved the problem....

Thanks & Regards,
Muthulingam