-
UID:120369
-
- 注册时间2011-08-02
- 最后登录2012-03-23
- 在线时间10小时
-
- 发帖3
- 搜Ta的帖子
- 精华0
- 金钱30
- 威望13
- 贡献值0
- 好评度3
-
访问TA的空间加好友用道具
|
我在仿Qt的delegate的一个例子, 想在一个 tableview中设置8X8的 颜色块delegate代码: - myDelegate::myDelegate(QObject *parent)
- : QItemDelegate(parent)
- {
- connect(this, SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), this, SLOT(closedelegate(QWidget *)));
- }
- myDelegate::~myDelegate()
- {
- }
- QWidget *myDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
- {
- QTableWidget *editor = new QTableWidget(8, 8, (QWidget*)parent->parent()->parent());
- // QTableWidget *editor = new QTableWidget(8, 8, parent);
- editor->horizontalHeader()->setVisible(FALSE);
- editor->verticalHeader()->setVisible(FALSE);
- editor->setShowGrid(FALSE);
- editor->horizontalHeader()->setDefaultSectionSize(21);
- editor->verticalHeader()->setDefaultSectionSize(21);
- editor->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置QtableWidget的 edit状态
- int m_pos;
- long colorValue = 0x000000;
- colorbox m_createcolor[64];
- long number[64] = { 0, 128, 8388736, 8421376, 16753920, 16720383, 16711680, 8388608,
- 3223857, 198, 8061183, 9509, 16363008, 16728831, 16728576, 12976128,
- 4868682, 255, 11874766, 32768, 15711488, 16711935, 16734769, 12993024,
- 7631988, 27647, 12995278, 37921, 16764416, 16737279, 16739138, 12411705,
- 8684676, 38143, 14050254, 48458, 16766464, 16745727, 16745472, 13471585,
- 11908533, 46591, 15172814, 54874, 16770816, 16756215, 16749691, 13016179,
- 12632256, 57087, 15703238, 65280, 16776960, 16762623, 16756133, 14071172,
- 16777215, 65535, 16758214, 8716032, 16777092, 16248807, 16762574, 15718061};
- QString name[64] = { "Black", "Blue", "Purple", "Khaki", "Apricot", "Pink", "Red", "Brown",
- "DarkBlack", "DarkBlue", "DarkPurple", "Yellow", "DarkApricot", "DarkPink", "Magenta", "DarkBrown",
- "LightBlack", "LightBlue","LightPurple", "Green", "DarkYellow", "LightPink","LightRed", "LightBrown",
- "MidBlack", "MidBlue", "MidPurpler", "Turquoise", "MidYellow", "MidPink", "MIdRed", "MidBrown",
- "Darker", "Azury", "TintPurple", "TintKhaki", "TintApricot", "TintPink", "Orange", "TintBrown",
- "Gray", "GrayBlue", "GrayPurple", "GrayKhaki", "GrayApricot", "GrayPink", "GrayRed", "GrayBrown",
- "LightGray", "LGrayBlue","Plum", "LGrayGreen", "Yellow", "LGrayPink","LGrayRed", "LGrayBrown",
- "Whiter", "Cyan", "WhiterPurple", "WhiterKhaki", "WhiterApricot","WhiterPink","WhiterRed","WhiterBrown"};
- for (int i = 0; i < 64; i++)
- {
- m_createcolor[i].m_index = i;
- m_createcolor[i].m_name = name[i];
- m_createcolor[i].m_number = number[i];
- }
- for (int i = 0;i<8;i++)
- {
- for (int j = 0;j<8;j++)
- {
- m_pos = j + i*8;
- colorValue = m_createcolor[m_pos].m_number;
- QTableWidgetItem* colorItem = new QTableWidgetItem;
- QColor color = QColor(colorValue| 0xff000000);
- QPixmap pixmap = QPixmap(40,36);
- pixmap.fill(color);
- QIcon coloricon = QIcon(pixmap);
- colorItem->setIcon(coloricon);
- //colorItem->setText(QString("%1").arg(colorValue));
- colorItem->setText(tr(m_createcolor[m_pos].m_name.toLocal8Bit().data()));
- colorItem->setData(Qt::UserRole,QVariant((int)colorValue));
- editor->setItem(i,j,colorItem);
- }
- }
- return editor;
- }
- void myDelegate::setEditorData ( QWidget * editor, const QModelIndex & index ) const
- {
- // int strd = index.model()->data(index).toInt();
- //
- // QTableWidget *tableWidget = static_cast<QTableWidget*>(editor);
- // tableWidget->setCurrentCell(2, 1);
- }
- void myDelegate::setModelData ( QWidget * editor, QAbstractItemModel * model, const QModelIndex & index ) const
- {
- QTableWidget *tablewidget = static_cast<QTableWidget*>(editor);
- QTableWidgetItem *cell = tablewidget->currentItem();
- model->setData(index, cell->data(Qt::UserRole));
- int data = QVariant(cell->data(Qt::UserRole)).toInt();
- QColor color = QColor(data | 0xff000000);
- QPixmap pixmap = QPixmap(10,10);
- pixmap.fill(color);
- QIcon coloricon = QIcon(pixmap);
- QStandardItem *item = ((QStandardItemModel *)model)->takeItem(index.row(), index.column());
- item->setIcon(coloricon);
- QString name = getNameByData(data);
- item->setText(name);
- ((QStandardItemModel *)model)->setItem(index.row(), index.column(), item);
- emit closeEditor(editor, QAbstractItemDelegate::NoHint);
-
- }
- void myDelegate::closedelegate(QWidget * editor)
- {
- editor->close();
- }
- void myDelegate::updateEditorGeometry ( QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const
- {
- editor->setGeometry(option.rect);
- editor->resize(200, 200);
- // editor->setGeometry(200, 200, 200, 200);
- }
- QString myDelegate::getNameByData(int data) const
- {
- QString qstring;
- int i;
- long number[64] = { 0, 128, 8388736, 8421376, 16753920, 16720383, 16711680, 8388608,
- 3223857, 198, 8061183, 9509, 16363008, 16728831, 16728576, 12976128,
- 4868682, 255, 11874766, 32768, 15711488, 16711935, 16734769, 12993024,
- 7631988, 27647, 12995278, 37921, 16764416, 16737279, 16739138, 12411705,
- 8684676, 38143, 14050254, 48458, 16766464, 16745727, 16745472, 13471585,
- 11908533, 46591, 15172814, 54874, 16770816, 16756215, 16749691, 13016179,
- 12632256, 57087, 15703238, 65280, 16776960, 16762623, 16756133, 14071172,
- 16777215, 65535, 16758214, 8716032, 16777092, 16248807, 16762574, 15718061};
- QString name[64] = { "Black", "Blue", "Purple", "Khaki", "Apricot", "Pink", "Red", "Brown",
- "DarkBlack", "DarkBlue", "DarkPurple", "Yellow", "DarkApricot", "DarkPink", "Magenta", "DarkBrown",
- "LightBlack", "LightBlue","LightPurple", "Green", "DarkYellow", "LightPink","LightRed", "LightBrown",
- "MidBlack", "MidBlue", "MidPurpler", "Turquoise", "MidYellow", "MidPink", "MIdRed", "MidBrown",
- "Darker", "Azury", "TintPurple", "TintKhaki", "TintApricot", "TintPink", "Orange", "TintBrown",
- "Gray", "GrayBlue", "GrayPurple", "GrayKhaki", "GrayApricot", "GrayPink", "GrayRed", "GrayBrown",
- "LightGray", "LGrayBlue","Plum", "LGrayGreen", "Yellow", "LGrayPink","LGrayRed", "LGrayBrown",
- "Whiter", "Cyan", "WhiterPurple", "WhiterKhaki", "WhiterApricot","WhiterPink","WhiterRed","WhiterBrown"};
- for (i = 0; i < 64; i++)
- {
- if (data == number[i])
- {
- break;
- }
- }
- qstring = name[i];
- return qstring;
- }
遇到两个 问题: 1>在tableview中设置了setEditTriggers为 每种类型都试过了 ,但是经常在tableview中点不出delegate。 2>当点出了delegate的时候选择颜色后,必需点击tableview的 其他块才能让delegate更新model。 求解???
|