Na poniższym zrzucie mam na pierwszym planie okno QDialog
a w tle jest okno QMainWindow
pytanie jest takie
jak po zmianie danych w oknie pokazanym na pierwszym tle zaktualizować model aby dane równocześnie pojawiły się w oknie na drugim planie ? Dodam, że oba okna korzystają z tego samego modelu
kod modelu
#include "sqlquerymodelborrowingusers.h"
#include <QDebug>
SqlQueryModelBorrowingUsers::SqlQueryModelBorrowingUsers(QObject *parent, QSqlDatabase *dbConnect) : QSqlQueryModel{parent}
{
db = dbConnect;
QString userData = "SELECT pIDuserWyp, pImie, pNazwisko FROM 'tUzytkownicyWypozyczajacy'";
this->setQuery(userData, *dbConnect);
this->query().exec();
}
int SqlQueryModelBorrowingUsers::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
int row=0;
QString rows = "SELECT count(*) FROM 'tUzytkownicyWypozyczajacy'";
QSqlQuery queryRow(*db);
queryRow.exec(rows);
QSqlRecord recRow;
queryRow.first();
recRow = queryRow.record();
row = recRow.value(0).toInt();
return row;
}
int SqlQueryModelBorrowingUsers::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
int column=0;
QString columns = "SELECT count(*) FROM PRAGMA_table_info('tUzytkownicyWypozyczajacy')";
QSqlQuery queryColumn(*db);
queryColumn.exec(columns);
QSqlRecord recColumn;
queryColumn.first();
recColumn = queryColumn.record();
column = recColumn.value(0).toInt();
return column;
}
QVariant SqlQueryModelBorrowingUsers::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role==Qt::DisplayRole && orientation==Qt::Horizontal){
if(section==1){
QVariant header1;
header1="Imię";
return header1;
}
if(section==2){
QVariant header2;
header2="Nazwisko";
return header2;
}
}
if(role==Qt::DisplayRole && orientation==Qt::Vertical){
return QString("%1").arg(section+1);
}
return QVariant();
}
QVariant SqlQueryModelBorrowingUsers::data(const QModelIndex &index, int role) const
{
if(this->query().first() && this->query().isValid()){
if(this->query().isActive()){
if(this->query().isSelect()){
QVariant myData = QSqlQueryModel::data(index, role);
if(role==Qt::FontRole && index.column()==1){
QFont font;
font.setWeight(QFont::ExtraBold);
myData = font.toString();
}
if(role==Qt::TextAlignmentRole && (index.column()==1 || index.column()==2)){
return Qt::AlignCenter;
}
if(role==Qt::BackgroundRole && index.row() % 2 == 0){
QColor color;
color.setRgb(176,161,106,69);
myData = color;
}
if(role==Qt::BackgroundRole && index.row() % 2 == 1){
QColor color;
color.setRgb(112,102,67,44);
myData = color;
}
return myData;
}
}
}
else if(this->query().isValid()==false){
return QVariant();
}
return QVariant();
}
bool SqlQueryModelBorrowingUsers::setData(const QModelIndex &index, const QVariant &value, int role)
{
QModelIndex pID = QSqlQueryModel::index(index.row(),0,QModelIndex());
int _pID = data(pID,Qt::EditRole | Qt::DisplayRole).toInt();
bool isSet=false;
if(!index.isValid()){
return false;
}
else if(index.column()<1){
return false;
}
else{
if(role==Qt::EditRole || role==Qt::DisplayRole){
if(index.column()==1){
isSet = setName(_pID, value.toString());
this->refresh();
return isSet;
}
if(index.column()==2){
isSet = setSurname(_pID, value.toString());
this->refresh();
return isSet;
}
}
}
return isSet;
}
Qt::ItemFlags SqlQueryModelBorrowingUsers::flags(const QModelIndex &index) const
{
Qt::ItemFlags flag = QSqlQueryModel::flags(index); //export domyślnie ustawionych flag przez klasę nadrzędną
if(!index.isValid()){
return Qt::NoItemFlags;
}
else if(index.isValid()){
if(index.column()==1 || index.column()==2){
flag |= Qt::ItemIsEditable; //dopisanie nowej flagi do wcześniej wyeksportowanych flag
return flag;
}
}
return flag;
}
bool SqlQueryModelBorrowingUsers::setName(int pID, const QString &pName)
{
QSqlQuery queryUpdateName;
QVariant _pID;
_pID = pID;
QString strUpdateName = "UPDATE 'tUzytkownicyWypozyczajacy' SET pImie = ? WHERE pIDuserWyp = ?";
queryUpdateName.prepare(strUpdateName);
queryUpdateName.addBindValue(pName);
queryUpdateName.addBindValue(_pID.toString());
return queryUpdateName.exec();
}
bool SqlQueryModelBorrowingUsers::setSurname(int pID, const QString &pSurname)
{
QSqlQuery queryUpdateSurname;
QVariant _pID;
_pID = pID;
QString strUpdateSurname = "UPDATE 'tUzytkownicyWypozyczajacy' SET pNazwisko = ? WHERE pIDuserWyp = ?";
queryUpdateSurname.prepare(strUpdateSurname);
queryUpdateSurname.addBindValue(pSurname);
queryUpdateSurname.addBindValue(_pID.toString());
return queryUpdateSurname.exec();
}
void SqlQueryModelBorrowingUsers::myAddRow()
{
QSqlQuery lastID, addRecord;
QString strGetID = "SELECT pIDuserWyp FROM 'tUzytkownicyWypozyczajacy'";
lastID.prepare(strGetID);
int id=0;
QVector<int> tabID;
if(lastID.exec()){
if(lastID.isActive()){
if(lastID.isSelect()){
while (lastID.next()){
id=lastID.value(0).toInt();
tabID.push_back(id);
}
}
}
}
QVariant getID, setID, saveID;
getID = max(tabID);
if(lastID.last()>=1){
setID=getID.toInt()+1;
saveID=setID.toString();
QString strAddRecord = "INSERT INTO 'tUzytkownicyWypozyczajacy' (pIDuserWyp, pImie, pNazwisko) VALUES (?,'','')";
addRecord.prepare(strAddRecord);
addRecord.addBindValue(saveID.toString(),QSql::In);
addRecord.exec();
}
else if(lastID.last()==0){
QString strAddRecord = "INSERT INTO 'tUzytkownicyWypozyczajacy' (pIDuserWyp, pImie, pNazwisko) VALUES (1,'','')";
addRecord.prepare(strAddRecord);
addRecord.exec();
}
refresh();
}
void SqlQueryModelBorrowingUsers::myRemoveRow()
{
QModelIndex getIDfromTable;
QVariant getIDtoRemove;
QSqlQuery currentID, removeRow;
QString strGetCurrentID = "SELECT pIDuserWyp FROM 'tUzytkownicyWypozyczajacy'";
currentID.prepare(strGetCurrentID);
int i=0;
if(currentID.exec()){
if(currentID.isActive()){
if(currentID.isSelect()){
while (currentID.next()){
i=currentID.at();
if(i==this->row){
getIDfromTable=index(i,0,QModelIndex());
getIDtoRemove=getIDfromTable.data();
}
}
}
}
}
QString strRemoveRow = "DELETE FROM 'tUzytkownicyWypozyczajacy' WHERE pIDuserWyp = (?)";
removeRow.prepare(strRemoveRow);
removeRow.addBindValue(getIDtoRemove.toString(),QSql::In);
removeRow.exec();
refresh();
}
void SqlQueryModelBorrowingUsers::refresh()
{
this->query().clear();
this->setQuery("SELECT pIDuserWyp, pImie, pNazwisko FROM 'tUzytkownicyWypozyczajacy'");
this->query().exec();
}
SqlQueryModelBorrowingUsers::~SqlQueryModelBorrowingUsers()
{
this->query().clear();
}