Got some more questions with this learning if I may.
I managed to get the program working to some extent, however, I suspect I'm missing something fundamental here.
I found that removing a column in one query caused me not to be able to recover the value in another part. My understanding was that removecolumn simply hid the data from the UI, apparently this isn't the case. I fixed this by reordering the data retrieval put in the combo box model thus
void MainWindow::populateGroupBox(void)
{
comboQueryModel->setQuery("select sharename, shareid from shareid");
//comboQueryModel->removeColumn(0);
ui->comboBox->setModel(comboQueryModel);
}
void MainWindow::populateGroupBox(void)
{
comboQueryModel = new QSqlQueryModel;
comboQueryModel->setQuery("select sharename, shareid from shareid");
//comboQueryModel->removeColumn(0);
ui->comboBox->setModel(comboQueryModel);
}
To copy to clipboard, switch view to plain text mode
Now.. the remaining issue is to do with parameterised queries.
The following works (building the string by hand each time)
void MainWindow::updatePriceData(int idx)
{
if(record.isEmpty())
{
qDebug() << "Error! Record is empty";
return;
}
QVariant shareidx
= record.
value("shareid");
QString string
= "select p.shareid, p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid where p.shareid =";
string = string + shareidx.toString();
priceQueryModel->setQuery(*query);
if(priceQueryModel->lastError().isValid())
qDebug() << priceQueryModel->lastError();
ui->tableView->setModel(priceQueryModel);
priceQueryModel->removeColumn(0);
priceQueryModel
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("Closing Date"));
priceQueryModel
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Share Price"));
priceQueryModel
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("Total Value"));
priceQueryModel
->setHeaderData
(3, Qt
::Horizontal,
QObject::tr("Company"));
}
void MainWindow::updatePriceData(int idx)
{
QSqlRecord record = comboQueryModel->record(idx);
if(record.isEmpty())
{
qDebug() << "Error! Record is empty";
return;
}
QVariant shareidx = record.value("shareid");
priceQueryModel = new QSqlQueryModel;
QString string = "select p.shareid, p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid where p.shareid =";
string = string + shareidx.toString();
query = new QSqlQuery(string);
priceQueryModel->setQuery(*query);
if(priceQueryModel->lastError().isValid())
qDebug() << priceQueryModel->lastError();
ui->tableView->setModel(priceQueryModel);
priceQueryModel->removeColumn(0);
priceQueryModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Closing Date"));
priceQueryModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Share Price"));
priceQueryModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Total Value"));
priceQueryModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Company"));
}
To copy to clipboard, switch view to plain text mode
This however does not work
void MainWindow::updatePriceData(int idx)
{
if(record.isEmpty())
{
qDebug() << "Error! Record is empty";
return;
}
QVariant shareidx
= record.
value("shareid");
query->prepare("select p.shareid, p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid where p.shareid = :shareidx");
query->bindValue(":shareidx", shareidx.toInt());
priceQueryModel->setQuery(*query);
if(priceQueryModel->lastError().isValid())
qDebug() << priceQueryModel->lastError();
ui->tableView->setModel(priceQueryModel);
priceQueryModel->removeColumn(0);
priceQueryModel
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("Closing Date"));
priceQueryModel
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Share Price"));
priceQueryModel
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("Total Value"));
priceQueryModel
->setHeaderData
(3, Qt
::Horizontal,
QObject::tr("Company"));
}
void MainWindow::updatePriceData(int idx)
{
QSqlRecord record = comboQueryModel->record(idx);
if(record.isEmpty())
{
qDebug() << "Error! Record is empty";
return;
}
QVariant shareidx = record.value("shareid");
priceQueryModel = new QSqlQueryModel;
query = new QSqlQuery;
query->prepare("select p.shareid, p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid where p.shareid = :shareidx");
query->bindValue(":shareidx", shareidx.toInt());
priceQueryModel->setQuery(*query);
if(priceQueryModel->lastError().isValid())
qDebug() << priceQueryModel->lastError();
ui->tableView->setModel(priceQueryModel);
priceQueryModel->removeColumn(0);
priceQueryModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Closing Date"));
priceQueryModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Share Price"));
priceQueryModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Total Value"));
priceQueryModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Company"));
}
To copy to clipboard, switch view to plain text mode
and I'm not sure why. The parameterised query would, I'm guessing be much more efficient.
I also found that when I had the shareidx value as an integer and tried to add that to the query string in the first instance that it added a \ infront of the number each time for some reason. Why is that?
ie.. the string would become s.shareid where p.shareid = \7 for example instead of =7.
Thanks in anticipation. I'm slowly getting there.
Peter.
Bookmarks