PDA

View Full Version : help in tableview



bala
12th November 2007, 15:09
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"

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();

}

database.cc

#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;

}




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();
QSqlTableModel *table;
QTableView *view ;
bool create_connection(void);
QTableView *initializeModel(QSqlTableModel *model);

};

#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

thanks in advance.

DeepDiver
12th November 2007, 15:17
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

bala
12th November 2007, 16:09
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:)

DeepDiver
12th November 2007, 16:46
Definition of instance: http://en.wikipedia.org/wiki/Instantiation_%28computer_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-general-programming-9/t-what-are-your-favourite-c-books-29.html