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
QtWS25 Last Chance

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 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
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on 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
      6
      • T Offline
        T Offline
        t0msk
        wrote on 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
        • dheerendraD dheerendra

          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 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++

          mrjjM 1 Reply Last reply
          0
          • T t0msk

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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
              0
              • SGaistS SGaist

                Hi,

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

                T Offline
                T Offline
                t0msk
                wrote on 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++

                mrjjM 1 Reply Last reply
                0
                • T t0msk

                  @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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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
                  2
                  • mrjjM mrjj

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

                    mrjjM 1 Reply Last reply
                    0
                    • T t0msk

                      @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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 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
                      1
                      • mrjjM mrjj

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

                        • Login

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