SIGSEGV signal in QSqlTableModel pointer
-
Hi, everyone!
After a few days of searching trouble, I've surrend and ask you to help me.
Got the SIGSEGV signal and error, that is linked with a pointer to QSqlTableModel, that is declared in MainWindow.h file.
In MainWindow::initModel() I allocate memory to the QSqlTableModel pointers and I can use them ONLY IN THIS METHOD. When I'm out, for example in MainWindow::MainWindow(), the system gives me a SIGSEGV and a error.
When I tryed to debug it, I saw, that the adress of pointer didn't changed even if I type
modelShifr = 0x000000;
or even if i tryed to allocate memory by 'new' operator.
Questions:
1)Why the system gives a SIGSEGV, when i use modelShifr and modelZayavka out of method, where the memory was allocate?
2)Why don't these adresses won't to change, when i am allocate memory to it?
I can attach the full projects or screenshots, if needed.
Sorry about my English. Pretty sure I got much mistakes in grammar.//==========================================================
//mainWindow.h==============================================
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
bool needToCreateTable(QSqlDatabase *pdb, QString table);
bool initUI();
bool initModel(QSqlTableModel *model, QTableView *view, QString strTable, QSqlDatabase *pdb, QStringList &lst);
~MainWindow();private:
Ui::MainWindow *ui;QSqlTableModel *modelStation; //HERE ARE THIS POINTER QSqlTableModel *modelZayavka; //AND THIS ONE DataBaseZayavka *pdbz; TableShifr *ptab; QStringList strlistTableShifr; QStringList strlistTableStation; QStringList strVidPerevozki;
};
MainWindow.cpp
@//==========================================================
//mainWindow.cpp============================================
bool MainWindow::initModel(QSqlTableModel *model, QTableView *view, QString strTable, QSqlDatabase *pdb, QStringList &lst)
{
//creating and setting up the model
model = new QSqlTableModel(this, *pdb);model->setTable(strTable); model->setEditStrategy(QSqlTableModel::OnFieldChange); for(int i = 0; i < lst.count(); i++) { model->setHeaderData(i, Qt::Horizontal, lst.at(i)); } if(!model->select()) { qDebug()<<"Can't select table in model: " + model->lastError().text(); return false; } view->setModel(model); //hide unused columns for(int i = lst.count(); i < model->columnCount(); i++) { view->setColumnHidden(i, TRUE); } return true;
}
//-----------------------------------------------------------------------------
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
strlistTableShifr <<QString::fromUtf8("Íîìåð ôîðìû")
<<QString::fromUtf8("Âèä ïåðåâîçêè")
<<QString::fromUtf8("Ïîëó÷àòåëü")
<<QString::fromUtf8("Òèï ïëàíà")
<<QString::fromUtf8("Äåëåãàò");strlistTableStation <<QString::fromUtf8("Êîä ñòàíöèè") <<QString::fromUtf8("Íàèìåíîâàíèå"); ui->setupUi(this); pdbz = new DataBaseZayavka; pdbz->createConnection(); if(needToCreateTable(pdbz, "potok")) { pdbz->createTablePotok(); } if(needToCreateTable(pdbz, "station")) { pdbz->createTableStation(); } initModel(modelZayavka, ui->tableShifrZayavki, "potok", pdbz, strlistTableShifr); initModel(modelStation, ui->tablePoluchatel, "station", pdbz, strlistTableStation);
// modelZayavka->index(0, 0); // HERE I GOT THE ERROR (SIGSEGV signal)
if(!initUI()) { exit(1); }
}
//-----------------------------------------------------------------------------
bool MainWindow::needToCreateTable(QSqlDatabase *pdb, QString table)
{
QSqlQuery query(*pdb);
if(!query.exec("SELECT * FROM " + table + ";"))
{
qDebug()<<"Error while selecting data from table "" + table;
return true;
}
return false;
}
@MyDatasbase.cpp
@//==========================================================
//dataBaseZayavka.cpp=======================================
bool DataBaseZayavka::createConnection()
{
//QSqlDatabase db; //in .h file
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("zayavka");
db.setUserName("zykis");
db.setHostName("localhost");
db.setPassword("pass");if(!db.open()) { qDebug()<<"Can't open Zayavka database: "<<db.lastError().text(); return false; } return true;
}
//-----------------------------------------------------------------------------
bool DataBaseZayavka::createTableStation()
{
QSqlQuery query;QString str = "CREATE TABLE station ( " "NS INTEGER PRIMARY KEY, " //Êîä ñòàíöèè PRIMARY KEY "NA CHARVAR(28) " //Íàèìåíîâàíèå ñòàíöèè ");"; if(!query.exec(str)) { qDebug()<<"Error of creating table station: " + query.lastError().text(); return false; } return true;
}@
Here is some debugger code:
Debugger started...
Temporarily disabling breakpoints for unloaded shared library "C:\Qt\Qt5.0.1\5.0.1\mingw47_32\plugins\platforms\qminimald.dll"
Temporarily disabling breakpoints for unloaded shared library "C:\Qt\Qt5.0.1\5.0.1\mingw47_32\plugins\platforms\qwindowsd.dll"
Temporarily disabling breakpoints for unloaded shared library "C:\Qt\Qt5.0.1\5.0.1\mingw47_32\plugins\sqldrivers\qsqlited.dll"
(Internal error: pc 0x0 in read in psymtab, but not in symtab.) -
Hi,
simply you MUST change "initModule" signature from
@bool MainWindow::initModel(QSqlTableModel *model, QTableView *view, QString strTable, QSqlDatabase *pdb, QStringList &lst)@to
@bool MainWindow::initModel(QSqlTableModel *&model, QTableView *view, QString strTable, QSqlDatabase *pdb, QStringList &lst)@this is beacuse in your version, after initModel call, the input parameter isn't changed.
-
Wow!
Thank you so much!
I Wondered if it is
@bool MainWindow::initModel(QSqlTableModel *&model, QTableView *view, QString strTable, QSqlDatabase *pdb, QStringList &lst)@not the same as
@bool MainWindow::initModel(QSqlTableModel model, QTableView *view, QString strTable, QSqlDatabase *pdb, QStringList &lst)@
?Don't know why, but that's really work!
O_o -
Hi,
May I suggest you to have a look at the QtSql module examples ? From your code it seems that you are going to complicate things a bit for what you want to do i.e. you should not create the model in initModel but pass it an already existing model and it seems that's what you do if DataBaseZayavka is a QSqlTableModel.
Also QSqlDataBase db does not need to be stored, it's becoming the default database once you have initialized it and further operation will be done on it.
Hope it helps
-
Hi
difference from
@ bool MainWindow::initModel(QSqlTableModel *&model, QTableView *view, QString strTable, QSqlDatabase *pdb, QStringList &lst)
@and
@ bool MainWindow::initModel(QSqlTableModel *model, QTableView *view, QString strTable, QSqlDatabase *pdb, QStringList &lst)@is that in first version you allow the model pointer to be changed; in the second version the caller never see the model value changed after method call.
Formally you MUST pass model (QSqlTableModel*) by reference.