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. QTableView shows nothing

QTableView shows nothing

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 2.5k Views
  • 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.
  • T Offline
    T Offline
    t0msk
    wrote on 11 Feb 2017, 16:34 last edited by
    #1

    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? :/

    Student who loves C/C++

    1 Reply Last reply
    0
    • D Online
      D Online
      dheerendra
      Qt Champions 2022
      wrote on 11 Feb 2017, 16:38 last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      T 1 Reply Last reply 11 Feb 2017, 19:40
      6
      • T Offline
        T Offline
        t0msk
        wrote on 11 Feb 2017, 16:58 last edited by
        #3

        Code is in function:

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

        Heap? Do you mean like pointer?

        Student who loves C/C++

        1 Reply Last reply
        0
        • D dheerendra
          11 Feb 2017, 16:38

          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.

          T Offline
          T Offline
          t0msk
          wrote on 11 Feb 2017, 19:40 last edited by
          #4

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

          Student who loves C/C++

          M 1 Reply Last reply 11 Feb 2017, 20:10
          0
          • T t0msk
            11 Feb 2017, 19:40

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 11 Feb 2017, 20:10 last edited by
            #5

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

            1 Reply Last reply
            2
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 11 Feb 2017, 20:19 last edited by
              #6

              Hi,

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

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

              T 1 Reply Last reply 11 Feb 2017, 20:51
              0
              • S SGaist
                11 Feb 2017, 20:19

                Hi,

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

                T Offline
                T Offline
                t0msk
                wrote on 11 Feb 2017, 20:51 last edited by
                #7

                @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

                Student who loves C/C++

                M 1 Reply Last reply 11 Feb 2017, 20:59
                0
                • T t0msk
                  11 Feb 2017, 20:51

                  @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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 11 Feb 2017, 20:59 last edited by
                  #8

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

                  T 1 Reply Last reply 11 Feb 2017, 21:16
                  2
                  • M mrjj
                    11 Feb 2017, 20:59

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

                    T Offline
                    T Offline
                    t0msk
                    wrote on 11 Feb 2017, 21:16 last edited by
                    #9

                    @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

                    Student who loves C/C++

                    M 1 Reply Last reply 11 Feb 2017, 21:21
                    0
                    • T t0msk
                      11 Feb 2017, 21:16

                      @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

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 11 Feb 2017, 21:21 last edited by
                      #10

                      @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

                      T 1 Reply Last reply 11 Feb 2017, 21:28
                      1
                      • M mrjj
                        11 Feb 2017, 21:21

                        @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

                        T Offline
                        T Offline
                        t0msk
                        wrote on 11 Feb 2017, 21:28 last edited by
                        #11

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

                        Student who loves C/C++

                        1 Reply Last reply
                        0

                        2/11

                        11 Feb 2017, 16:38

                        topic:navigator.unread, 9
                        • Login

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