Results 1 to 2 of 2

Thread: tableview: it does not want to appear on my screen

  1. #1
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tableview: it does not want to appear on my screen

    I am trying to work with the SQL example here
    but instead of the tableviews, two blank windows appear!!
    I cannot find the error!


    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3.  
    4. //#include "../connection.h"
    5.  
    6. void initializeModel(QSqlTableModel *model)
    7. {
    8. model->setTable("person");
    9. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    10. model->select();
    11.  
    12. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    13. model->setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
    14. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
    15. }
    16.  
    17. QTableView *createView(const QString &title, QSqlTableModel *model)
    18. {
    19. QTableView *view = new QTableView;
    20. view->setModel(model);
    21. view->setWindowTitle(title);
    22. return view;
    23. }
    24.  
    25. int main(int argc, char *argv[])
    26. {
    27. QApplication app(argc, argv);
    28. // if (!createConnection())
    29. // return 1;
    30.  
    31.  
    32. initializeModel(&model);
    33.  
    34. QTableView *view1 = createView(QObject::tr("Table Model (View 1)"), &model);
    35. QTableView *view2 = createView(QObject::tr("Table Model (View 2)"), &model);
    36.  
    37. view1->show();
    38. view2->move(view1->x() + view1->width() + 20, view1->y());
    39. view2->show();
    40.  
    41. return app.exec();
    42. }
    To copy to clipboard, switch view to plain text mode 


    Added after 20 minutes:


    that was simply because I had ignored the "connection.h" file. I thought that if the tableview has no data, it will simply show empty cells.

    Qt Code:
    1. #ifndef CONNECTION_H
    2. #define CONNECTION_H
    3.  
    4. #include <QSqlQuery>
    5. #include <QMessageBox>
    6.  
    7.  
    8. static bool createConnection()
    9. {
    10. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    11. db.setDatabaseName(":memory:");
    12. if (!db.open()) {
    13. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    14. qApp->tr("Unable to establish a database connection.\n"
    15. "This example needs SQLite support. Please read "
    16. "the Qt SQL driver documentation for information how "
    17. "to build it.\n\n"
    18. "Click Cancel to exit."), QMessageBox::Cancel);
    19. return false;
    20. }
    21.  
    22. QSqlQuery query;
    23. query.exec("create table person (id int primary key, "
    24. "firstname varchar(20), lastname varchar(20))");
    25. query.exec("insert into person values(101, 'Danny', 'Young')");
    26. query.exec("insert into person values(102, 'Christine', 'Holand')");
    27. query.exec("insert into person values(103, 'Lars', 'Gordon')");
    28. query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
    29. query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
    30.  
    31. query.exec("create table offices (id int primary key,"
    32. "imagefile int,"
    33. "location varchar(20),"
    34. "country varchar(20),"
    35. "description varchar(100))");
    36. query.exec("insert into offices "
    37. "values(0, 0, 'Oslo', 'Norway',"
    38. "'Oslo is home to more than 500 000 citizens and has a "
    39. "lot to offer.It has been called \"The city with the big "
    40. "heart\" and this is a nickname we are happy to live up to.')");
    41. query.exec("insert into offices "
    42. "values(1, 1, 'Brisbane', 'Australia',"
    43. "'Brisbane is the capital of Queensland, the Sunshine State, "
    44. "where it is beautiful one day, perfect the next. "
    45. "Brisbane is Australia''s 3rd largest city, being home "
    46. "to almost 2 million people.')");
    47. query.exec("insert into offices "
    48. "values(2, 2, 'Redwood City', 'US',"
    49. "'You find Redwood City in the heart of the Bay Area "
    50. "just north of Silicon Valley. The largest nearby city is "
    51. "San Jose which is the third largest city in California "
    52. "and the 10th largest in the US.')");
    53. query.exec("insert into offices "
    54. "values(3, 3, 'Berlin', 'Germany',"
    55. "'Berlin, the capital of Germany is dynamic, cosmopolitan "
    56. "and creative, allowing for every kind of lifestyle. "
    57. "East meets West in the metropolis at the heart of a "
    58. "changing Europe.')");
    59. query.exec("insert into offices "
    60. "values(4, 4, 'Munich', 'Germany',"
    61. "'Several technology companies are represented in Munich, "
    62. "and the city is often called the \"Bavarian Silicon Valley\". "
    63. "The exciting city is also filled with culture, "
    64. "art and music. ')");
    65. query.exec("insert into offices "
    66. "values(5, 5, 'Beijing', 'China',"
    67. "'Beijing as a capital city has more than 3000 years of "
    68. "history. Today the city counts 12 million citizens, and "
    69. "is the political, economic and cultural centre of China.')");
    70.  
    71. query.exec("create table images (locationid int, file varchar(20))");
    72. query.exec("insert into images values(0, 'images/oslo.png')");
    73. query.exec("insert into images values(1, 'images/brisbane.png')");
    74. query.exec("insert into images values(2, 'images/redwood.png')");
    75. query.exec("insert into images values(3, 'images/berlin.png')");
    76. query.exec("insert into images values(4, 'images/munich.png')");
    77. query.exec("insert into images values(5, 'images/beijing.png')");
    78.  
    79. return true;
    80. }
    81. #endif // CONNECTION_H
    To copy to clipboard, switch view to plain text mode 


    Added after 1:


    I don't really understand the "connection.h" function!
    I have created a simple model but it does not show up because it has no data!


    Qt Code:
    1. tm->setEditStrategy(QSqlTableModel::OnFieldChange);
    2. tm->setTable("vorgang");
    3. tm->select();
    4. tm->setHeaderData(0, Qt::Horizontal, tr("id"));
    5. tm->setHeaderData(1, Qt::Horizontal, tr("Start"));
    6. tm->setHeaderData(2, Qt::Horizontal, tr("End"));
    7.  
    8. if (!tm->select()) {
    9. qDebug() << "select nok";
    10. }
    11.  
    12.  
    13. //set up the view
    14. QTableView* tv = new QTableView(this);
    15. tv->setModel(tm);
    16. tv->resizeColumnsToContents();
    17. tv->setWindowTitle("pame");
    To copy to clipboard, switch view to plain text mode 

    How can i do this simple thing:

    I want to have blank cells, that the user will fill in and update the database?
    Last edited by fatecasino; 24th February 2011 at 17:03.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: tableview: it does not want to appear on my screen

    The connection.h file from the examples creates an in-memory Sqlite database with a couple of tables and few rows of data. Not much to understand.

    The QTableView displays the rows that are in the model. If there are no rows in the model then there are no rows in the view. You should still have the column headings. You need to look at QSqlTableModel::insertRows() to create new rows in the model.

Similar Threads

  1. Replies: 3
    Last Post: 25th January 2011, 14:36
  2. Replies: 0
    Last Post: 19th January 2011, 04:29
  3. application screen as screen saver
    By wizarda in forum Qt Programming
    Replies: 0
    Last Post: 6th January 2011, 20:15
  4. Replies: 2
    Last Post: 11th June 2010, 08:23
  5. tableview
    By GuL in forum Newbie
    Replies: 1
    Last Post: 26th August 2008, 18:18

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.