Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SIGSEGV signal in QSqlTableModel pointer

SIGSEGV signal in QSqlTableModel pointer

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AwayFromKeyboard
    wrote on last edited by
    #1

    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_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&#40;str&#41;)
    {
        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.)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      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.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AwayFromKeyboard
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            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.

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved