Results 1 to 3 of 3

Thread: How to show connected sqlite database in Tableview

  1. #1
    Join Date
    May 2014
    Posts
    2
    Thanks
    1

    Default How to show connected sqlite database in Tableview

    So hello,I have an existing database in my project.I connected my database to my prog.I can't understand how to show my database in Tableview.Please look at my project, what I'm doing wrong?

    qt.jpg

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
    11. mydb.setDatabaseName("C:/GLOSSARIY/Ядерная медицина.sqlite");
    12. mydb.open();
    13.  
    14. if (!mydb.open())
    15. ui->label->setText("Failed to open your database");
    16. else
    17. ui->label->setText("Connected succesfully...");
    18.  
    19. }
    20.  
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    26. void MainWindow::on_pushButton_clicked()
    27.  
    28. {
    29. QSqlQuery query;
    30. query.exec("SELECT * FROM Глоссарий по радиобиологии");
    31. model->setQuery(query);
    32. ui->tableView->setModel(model);
    33. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtSql>
    6. #include <QtDebug>
    7. #include <QFileInfo>
    8.  
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. //public:
    19. //QSqlDatabase mydb;
    20.  
    21. public:
    22. explicit MainWindow(QWidget *parent = 0);
    23. ~MainWindow();
    24.  
    25. private slots:
    26. void on_pushButton_clicked();
    27.  
    28. private:
    29. Ui::MainWindow *ui;
    30.  
    31. };
    32.  
    33. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to show connected sqlite database in Tableview

    I think that SQL query is NOT executed (QSqlQuery::exec returns false) because You have spaces in name of table.
    It should looks like :
    Qt Code:
    1. query.exec("SELECT * FROM \"Глоссарий по радиобиологии\"");
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lesiok for this useful post:

    Clandeik (18th May 2014)

  4. #3
    Join Date
    May 2014
    Posts
    2
    Thanks
    1

    Default Re: How to show connected sqlite database in Tableview

    Thank you Lesiok!....interesting, how I can stretch the field in the table to see all the word in it, ....I tried use
    Qt Code:
    1. ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    To copy to clipboard, switch view to plain text mode 
    but it does not show all the text!
    solved this problem never mind.
    Last edited by Clandeik; 18th May 2014 at 18:14.

Similar Threads

  1. Replies: 3
    Last Post: 26th February 2014, 04:56
  2. QSqlQueryModel, tableView, SQLite problem
    By ohmyqt in forum Newbie
    Replies: 0
    Last Post: 28th September 2013, 09:07
  3. Replies: 7
    Last Post: 30th June 2011, 10:16
  4. Replies: 6
    Last Post: 2nd May 2011, 09:43
  5. Replies: 3
    Last Post: 12th July 2010, 13:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.