#include <QtGui>
#include <QDebug>
Q_OBJECT
public:
setRowCount(2);
setColumnCount(2);
// Default population
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
it->setData(Qt::EditRole, 0);
setItem(r, c, it);
}
}
}
void setData(bool M[2][2]) {
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
Q_ASSERT(it);
int val = M[r][c]? 1: 0;
it->setData(Qt::EditRole, val);
}
}
}
void getData(bool M[2][2]) {
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
Q_ASSERT(it);
int val = it->data(Qt::EditRole).toInt();
M[r][c] = (val == 1)? true: false;
}
}
}
};
int main(int argc, char *argv[])
{
bool M[2][2] = { {false, true}, {true, false} };
MatrixWidget m;
m.setData(M);
m.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QDebug>
class MatrixWidget: public QTableWidget {
Q_OBJECT
public:
MatrixWidget(QWidget *p = 0): QTableWidget(p) {
setRowCount(2);
setColumnCount(2);
// Default population
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
QTableWidgetItem *it = new QTableWidgetItem;
it->setData(Qt::EditRole, 0);
setItem(r, c, it);
}
}
}
void setData(bool M[2][2]) {
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
QTableWidgetItem *it = item(r, c);
Q_ASSERT(it);
int val = M[r][c]? 1: 0;
it->setData(Qt::EditRole, val);
}
}
}
void getData(bool M[2][2]) {
for (int r = 0; r < 2; ++r) {
for (int c = 0; c < 2; ++c) {
QTableWidgetItem *it = item(r, c);
Q_ASSERT(it);
int val = it->data(Qt::EditRole).toInt();
M[r][c] = (val == 1)? true: false;
}
}
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
bool M[2][2] = { {false, true}, {true, false} };
MatrixWidget m;
m.setData(M);
m.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks