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 = 0×000000;
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_OBJECT
public:
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 0×0 in read in psymtab, but not in symtab.)
↧