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 and its model
Forum Updated to NodeBB v4.3 + New Features

QTableView and its model

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.8k Views 1 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.
  • S Offline
    S Offline
    SumRandomGuy
    wrote on last edited by
    #1

    OK, this is bugging me. If I do this:
    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    DirectorDB = QSqlDatabase::addDatabase("QSQLITE", "test");
    DirectorDB.setHostName("localhost");
    DirectorDB.setDatabaseName("DirectorConfig.db");
    DirectorDB.open();

    QSqlQueryModel model;
    model.setQuery("select * from profiles;", QSqlDatabase::database("test"));
    ui->tableView->setModel( &model );
    

    }@

    It does not populate the tableView. It DOES give me the top-left corner thing. If I alter the SQL to reference a non-existent table, I don't get the top-left corner thing so I know it's finding the DB and finding the table, it's just not populating the tableView.

    If I do this:
    @int main( int argc, char * * argv )
    {
    QApplication app( argc, argv );

        QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE", "test" );
        db.setDatabaseName( databaseName );
        db.open();
    
        QSqlQueryModel model;
        model.setQuery( "select * from profiles;", QSqlDatabase::database( "test" ) );
    
        QTableView view;
        view.setWindowTitle( "Test" );
        view.setModel( &model );
        view.show();
    
        return( app.exec() );
    

    }@

    It works fine. What is the difference between the two other than the fact that I've put the tableView into a properly developed form?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SumRandomGuy
      wrote on last edited by
      #2

      I have confirmed that the one I created in QT Creator does know about the SQL connection. I can change the query to "select * from prxxxxofiles;" (obviously broken) and it does not give me any kind of table inside the tableView (ie the top-left click button). If I use a valid table, I do, indeed, get that button up there.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alexisdm
        wrote on last edited by
        #3

        model is allocated locally by the constructor, so it is deleted at the end of that function.

        As you may need to access the model elsewhere, it should probably be added as a member of that class anyway.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SumRandomGuy
          wrote on last edited by
          #4

          Hah! Got it.

          Moved:
          QSqlQueryModel model;

          To the MainWindow definition. It was going out of scope when the init method was done.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SumRandomGuy
            wrote on last edited by
            #5

            Looks like alexisdm posted the solution while I was eating dinner having an epiphany. Thanks for the answer. That was exactly it.

            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