Results 1 to 4 of 4

Thread: help in tableview

  1. #1
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy help in tableview

    Hi,
    I created tableview using designer and trying
    to get data from mysql table and displaying on it.
    The prigram is running without error.
    But nothing is displaying in the view.
    Please some one help me.
    the code looks like

    main.cc
    Qt Code:
    1. #include <QtGui>
    2. #include "database.h"
    3. #include "ui_tableview.h"
    4.  
    5. class mainwindow :public QMainWindow ,Ui::MainWindow
    6. {
    7. public:
    8. QSqlTableModel *model;
    9. mainwindow()
    10. {
    11. setupUi(this);
    12. model = new QSqlTableModel;
    13. database db;
    14. db.create_connection();
    15. tableView = db.initializeModel(model);
    16.  
    17. }
    18.  
    19.  
    20.  
    21. };
    22.  
    23. int main(int argc,char *argv[])
    24. {
    25. QApplication app(argc,argv);
    26. mainwindow *window = new mainwindow;
    27. window->show();
    28. app.exec();
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

    database.cc
    Qt Code:
    1. #include "database.h"
    2. database::database()
    3. {
    4. table = new QSqlTableModel;
    5.  
    6. }
    7.  
    8. bool database::create_connection()
    9. {
    10. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    11. db.setHostName("localhost");
    12. db.setDatabaseName("dicom");
    13. db.setUserName("root");
    14. if (!db.open()) {
    15. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    16. qApp->tr("Unable to establish a database connection.\n"
    17. "This example needs SQLite support. Please read "
    18. "the Qt SQL driver documentation for information how "
    19. "to build it.\n\n"
    20. "Click Cancel to exit."), QMessageBox::Cancel);
    21. return false;
    22. }
    23. std::cout<<"db connected"<<std::endl;
    24. return true;
    25. }
    26.  
    27. QTableView *database::initializeModel(QSqlTableModel *model)
    28. {
    29.  
    30. view = new QTableView;
    31. model->setTable("report");
    32. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    33. model->select();
    34.  
    35. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    36. model->setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
    37. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
    38. view->setModel( model );
    39. cout<<"i am in initializeModel"<<endl;
    40. //QSqlTableView *view = new QTableView;
    41. //view->setModel( model );
    42. return view;
    43.  
    44. }
    To copy to clipboard, switch view to plain text mode 

    database.h
    Qt Code:
    1. #ifndef DATABASE_H
    2. #define DATABASE_H
    3. #include<QtGui>
    4. #include<iostream>
    5. #include<QtSql>
    6. #include<QSqlDatabase>
    7. #include "/usr/include/mysql/mysql.h"
    8. #include<QCoreApplication>
    9. //#include <QSqlTableModel>
    10. #include <iostream>
    11. using namespace std;
    12. #include <QtGui>
    13. #include <QtSql>
    14.  
    15. //#include "connection.h"
    16.  
    17. class database
    18. {
    19. public:
    20. database();
    21. QTableView *view ;
    22. bool create_connection(void);
    23. QTableView *initializeModel(QSqlTableModel *model);
    24.  
    25. };
    26.  
    27. #endif
    To copy to clipboard, switch view to plain text mode 

    ui_tableview.h

    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading ui file 'tableview.ui'
    3. **
    4. ** Created: Mon Nov 12 19:03:58 2007
    5. ** by: Qt User Interface Compiler version 4.2.3
    6. **
    7. ** WARNING! All changes made in this file will be lost when recompiling ui file!
    8. ********************************************************************************/
    9.  
    10. #ifndef UI_TABLEVIEW_H
    11. #define UI_TABLEVIEW_H
    12.  
    13. #include <QtCore/QVariant>
    14. #include <QtGui/QAction>
    15. #include <QtGui/QApplication>
    16. #include <QtGui/QButtonGroup>
    17. #include <QtGui/QMainWindow>
    18. #include <QtGui/QMenuBar>
    19. #include <QtGui/QStatusBar>
    20. #include <QtGui/QTableView>
    21. #include <QtGui/QWidget>
    22.  
    23. class Ui_MainWindow
    24. {
    25. public:
    26. QWidget *centralwidget;
    27. QTableView *tableView;
    28. QMenuBar *menubar;
    29. QStatusBar *statusbar;
    30.  
    31. void setupUi(QMainWindow *MainWindow)
    32. {
    33. MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    34. centralwidget = new QWidget(MainWindow);
    35. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    36. tableView = new QTableView(centralwidget);
    37. tableView->setObjectName(QString::fromUtf8("tableView"));
    38. tableView->setGeometry(QRect(30, 10, 411, 281));
    39. MainWindow->setCentralWidget(centralwidget);
    40. menubar = new QMenuBar(MainWindow);
    41. menubar->setObjectName(QString::fromUtf8("menubar"));
    42. menubar->setGeometry(QRect(0, 0, 508, 25));
    43. MainWindow->setMenuBar(menubar);
    44. statusbar = new QStatusBar(MainWindow);
    45. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    46. MainWindow->setStatusBar(statusbar);
    47.  
    48. retranslateUi(MainWindow);
    49.  
    50. QSize size(508, 338);
    51. size = size.expandedTo(MainWindow->minimumSizeHint());
    52. MainWindow->resize(size);
    53.  
    54.  
    55. QMetaObject::connectSlotsByName(MainWindow);
    56. } // setupUi
    57.  
    58. void retranslateUi(QMainWindow *MainWindow)
    59. {
    60. MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    61. Q_UNUSED(MainWindow);
    62. } // retranslateUi
    63.  
    64. };
    65.  
    66. namespace Ui {
    67. class MainWindow: public Ui_MainWindow {};
    68. } // namespace Ui
    69.  
    70. #endif // UI_TABLEVIEW_H
    To copy to clipboard, switch view to plain text mode 

    thanks in advance.

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help in tableview

    What look a bit strange for me is that you have two instances of QTableView and two instances of QSqlTableModel.

    Most likely the returned instance of QTableView of the method QTableView *database::initializeModel(QSqlTableModel *model) is senceless.

    Use the instance in the ui-class instead.

    Good luck

  3. #3
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: help in tableview

    thanks for ur suggestion.
    please give me a sample code,
    as i am new to Qt and C++,
    i don't how to write for this instance.

    thanks a lot

  4. #4
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help in tableview

    Definition of instance: http://en.wikipedia.org/wiki/Instant...ter_science%29

    One thing shall be clear: this forum is here for teaching software development or programming. There a lot of good books out there:
    http://www.qtcentre.org/forum/f-gene...-books-29.html

Similar Threads

  1. Drag an item from QTreeWidget and drop in TableView
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2007, 11:42
  2. Replies: 15
    Last Post: 8th May 2007, 11:46
  3. tableview and layouts
    By zorro68 in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2007, 17:21
  4. TreeView, TableView
    By rbrand in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2006, 08:54
  5. Modified tableview
    By dkite in forum Qt Programming
    Replies: 2
    Last Post: 14th March 2006, 01:58

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.