Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8560975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:23:54+00:00 2026-06-11T16:23:54+00:00

I am playing with Qt (I am a beginner), and trying to show a

  • 0

I am playing with Qt (I am a beginner), and trying to show a table’s data in a QTableView.
So far I have this:

ui->setupUi(this);

QSqlDatabase db;
db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test.db");

if (db.open())
    qDebug() << "success";
else
    qDebug() << "failed";
QSqlTableModel model;
model.setTable("names");
model.select();
qDebug() << model.rowCount();

ui->tableView->setModel(&model);

The problem is that, the model does get the data(2 rows), but the QTableView does now show it.

Any clues what is wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:23:55+00:00Added an answer on June 11, 2026 at 4:23 pm

    Looking at your code it could be your model object is going out of scope. Make these few changes, hopefully they will get your issue corrected.

    ui->setupUi( this );
    
    // Setup db
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("test.db");
    
    if( db.open() )
        qDebug() << "success";
    else
        qDebug() << "failed";
    
    // Initialize your model with the database.
    QSqlTableModel* model = new QSqlTableModel( this, db );
    
    // Let the QTableView take ownership of the model.
    ui->tableView->setModel( model );
    
    // Add the rest of your code here.
    model->setTable( "names" );
    model->select();
    qDebug() << model->rowCount();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have some useful beginner tutorials and code snippets for playing with basic
I'm a Java beginner and have been futzing around with various solutions to this
Playing a little with coffeescript and Rails 3.1.0.rc4. Have this code: yourMom = (location)
I'm a beginner and have just about managed to put together this website with
I'm a Haskell beginner and I've been playing around with point-free functions. I have
I'm playing around with beginner Haskell, and I wanted to write an average function.
I'm beginner in Android (and java), and I'm just playing around and want to
Playing with jquery for the first time, and I'm trying to get a simple
Beginner here, I have a simple question. In Android what would be the best
I'm using Visual Studio 2010, and have the tangibleT4EditorPlusModellingTools installed. I'm just playing around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.