Commit 9643898b authored by Jordan P. Lefebvre's avatar Jordan P. Lefebvre
Browse files

Adding TableWidget example.

parent d810a53c
......@@ -2,16 +2,27 @@
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
IF(USE_QT4)
QT4_WRAP_CPP(QMOC_FILES
QT4_WRAP_CPP(QMOC_FILES1
radixtabwidget.hh
)
QT4_WRAP_CPP(QMOC_FILES2
radixtablewidget.hh
)
ELSE()
QT5_WRAP_CPP(QMOC_FILES
QT5_WRAP_CPP(QMOC_FILES1
radixtabwidget.hh
)
QT5_WRAP_CPP(QMOC_FILES2
radixtablewidget.hh
)
ENDIF()
TRIBITS_ADD_EXECUTABLE(radixtabwidget
NOEXEPREFIX
SOURCES radixtabwidget.cc ${QMOC_FILES}
SOURCES radixtabwidget.cc ${QMOC_FILES1}
)
TRIBITS_ADD_EXECUTABLE(radixtablewidget
NOEXEPREFIX
SOURCES radixtablewidget.cc ${QMOC_FILES2}
)
/*
* @file: mainwindow.cpp
* @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
*
* Created on June 24, 2018, 10:52 AM
*/
#include "radixtablewidget.hh"
#include <QApplication>
#include <QGridLayout>
#include <QLabel>
#include <limits>
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
TableModel::TableModel(QList<Contact> contacts, QObject *parent)
: QAbstractTableModel(parent)
, contacts(contacts)
{
}
int TableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return contacts.size();
}
int TableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
}
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
if (index.row() >= contacts.size() || index.row() < 0) return QVariant();
if (role == Qt::DisplayRole)
{
const auto &contact = contacts.at(index.row());
if (index.column() == 0)
return contact.name;
else if (index.column() == 1)
return contact.address;
}
return QVariant();
}
QVariant TableModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole) return QVariant();
if (orientation == Qt::Horizontal)
{
switch (section)
{
case 0:
return tr("Name");
case 1:
return tr("Address");
default:
return QVariant();
}
}
return QVariant();
}
bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
contacts.insert(position, {QString(), QString()});
endInsertRows();
return true;
}
bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row) contacts.removeAt(position);
endRemoveRows();
return true;
}
bool TableModel::setData(const QModelIndex &index, const QVariant &value,
int role)
{
if (index.isValid() && role == Qt::EditRole)
{
int row = index.row();
auto contact = contacts.value(row);
if (index.column() == 0)
contact.name = value.toString();
else if (index.column() == 1)
contact.address = value.toString();
else
return false;
contacts.replace(row, contact);
emit(dataChanged(index, index));
return true;
}
return false;
}
Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) return Qt::ItemIsEnabled;
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setGeometry(400, 250, 542, 390);
QList<Contact> contacts;
contacts << Contact{"name1", "adress 1"};
contacts << Contact{"name2", "adress 2"};
mModel = new TableModel(contacts, this);
mTableView = new radix::TableView(this);
mTableView->setModel(mModel);
setCentralWidget(mTableView);
}
MainWindow::~MainWindow() {}
/******************************************************************************/
/******************************** MAIN PROGRAM ********************************/
/******************************************************************************/
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
mainWindow.raise();
return app.exec();
}
#ifndef RADIX_RADIXWIDGETS_EXAMPLE_RADIXTABLEWIDGET_HH_
#define RADIX_RADIXWIDGETS_EXAMPLE_RADIXTABLEWIDGET_HH_
#include <QAbstractTableModel>
#include <QMainWindow>
#include <QWidget>
#include "radixwidgets/tableview.hh"
namespace Ui
{
class MainWindow;
}
struct Contact
{
QString name;
QString address;
};
class TableModel : public QAbstractTableModel
{
Q_OBJECT
QList<Contact> contacts;
public:
TableModel(QObject *parent = 0);
TableModel(QList<Contact> contacts, QObject *parent = 0);
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation,
int role) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
bool insertRows(int position, int rows,
const QModelIndex &index = QModelIndex()) override;
bool removeRows(int position, int rows,
const QModelIndex &index = QModelIndex()) override;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
radix::TableView *mTableView;
TableModel *mModel;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // RADIX_RADIXWIDGETS_EXAMPLE_RADIXTABLEWIDGET_HH_
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment