QTableView shows nothing



  • Hello, this is my code:

        db.open();
    
        QSqlQueryModel query;
        query.setQuery("SELECT * FROM pve.010_store");
    
        ui->tableView->setModel(&query);
    

    But, when I launch program it shows nothing..

    Screenshot:

    Screenshot

    Where is a problem? :/


  • Qt Champions 2017

    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.



  • Code is in function:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    

    Heap? Do you mean like pointer?



  • @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?


  • Qt Champions 2017

    @t0msk

    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.


  • Lifetime Qt Champion

    Hi,

    To add to my fellow, you are also not checking whether the database is opened correctly.



  • @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:

    @t0msk

    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


  • Qt Champions 2017

    @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.



  • @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


  • Qt Champions 2017

    @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



  • @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 mainwindow

    Ah, thank you for explanation :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.