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. tableView->setModel(model); causes SIGSEGV.
Forum Updated to NodeBB v4.3 + New Features

tableView->setModel(model); causes SIGSEGV.

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 4 Posters 2.3k Views 2 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #17

    You should really strip this down to a minimal, compileable example. Simply open a db connection, create and fill a table and set the query to the model. Modify it to your use case until it is crashing or you can see what you are doing wrong.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    0
    • snubbenS snubben

      So I debugged some more and if I understand correctly it seg faults in qsql_mysql.cpp at

      bool QMYSQLResult::fetch(int i)
      {
          Q_D(QMYSQLResult);
          if (!driver())
              return false;
          if (isForwardOnly()) { // fake a forward seek
              if (at() < i) {
                  int x = i - at();
                  while (--x && fetchNext()) {};
                  return fetchNext();
              } else {
                  return false;
              }
          }
          if (at() == i)
              return true;
          if (d->preparedQuery) {
              mysql_stmt_data_seek(d->stmt, i);
      
              int nRC = mysql_stmt_fetch(d->stmt); <<<< HERE SEG FAULT
              if (nRC) {
                  if (nRC == 1 || nRC == MYSQL_DATA_TRUNCATED)
                      setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult",
                               "Unable to fetch data"), QSqlError::StatementError, d->stmt));
                  return false;
              }
          } else {
              mysql_data_seek(d->result, i);
              d->row = mysql_fetch_row(d->result);
              if (!d->row)
                  return false;
          }
      
          setAt(i);
          return true;
      }
      

      d pointer has the following information:

      	*d	@0x5555557afe10	QMYSQLResultPrivate
      		[QSqlResultPrivate]	<not accessible>	QSqlResultPrivate
      		fields	<5 items>	QList<QMYSQLResultPrivate::QMyField>
      			[0]	@0x55555573e930	QMYSQLResultPrivate::QMyField
      				bufLength	401	ulong
      				*myField	@0x555555929d88	MYSQL_FIELD
      					catalog	"def"	char *
      					catalog_length	3	unsigned int
      					charsetnr	45	unsigned int
      					db	"database_name"	char *
      					db_length	9	unsigned int
      					decimals	0	unsigned int
      					def	0x0	char *
      					def_length	0	unsigned int
      					extension	0x0	void *
      					flags	20483	unsigned int
      					length	400	unsigned long
      					max_length	0	unsigned long
      					name	"field1"	char *
      					name_length	7	unsigned int
      					org_name	"field1"	char *
      					org_name_length	7	unsigned int
      					org_table	"products"	char *
      					org_table_length	11	unsigned int
      					table	"products"	char *
      					table_length	11	unsigned int
      					type	MYSQL_TYPE_VAR_STRING (253)	enum_field_types
      				nullIndicator	0	my_bool
      				outField	""	char *
      				type	@0x55555573e940	QMetaType
      			[1]	@0x55555573e958	QMYSQLResultPrivate::QMyField
      				bufLength	401	ulong
      				*myField	@0x555555929e08	MYSQL_FIELD
      					catalog	"def"	char *
      					catalog_length	3	unsigned int
      					charsetnr	45	unsigned int
      					db	"database_name"	char *
      					db_length	9	unsigned int
      					decimals	0	unsigned int
      					def	0x0	char *
      					def_length	0	unsigned int
      					extension	0x0	void *
      					flags	4097	unsigned int
      					length	400	unsigned long
      					max_length	0	unsigned long
      					name	"name"	char *
      					name_length	4	unsigned int
      					org_name	"name"	char *
      					org_name_length	4	unsigned int
      					org_table	"products"	char *
      					org_table_length	11	unsigned int
      					table	"products"	char *
      					table_length	11	unsigned int
      					type	MYSQL_TYPE_VAR_STRING (253)	enum_field_types
      				nullIndicator	0	my_bool
      				outField	""	char *
      				type	@0x55555573e968	QMetaType
      			[2]	@0x55555573e980	QMYSQLResultPrivate::QMyField
      				bufLength	4	ulong
      				*myField	@0x555555929e88	MYSQL_FIELD
      					catalog	"def"	char *
      					catalog_length	3	unsigned int
      					charsetnr	63	unsigned int
      					db	"database_name"	char *
      					db_length	9	unsigned int
      					decimals	0	unsigned int
      					def	0x0	char *
      					def_length	0	unsigned int
      					extension	0x0	void *
      					flags	36865	unsigned int
      					length	11	unsigned long
      					max_length	0	unsigned long
      					name	"prodCount"	char *
      					name_length	5	unsigned int
      					org_name	"prodCount"	char *
      					org_name_length	10	unsigned int
      					org_table	"products"	char *
      					org_table_length	11	unsigned int
      					table	"products"	char *
      					table_length	11	unsigned int
      					type	MYSQL_TYPE_LONG (3)	enum_field_types
      				nullIndicator	0	my_bool
      				outField	""	char *
      				type	@0x55555573e990	QMetaType
      			[3]	@0x55555573e9a8	QMYSQLResultPrivate::QMyField
      				bufLength	401	ulong
      				*myField	@0x555555929f08	MYSQL_FIELD
      					catalog	"def"	char *
      					catalog_length	3	unsigned int
      					charsetnr	45	unsigned int
      					db	"database_name"	char *
      					db_length	9	unsigned int
      					decimals	0	unsigned int
      					def	0x0	char *
      					def_length	0	unsigned int
      					extension	0x0	void *
      					flags	4097	unsigned int
      					length	400	unsigned long
      					max_length	0	unsigned long
      					name	"field3"	char *
      					name_length	4	unsigned int
      					org_name	"field3"	char *
      					org_name_length	17	unsigned int
      					org_table	"products"	char *
      					org_table_length	11	unsigned int
      					table	"products"	char *
      					table_length	11	unsigned int
      					type	MYSQL_TYPE_VAR_STRING (253)	enum_field_types
      				nullIndicator	0	my_bool
      				outField	""	char *
      				type	@0x55555573e9b8	QMetaType
      			[4]	@0x55555573e9d0	QMYSQLResultPrivate::QMyField
      				bufLength	40	ulong
      				*myField	@0x555555929f88	MYSQL_FIELD
      					catalog	"def"	char *
      					catalog_length	3	unsigned int
      					charsetnr	63	unsigned int
      					db	"database_name"	char *
      					db_length	9	unsigned int
      					decimals	0	unsigned int
      					def	0x0	char *
      					def_length	0	unsigned int
      					extension	0x0	void *
      					flags	128	unsigned int
      					length	10	unsigned long
      					max_length	0	unsigned long
      					name	"dateMin"	char *
      					name_length	8	unsigned int
      					org_name	"dateMin"	char *
      					org_name_length	8	unsigned int
      					org_table	"products"	char *
      					org_table_length	11	unsigned int
      					table	"products"	char *
      					table_length	11	unsigned int
      					type	MYSQL_TYPE_DATE (10)	enum_field_types
      				nullIndicator	0	my_bool
      				outField	""	char *
      				type	@0x55555573e9e0	QMetaType
      		hasBlobs	false	bool
      		*inBinds	@0x5555557ce380	MYSQL_BIND
      			buffer	0x5555556f1d30	void *
      			buffer_length	401	unsigned long
      			buffer_type	MYSQL_TYPE_STRING (254)	enum_field_types
      			error	0x0	my_bool *
      			error_value	0	my_bool
      			extension	0x0	void *
      			fetch_result	0x0	void (st_mysql_bind *, MYSQL_FIELD *, unsigned char * *) *
      			flags	0	unsigned int
      			is_null	""	my_bool *
      			is_null_value	0	my_bool
      			is_unsigned	0	my_bool
      			*length	401	unsigned long
      			length_value	0	unsigned long
      			long_data_used	0	my_bool
      			offset	0	unsigned long
      			pack_length	0	unsigned int
      			skip_result	0x0	void (st_mysql_bind *, MYSQL_FIELD *, unsigned char * *) *
      			store_param_func	0x0	void (NET *, st_mysql_bind *) *
      			u	@0x5555557ce3a0	union {...}
      				indicator	0x0	char *
      				row_ptr	0x0	unsigned char *
      		*meta	@0x5555556eedf0	MYSQL_RES
      			current_field	0	unsigned int
      			current_row	0	MYSQL_ROW
      			data	0x0	MYSQL_DATA *
      			data_cursor	0x0	MYSQL_ROWS *
      			eof	1	my_bool
      			field_alloc	@0x5555556eee18	MA_MEM_ROOT
      			field_count	5	unsigned int
      			*fields	@0x555555929d88	MYSQL_FIELD
      			handle	0x0	MYSQL *
      			is_ps	0	my_bool
      			lengths	0x0	unsigned long *
      			row	0	MYSQL_ROW
      			row_count	0	unsigned long long
      		outBinds	0x0	MYSQL_BIND *
      		preparedQuery	true	bool
      		result	0x0	MYSQL_RES *
      		row	0	MYSQL_ROW
      		rowsAffected	-1	int
      		*stmt	@0x5555556c81a0	MYSQL_STMT
      			array_size	0	unsigned int
      			*bind	@0x55555592a198	MYSQL_BIND
      			bind_param_done	0	my_bool
      			bind_result_done	1	my_bool
      			cursor_exists	0	my_bool
      			default_rset_handler	0x7ffff0cf5880 <mysql_next_result+224>	mysql_stmt_use_or_store_func
      			execute_count	1	unsigned int
      			extension	0x5555556a9700	void *
      			fetch_row_func	140737233504192	mysql_stmt_fetch_row_func
      			field_count	5	unsigned int
      			*fields	@0x555555929d88	MYSQL_FIELD
      			flags	0	unsigned long
      			last_errno	0	unsigned int
      			last_error	"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000..."	char[513] of length 513
      			list	@0x5555556c84c0	LIST
      			mem_root	@0x5555556c81a0	MA_MEM_ROOT
      			*mysql	@0x5555556a8280	MYSQL
      			param_callback	0	ps_param_callback
      			param_count	0	unsigned int
      			params	0x0	MYSQL_BIND *
      			prebind_params	0	unsigned int
      			prefetch_rows	1	unsigned long
      			request_buffer	0x0	unsigned char *
      			request_length	0	size_t
      			result	@0x5555556c8220	MYSQL_DATA
      			result_callback	0	ps_result_callback
      			*result_cursor	@0x55555590fa68	MYSQL_ROWS
      			row_size	0	size_t
      			send_types_to_server	'\0'	0	0x00	unsigned char
      			sqlstate	"00000\000"	char[6] of length 6
      			state	MYSQL_STMT_USER_FETCHING (5)	enum_mysqlnd_stmt_state
      			stmt_id	18	unsigned long
      			update_max_length	0	my_bool
      			upsert_status	@0x5555556c8290	mysql_upsert_status
      			user_data	0x0	void *
      
      

      I have anonymized the SQL and database information, therefore the field name lengths do not match.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #18

      @snubben
      As @Christian-Ehrlicher says.

      Given that you say

      QSqlQueryModel* model = new QSqlQueryModel(this);
      model->setQuery("SELECT field1, field2, prodCount, field3, prod_date  FROM products WHERE 1 = 1 ORDER BY prod_date DESC;");
      

      does work I am not sure where your actual example differs significantly from this. Try it now the way you do it as

      QSqlQueryModel* model = new QSqlQueryModel(this);
      QSqlQuery query(db);
      query.prepare("SELECT field1, field2, prodCount, field3, prod_date  FROM products WHERE 1 = 1 ORDER BY prod_date DESC;");
      model->setQuery(query);
      

      I have not included your std::move() not your query.exec();. Only add those in if it does not work as is. See where you get to. If you get that working I'm sure you can then start introducing parameter bindings.

      At some point you ought get rid of that QSqlDatabase db class member variable. Per the docs you should be using the static QSqlDatabase QSqlDatabase::database() for that instead. I don't know whether it might be relevant here, probably not but you never know.

      1 Reply Last reply
      0
      • snubbenS Offline
        snubbenS Offline
        snubben
        wrote on last edited by snubben
        #19

        It works!

        QSqlTableModel* model = new QSqlTableModel(this);
        

        Using this it works. I must read more about QSqlQueryModel and why it didn't work in this situation.
        Thank you all!

        JonBJ 1 Reply Last reply
        0
        • snubbenS snubben has marked this topic as solved on
        • snubbenS snubben

          It works!

          QSqlTableModel* model = new QSqlTableModel(this);
          

          Using this it works. I must read more about QSqlQueryModel and why it didn't work in this situation.
          Thank you all!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #20

          @snubben said in tableView->setModel(model); causes SIGSEGV.:

          Using this it works.

          @Christian-Ehrlicher
          OP reports that QSqlTableModel works for his QTableView but not QSqlQueryModel. I wonder why? QTV only requires a QAbstractItemModel, QSqlTableModel inherits from QSqlQueryModel, why should the latter not work?

          The model for QTV must not be forward-only. Is QSqlQueryModel perhaps automatically forward-only, is that an issue?

          UPDATE
          Non-issue, see further posts below.

          1 Reply Last reply
          1
          • snubbenS Offline
            snubbenS Offline
            snubben
            wrote on last edited by
            #21

            This works too!

            QSqlQueryModel* model = new QSqlQueryModel(this);
            

            I feel like a complete moron. I had to check git and in an other function where I get strings for a QComboBox I had

                if (!QSqlDatabase::database().open()) {
                    qCritical() << "Database connection failed:" << QSqlDatabase::database().lastError().text();
                    //emit errorOccurred(db.lastError().text());
                    return result;
                }
            

            which should have of course been

            if (!QSqlDatabase::database().isOpen()) {
            

            and this I changed without even reflecting upon it.
            A lot I need to learn to properly find the erring location with Qt Creator. Once again, thanks guys & girls!

            JonBJ 1 Reply Last reply
            0
            • snubbenS snubben

              This works too!

              QSqlQueryModel* model = new QSqlQueryModel(this);
              

              I feel like a complete moron. I had to check git and in an other function where I get strings for a QComboBox I had

                  if (!QSqlDatabase::database().open()) {
                      qCritical() << "Database connection failed:" << QSqlDatabase::database().lastError().text();
                      //emit errorOccurred(db.lastError().text());
                      return result;
                  }
              

              which should have of course been

              if (!QSqlDatabase::database().isOpen()) {
              

              and this I changed without even reflecting upon it.
              A lot I need to learn to properly find the erring location with Qt Creator. Once again, thanks guys & girls!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #22

              @snubben said in tableView->setModel(model); causes SIGSEGV.:

              This works too!

              QSqlQueryModel* model = new QSqlQueryModel(this);

              Huh? That is what you started with right at the beginning:

              QSqlQueryModel* model = new QSqlQueryModel(this);

              That's from your first post. So if you're not saying you had to change from a QSqlQueryModel to a QSqlTableModel per your previous post --- and like I write to @Christian-Ehrlicher I don;t see why you should have to --- then I don't understand what you think you changed to make it work.

              snubbenS 1 Reply Last reply
              0
              • JonBJ JonB

                @snubben said in tableView->setModel(model); causes SIGSEGV.:

                This works too!

                QSqlQueryModel* model = new QSqlQueryModel(this);

                Huh? That is what you started with right at the beginning:

                QSqlQueryModel* model = new QSqlQueryModel(this);

                That's from your first post. So if you're not saying you had to change from a QSqlQueryModel to a QSqlTableModel per your previous post --- and like I write to @Christian-Ehrlicher I don;t see why you should have to --- then I don't understand what you think you changed to make it work.

                snubbenS Offline
                snubbenS Offline
                snubben
                wrote on last edited by
                #23

                @JonB said in tableView->setModel(model); causes SIGSEGV.:

                @snubben said in tableView->setModel(model); causes SIGSEGV.:

                This works too!

                QSqlQueryModel* model = new QSqlQueryModel(this);

                Huh? That is what you started with right at the beginning:

                QSqlQueryModel* model = new QSqlQueryModel(this);

                That's from your first post. So if you're not saying you had to change from a QSqlQueryModel to a QSqlTableModel per your previous post --- and like I write to @Christian-Ehrlicher I don;t see why you should have to --- then I don't understand what you think you changed to make it work.

                In the previous post I explained that I erroneously called QSqlDatabase::database().open() twice!

                JonBJ Pl45m4P 2 Replies Last reply
                0
                • snubbenS snubben

                  @JonB said in tableView->setModel(model); causes SIGSEGV.:

                  @snubben said in tableView->setModel(model); causes SIGSEGV.:

                  This works too!

                  QSqlQueryModel* model = new QSqlQueryModel(this);

                  Huh? That is what you started with right at the beginning:

                  QSqlQueryModel* model = new QSqlQueryModel(this);

                  That's from your first post. So if you're not saying you had to change from a QSqlQueryModel to a QSqlTableModel per your previous post --- and like I write to @Christian-Ehrlicher I don;t see why you should have to --- then I don't understand what you think you changed to make it work.

                  In the previous post I explained that I erroneously called QSqlDatabase::database().open() twice!

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #24

                  @snubben
                  OK. Hopefully the moral for you is this is why we (usually) insist on people pasting a complete, minimal example of their code before spending a lot of time on fragments.

                  snubbenS 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @snubben
                    OK. Hopefully the moral for you is this is why we (usually) insist on people pasting a complete, minimal example of their code before spending a lot of time on fragments.

                    snubbenS Offline
                    snubbenS Offline
                    snubben
                    wrote on last edited by
                    #25

                    @JonB said in tableView->setModel(model); causes SIGSEGV.:

                    @snubben
                    OK. Hopefully the moral for you is this is why we (usually) insist on people pasting a complete, minimal example of their code before spending a lot of time on fragments.

                    OK, I will do that the next time, thanks!

                    1 Reply Last reply
                    1
                    • snubbenS snubben

                      @JonB said in tableView->setModel(model); causes SIGSEGV.:

                      @snubben said in tableView->setModel(model); causes SIGSEGV.:

                      This works too!

                      QSqlQueryModel* model = new QSqlQueryModel(this);

                      Huh? That is what you started with right at the beginning:

                      QSqlQueryModel* model = new QSqlQueryModel(this);

                      That's from your first post. So if you're not saying you had to change from a QSqlQueryModel to a QSqlTableModel per your previous post --- and like I write to @Christian-Ehrlicher I don;t see why you should have to --- then I don't understand what you think you changed to make it work.

                      In the previous post I explained that I erroneously called QSqlDatabase::database().open() twice!

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by
                      #26

                      @snubben said in tableView->setModel(model); causes SIGSEGV.:

                      In the previous post I explained that I erroneously called QSqlDatabase::database().open() twice!

                      That's why you should use QSqlDatabase::database() and not a DB member... it's less easier to make mistakes there, since database() returns the currently opened connection.

                      @Pl45m4 said in tableView->setModel(model); causes SIGSEGV.:

                      Also:

                      if (!db.isOpen()) {
                      

                      This looks like you store your db connection as private member in DatabaseHandler.
                      Don't do that, it might cause undefined behavior. Better use the static QSqlDatabase::database() whenever it's needed.

                      I said it here :)

                      @JonB said in tableView->setModel(model); causes SIGSEGV.:

                      At some point you ought get rid of that QSqlDatabase db class member variable. Per the docs you should be using the static QSqlDatabase QSqlDatabase::database() for that instead. I don't know whether it might be relevant here, probably not but you never know.

                      And @JonB said it here again :)


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      1
                      • snubbenS Offline
                        snubbenS Offline
                        snubben
                        wrote on last edited by
                        #27

                        Point taken! I will remove it. Thanks!

                        1 Reply Last reply
                        1

                        • Login

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