QTableView shows nothing
-
wrote on 11 Feb 2017, 16:34 last edited by
-
Where is the piece of code ? Is it in main function or somewhere else ?
Could be because query object is stack object. Try allocating in Heap. -
wrote on 11 Feb 2017, 16:58 last edited by
Code is in function:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this);
Heap? Do you mean like pointer?
-
Where is the piece of code ? Is it in main function or somewhere else ?
Could be because query object is stack object. Try allocating in Heap.wrote on 11 Feb 2017, 19:40 last edited by@dheerendra said in QTableView shows nothing:
Where is the piece of code ? Is it in main function or somewhere else ?
Could be because query object is stack object. Try allocating in Heap.Thanks problem solved :)
How to know, when allocate in Heap and when as stack object?
-
@dheerendra said in QTableView shows nothing:
Where is the piece of code ? Is it in main function or somewhere else ?
Could be because query object is stack object. Try allocating in Heap.Thanks problem solved :)
How to know, when allocate in Heap and when as stack object?
void mainwindow::some func() {
MyClass C; // stack
MyClass *D = new MyClass; // heap} // here C will be deleted. ( runs out of scope) BUT D lives until deleted by programmer.
So when using new, you are using heap.
-
Hi,
To add to my fellow, you are also not checking whether the database is opened correctly.
-
Hi,
To add to my fellow, you are also not checking whether the database is opened correctly.
wrote on 11 Feb 2017, 20:51 last edited by@SGaist said in QTableView shows nothing:
Hi,
To add to my fellow, you are also not checking whether the database is opened correctly.
I am checking
if(db.open()) {
but i didnt include it in post :) but thank you anyway :)
@mrjj said in QTableView shows nothing:
void mainwindow::some func() {
MyClass C; // stack
MyClass *D = new MyClass; // heap} // here C will be deleted. ( runs out of scope) BUT D lives until deleted by programmer.
So when using new, you are using heap.
yes i know, but i dont know when use pointer and when stack
-
@SGaist said in QTableView shows nothing:
Hi,
To add to my fellow, you are also not checking whether the database is opened correctly.
I am checking
if(db.open()) {
but i didnt include it in post :) but thank you anyway :)
@mrjj said in QTableView shows nothing:
void mainwindow::some func() {
MyClass C; // stack
MyClass *D = new MyClass; // heap} // here C will be deleted. ( runs out of scope) BUT D lives until deleted by programmer.
So when using new, you are using heap.
yes i know, but i dont know when use pointer and when stack
@t0msk said in QTableView shows nothing:
i dont know when use pointer and when stack
Well use stack when possible.
If the variable is to be used outside a function/live longer than the function where its declared , consider using new or adding it as a member variable. -
@t0msk said in QTableView shows nothing:
i dont know when use pointer and when stack
Well use stack when possible.
If the variable is to be used outside a function/live longer than the function where its declared , consider using new or adding it as a member variable.wrote on 11 Feb 2017, 21:16 last edited by@mrjj said in QTableView shows nothing:
@t0msk said in QTableView shows nothing:
i dont know when use pointer and when stack
Well use stack when possible.
If the variable is to be used outside a function/live longer than the function where its declared , consider using new or adding it as a member variable.yea, so I have to use "new" always, because everything is function :D ,
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
Is function too (my problem) :D
-
@mrjj said in QTableView shows nothing:
@t0msk said in QTableView shows nothing:
i dont know when use pointer and when stack
Well use stack when possible.
If the variable is to be used outside a function/live longer than the function where its declared , consider using new or adding it as a member variable.yea, so I have to use "new" always, because everything is function :D ,
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
Is function too (my problem) :D
@t0msk
Yes
You do
QSqlQueryModel query;
..
ui->tableView->setModel(&query);Here you use & to take adress of it.
So as soon as function ends query is deleted and view is empty.
You could move
QSqlQueryModel query; to .h file.
as member in mainwindow -
@t0msk
Yes
You do
QSqlQueryModel query;
..
ui->tableView->setModel(&query);Here you use & to take adress of it.
So as soon as function ends query is deleted and view is empty.
You could move
QSqlQueryModel query; to .h file.
as member in mainwindowwrote on 11 Feb 2017, 21:28 last edited by@mrjj said in QTableView shows nothing:
@t0msk
Yes
You do
QSqlQueryModel query;
..
ui->tableView->setModel(&query);Here you use & to take adress of it.
So as soon as function ends query is deleted and view is empty.
You could move
QSqlQueryModel query; to .h file.
as member in mainwindowAh, thank you for explanation :)
2/11