//main.cpp
#include <QtGui>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlTableModel>
{
public:
explicit MyView
(QWidget * parent
= 0) , mGroup(0)
, mModel(0)
, mColumn(0)
{
setLayout(mLayout);
updateView();
}
{ mModel = model, updateView(); }
void setColumn(int column)
{ mColumn = column, updateView(); }
private:
int mColumn;
void updateView(void)
{
if(mGroup)
delete mGroup;
if(mGroup->layout())
delete mGroup->layout();
if(mModel != 0)
for(int i = 0; i < mModel->rowCount(); i++)
layout
->addWidget
(new QRadioButton(mModel
->index
(i, mColumn
).
data().
toString()));
mGroup->setLayout(layout);
mLayout->addWidget(mGroup);
update();
}
};
int main(int argc, char *argv[])
{
// Create Sample Databse (can use MYSQL, instead of Sqlite)
database.setDatabaseName("Test.sqlite");
// open database
if(database.open())
{
// if table does not exists
if(database.tables().count() == 0)
{
// Create table
QString q
("CREATE TABLE Employee ( Name varchar(255), Salary int );");
query.exec();
database.commit();
}
}
// Create model for the sql table
model->setTable("Employee");
model->select();
// if not rows exist in table
if(model->rowCount() == 0)
{
// Add sample rows/records
model->insertRows(0, 10);
for(int i = 0; i < model->rowCount(); i++)
{
model
->setData
(model
->index
(i,
0),
QString("Employee-%1").
arg(i
+ 1));
model->setData(model->index(i, 1), i*555);
}
model->submit();
}
// Create a Table View
v1.setModel(model), v1.setWindowTitle("QTableView"), v1.show();
// Create a MyView 1
MyView v2;
v2.setModel(model), v2.setWindowTitle("MyView 2"), v2.setColumn(0), v2.show();
// Create a MyView 2
MyView v3;
v3.setModel(model), v3.setWindowTitle("MyView 3"), v3.setColumn(1), v3.show();
return app.exec();
}
//main.cpp
#include <QtGui>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlTableModel>
class MyView : public QWidget
{
public:
explicit MyView(QWidget * parent = 0)
: QWidget(parent)
, mLayout(new QGridLayout(this))
, mGroup(0)
, mModel(0)
, mColumn(0)
{
setLayout(mLayout);
updateView();
}
void setModel(QAbstractTableModel *model)
{ mModel = model, updateView(); }
void setColumn(int column)
{ mColumn = column, updateView(); }
private:
QGridLayout * mLayout;
QGroupBox * mGroup;
QAbstractTableModel * mModel;
int mColumn;
void updateView(void)
{
if(mGroup)
delete mGroup;
mGroup = new QGroupBox;
if(mGroup->layout())
delete mGroup->layout();
QVBoxLayout * layout = new QVBoxLayout;
if(mModel != 0)
for(int i = 0; i < mModel->rowCount(); i++)
layout->addWidget(new QRadioButton(mModel->index(i, mColumn).data().toString()));
mGroup->setLayout(layout);
mLayout->addWidget(mGroup);
update();
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// Create Sample Databse (can use MYSQL, instead of Sqlite)
QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE", "Test-sqlite");
database.setDatabaseName("Test.sqlite");
// open database
if(database.open())
{
// if table does not exists
if(database.tables().count() == 0)
{
// Create table
QString q("CREATE TABLE Employee ( Name varchar(255), Salary int );");
QSqlQuery query(q, database);
query.exec();
database.commit();
}
}
// Create model for the sql table
QSqlTableModel * model = new QSqlTableModel(&app, database);
model->setTable("Employee");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
// if not rows exist in table
if(model->rowCount() == 0)
{
// Add sample rows/records
model->insertRows(0, 10);
for(int i = 0; i < model->rowCount(); i++)
{
model->setData(model->index(i, 0), QString("Employee-%1").arg(i + 1));
model->setData(model->index(i, 1), i*555);
}
model->submit();
}
// Create a Table View
QTableView v1;
v1.setModel(model), v1.setWindowTitle("QTableView"), v1.show();
// Create a MyView 1
MyView v2;
v2.setModel(model), v2.setWindowTitle("MyView 2"), v2.setColumn(0), v2.show();
// Create a MyView 2
MyView v3;
v3.setModel(model), v3.setWindowTitle("MyView 3"), v3.setColumn(1), v3.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks