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
#include <QtGui>
#include "database.h"
#include "ui_tableview.h"
{
public:
mainwindow()
{
setupUi(this);
database db;
db.create_connection();
tableView = db.initializeModel(model);
}
};
int main(int argc,char *argv[])
{
mainwindow *window = new mainwindow;
window->show();
app.exec();
}
#include <QtGui>
#include "database.h"
#include "ui_tableview.h"
class mainwindow :public QMainWindow ,Ui::MainWindow
{
public:
QSqlTableModel *model;
mainwindow()
{
setupUi(this);
model = new QSqlTableModel;
database db;
db.create_connection();
tableView = db.initializeModel(model);
}
};
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
mainwindow *window = new mainwindow;
window->show();
app.exec();
}
To copy to clipboard, switch view to plain text mode
database.cc
#include "database.h"
database::database()
{
}
bool database::create_connection()
{
db.setHostName("localhost");
db.setDatabaseName("dicom");
db.setUserName("root");
if (!db.open()) {
QMessageBox::critical(0, qApp
->tr
("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
return false;
}
std::cout<<"db connected"<<std::endl;
return true;
}
{
model->setTable("report");
model->select();
model
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("ID"));
model
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("First name"));
model
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("Last name"));
view->setModel( model );
cout<<"i am in initializeModel"<<endl;
//QSqlTableView *view = new QTableView;
//view->setModel( model );
return view;
}
#include "database.h"
database::database()
{
table = new QSqlTableModel;
}
bool database::create_connection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("dicom");
db.setUserName("root");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
std::cout<<"db connected"<<std::endl;
return true;
}
QTableView *database::initializeModel(QSqlTableModel *model)
{
view = new QTableView;
model->setTable("report");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
view->setModel( model );
cout<<"i am in initializeModel"<<endl;
//QSqlTableView *view = new QTableView;
//view->setModel( model );
return view;
}
To copy to clipboard, switch view to plain text mode
database.h
#ifndef DATABASE_H
#define DATABASE_H
#include<QtGui>
#include<iostream>
#include<QtSql>
#include<QSqlDatabase>
#include "/usr/include/mysql/mysql.h"
#include<QCoreApplication>
//#include <QSqlTableModel>
#include <iostream>
using namespace std;
#include <QtGui>
#include <QtSql>
//#include "connection.h"
class database
{
public:
database();
bool create_connection(void);
};
#endif
#ifndef DATABASE_H
#define DATABASE_H
#include<QtGui>
#include<iostream>
#include<QtSql>
#include<QSqlDatabase>
#include "/usr/include/mysql/mysql.h"
#include<QCoreApplication>
//#include <QSqlTableModel>
#include <iostream>
using namespace std;
#include <QtGui>
#include <QtSql>
//#include "connection.h"
class database
{
public:
database();
QSqlTableModel *table;
QTableView *view ;
bool create_connection(void);
QTableView *initializeModel(QSqlTableModel *model);
};
#endif
To copy to clipboard, switch view to plain text mode
ui_tableview.h
/********************************************************************************
** Form generated from reading ui file 'tableview.ui'
**
** Created: Mon Nov 12 19:03:58 2007
** by: Qt User Interface Compiler version 4.2.3
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
********************************************************************************/
#ifndef UI_TABLEVIEW_H
#define UI_TABLEVIEW_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QTableView>
#include <QtGui/QWidget>
class Ui_MainWindow
{
public:
{
MainWindow
->setObjectName
(QString::fromUtf8("MainWindow"));
centralwidget
= new QWidget(MainWindow
);
centralwidget
->setObjectName
(QString::fromUtf8("centralwidget"));
tableView
->setObjectName
(QString::fromUtf8("tableView"));
tableView
->setGeometry
(QRect(30,
10,
411,
281));
MainWindow->setCentralWidget(centralwidget);
menubar
->setObjectName
(QString::fromUtf8("menubar"));
menubar
->setGeometry
(QRect(0,
0,
508,
25));
MainWindow->setMenuBar(menubar);
statusbar
->setObjectName
(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
retranslateUi(MainWindow);
size = size.expandedTo(MainWindow->minimumSizeHint());
MainWindow->resize(size);
} // setupUi
{
Q_UNUSED(MainWindow);
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
#endif // UI_TABLEVIEW_H
/********************************************************************************
** Form generated from reading ui file 'tableview.ui'
**
** Created: Mon Nov 12 19:03:58 2007
** by: Qt User Interface Compiler version 4.2.3
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
********************************************************************************/
#ifndef UI_TABLEVIEW_H
#define UI_TABLEVIEW_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QTableView>
#include <QtGui/QWidget>
class Ui_MainWindow
{
public:
QWidget *centralwidget;
QTableView *tableView;
QMenuBar *menubar;
QStatusBar *statusbar;
void setupUi(QMainWindow *MainWindow)
{
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
tableView = new QTableView(centralwidget);
tableView->setObjectName(QString::fromUtf8("tableView"));
tableView->setGeometry(QRect(30, 10, 411, 281));
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 508, 25));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
retranslateUi(MainWindow);
QSize size(508, 338);
size = size.expandedTo(MainWindow->minimumSizeHint());
MainWindow->resize(size);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
Q_UNUSED(MainWindow);
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
#endif // UI_TABLEVIEW_H
To copy to clipboard, switch view to plain text mode
thanks in advance.
Bookmarks