#include
#include "window.h"
#include "colorlisteditor.h"
#include "LineEditorCreator.h"
//! [0]
Window::Window()
{
/*QItemEditorFactory *factory = new QItemEditorFactory;
QItemEditorCreatorBase *colorListCreator =new QStandardItemEditorCreator();
factory->registerEditor(QVariant::Color, colorListCreator);
QItemEditorFactory::setDefaultFactory(factory);*/
QItemEditorFactory *factory = new QItemEditorFactory();
QItemEditorCreatorBase *lineEditorCreator = new QStandardItemEditorCreator();
factory->registerEditor(QVariant::String, lineEditorCreator);
QItemEditorFactory::setDefaultFactory(factory);
LineEditorCreator::setItemWidget(this);
//table->setItemDelegate(&delegate);
createGUI();
}
//! [0]
void Window::createGUI()
{
QList > list;
list << QPair(tr("Alice"), QColor("aliceblue")) <<
QPair(tr("Neptun"), QColor("aquamarine")) <<
QPair(tr("Ferdinand"), QColor("springgreen"));
table = new QTableWidget(3, 2);
table->setObjectName("tableTTT");
table->setHorizontalHeaderLabels(QStringList() << tr("Name") << tr("Hair Color"));
table->verticalHeader()->setVisible(false);
table->resize(150, 50);
connect(table, SIGNAL(cellChanged(int,int)), this, SLOT(changeTest(int, int)));
table->blockSignals(true);
for (int i = 0; i < 3; ++i) {
QPair pair = list.at(i);
QTableWidgetItem *nameItem = new QTableWidgetItem(pair.first);
QTableWidgetItem *colorItem = new QTableWidgetItem;
colorItem->setData(Qt::DisplayRole, pair.second);
nameItem->setCheckState(Qt::Checked);
//nameItem->setFlags(colorItem->flags () | Qt::ItemIsUserCheckable);
table->setItem(i, 0, nameItem);
table->setItem(i, 1, colorItem);
}
table->resizeColumnToContents(0);
table->horizontalHeader()->setStretchLastSection(true);
QGridLayout *layout = new QGridLayout;
layout->addWidget(table, 0, 0);
table->blockSignals(false);
setLayout(layout);
setWindowTitle(tr("Color Editor Factory"));
}
void Window::slotTextChange(const QString &text)
{
QTableWidgetItem *item = table->currentItem();
qDebug() << "edit = " << text;
qDebug() << "edit = " << item->data(Qt::DisplayRole) << item->checkState();
}
void Window::changeTest(int row, int col)
{
if (col==0){
if(table->item(row, col)->checkState() == Qt::Checked)
qDebug() << "checked ";
else
qDebug() << "unchecked ";
}
}