• 6785阅读
  • 1回复

在listview中设置delegate的问题 [复制链接]

上一主题 下一主题
离线qlizhi
 
只看楼主 倒序阅读 楼主  发表于: 2011-08-02
我在仿Qt的delegate的一个例子, 想在一个tableview中设置8X8的颜色块delegate代码:
  1. myDelegate::myDelegate(QObject *parent)
  2.     : QItemDelegate(parent)
  3. {
  4.      connect(this, SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), this, SLOT(closedelegate(QWidget *)));
  5. }
  6. myDelegate::~myDelegate()
  7. {
  8. }
  9. QWidget *myDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
  10. {
  11.     QTableWidget *editor = new QTableWidget(8, 8, (QWidget*)parent->parent()->parent());
  12. //    QTableWidget *editor = new QTableWidget(8, 8, parent);
  13.     editor->horizontalHeader()->setVisible(FALSE);
  14.     editor->verticalHeader()->setVisible(FALSE);
  15.     editor->setShowGrid(FALSE);
  16.     editor->horizontalHeader()->setDefaultSectionSize(21);
  17.     editor->verticalHeader()->setDefaultSectionSize(21);
  18.     editor->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置QtableWidget的 edit状态
  19.     int m_pos;
  20.     long colorValue = 0x000000;
  21.     colorbox m_createcolor[64];
  22.     long number[64] =  {            0,     128,   8388736,  8421376,  16753920,  16720383,   16711680,   8388608,
  23.         3223857,  198,   8061183,  9509,     16363008,  16728831,   16728576,   12976128,
  24.         4868682,  255,   11874766, 32768,    15711488,  16711935,   16734769,   12993024,
  25.         7631988,  27647, 12995278, 37921,    16764416,  16737279,   16739138,   12411705,
  26.         8684676,  38143, 14050254, 48458,    16766464,  16745727,   16745472,   13471585,
  27.         11908533, 46591, 15172814, 54874,    16770816,  16756215,   16749691,   13016179,
  28.         12632256, 57087, 15703238, 65280,    16776960,  16762623,   16756133,   14071172,
  29.         16777215, 65535, 16758214, 8716032,  16777092,  16248807,   16762574,   15718061};
  30.     QString name[64] = {        "Black",        "Blue",        "Purple",        "Khaki",        "Apricot",        "Pink",        "Red",        "Brown",
  31.         "DarkBlack",    "DarkBlue",    "DarkPurple",    "Yellow",        "DarkApricot",    "DarkPink",    "Magenta",    "DarkBrown",
  32.         "LightBlack",    "LightBlue","LightPurple",    "Green",        "DarkYellow",    "LightPink","LightRed",    "LightBrown",
  33.         "MidBlack",        "MidBlue",    "MidPurpler",    "Turquoise",    "MidYellow",    "MidPink",    "MIdRed",    "MidBrown",
  34.         "Darker",        "Azury",    "TintPurple",    "TintKhaki",    "TintApricot",    "TintPink",    "Orange",    "TintBrown",
  35.         "Gray",         "GrayBlue",    "GrayPurple",    "GrayKhaki",    "GrayApricot",    "GrayPink",    "GrayRed",    "GrayBrown",
  36.         "LightGray",    "LGrayBlue","Plum",            "LGrayGreen",    "Yellow",        "LGrayPink","LGrayRed",    "LGrayBrown",
  37.         "Whiter",        "Cyan",        "WhiterPurple",    "WhiterKhaki",    "WhiterApricot","WhiterPink","WhiterRed","WhiterBrown"};
  38.     for (int i = 0; i < 64; i++)
  39.     {
  40.         m_createcolor[i].m_index = i;
  41.         m_createcolor[i].m_name = name[i];
  42.         m_createcolor[i].m_number = number[i];
  43.     }
  44.     for (int i = 0;i<8;i++)
  45.     {
  46.         for (int j = 0;j<8;j++)
  47.         {
  48.             m_pos = j + i*8;
  49.             colorValue = m_createcolor[m_pos].m_number;
  50.             QTableWidgetItem* colorItem = new QTableWidgetItem;
  51.             QColor color = QColor(colorValue| 0xff000000);
  52.             QPixmap pixmap = QPixmap(40,36);
  53.             pixmap.fill(color);
  54.             QIcon coloricon = QIcon(pixmap);
  55.             colorItem->setIcon(coloricon);
  56.             //colorItem->setText(QString("%1").arg(colorValue));
  57.             colorItem->setText(tr(m_createcolor[m_pos].m_name.toLocal8Bit().data()));
  58.             colorItem->setData(Qt::UserRole,QVariant((int)colorValue));
  59.             editor->setItem(i,j,colorItem);
  60.         }
  61.     }
  62.     return editor;
  63. }
  64. void    myDelegate::setEditorData ( QWidget * editor, const QModelIndex & index ) const
  65. {
  66. //         int strd = index.model()->data(index).toInt();
  67. //
  68. //         QTableWidget *tableWidget = static_cast<QTableWidget*>(editor);
  69. //          tableWidget->setCurrentCell(2, 1);
  70. }
  71. void    myDelegate::setModelData ( QWidget * editor, QAbstractItemModel * model, const QModelIndex & index ) const
  72. {
  73.         QTableWidget *tablewidget = static_cast<QTableWidget*>(editor);
  74.         QTableWidgetItem *cell = tablewidget->currentItem();
  75.         model->setData(index, cell->data(Qt::UserRole));
  76.         int data = QVariant(cell->data(Qt::UserRole)).toInt();
  77.         QColor color = QColor(data | 0xff000000);
  78.         QPixmap pixmap = QPixmap(10,10);
  79.         pixmap.fill(color);
  80.         QIcon coloricon = QIcon(pixmap);
  81.         QStandardItem *item = ((QStandardItemModel *)model)->takeItem(index.row(), index.column());
  82.         item->setIcon(coloricon);
  83.         QString name = getNameByData(data);
  84.         item->setText(name);
  85.         ((QStandardItemModel *)model)->setItem(index.row(), index.column(), item);
  86.         emit closeEditor(editor, QAbstractItemDelegate::NoHint);
  87.         
  88. }
  89. void myDelegate::closedelegate(QWidget * editor)
  90. {
  91.     editor->close();
  92. }
  93. void    myDelegate::updateEditorGeometry ( QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const
  94. {
  95.     editor->setGeometry(option.rect);
  96.      editor->resize(200, 200);
  97. //     editor->setGeometry(200, 200, 200, 200);
  98. }
  99. QString myDelegate::getNameByData(int data) const
  100. {
  101.     QString qstring;
  102.     int i;
  103.     long number[64] =  {            0,     128,   8388736,  8421376,  16753920,  16720383,   16711680,   8388608,
  104.         3223857,  198,   8061183,  9509,     16363008,  16728831,   16728576,   12976128,
  105.         4868682,  255,   11874766, 32768,    15711488,  16711935,   16734769,   12993024,
  106.         7631988,  27647, 12995278, 37921,    16764416,  16737279,   16739138,   12411705,
  107.         8684676,  38143, 14050254, 48458,    16766464,  16745727,   16745472,   13471585,
  108.         11908533, 46591, 15172814, 54874,    16770816,  16756215,   16749691,   13016179,
  109.         12632256, 57087, 15703238, 65280,    16776960,  16762623,   16756133,   14071172,
  110.         16777215, 65535, 16758214, 8716032,  16777092,  16248807,   16762574,   15718061};
  111.     QString name[64] = {        "Black",        "Blue",        "Purple",        "Khaki",        "Apricot",        "Pink",        "Red",        "Brown",
  112.         "DarkBlack",    "DarkBlue",    "DarkPurple",    "Yellow",        "DarkApricot",    "DarkPink",    "Magenta",    "DarkBrown",
  113.         "LightBlack",    "LightBlue","LightPurple",    "Green",        "DarkYellow",    "LightPink","LightRed",    "LightBrown",
  114.         "MidBlack",        "MidBlue",    "MidPurpler",    "Turquoise",    "MidYellow",    "MidPink",    "MIdRed",    "MidBrown",
  115.         "Darker",        "Azury",    "TintPurple",    "TintKhaki",    "TintApricot",    "TintPink",    "Orange",    "TintBrown",
  116.         "Gray",         "GrayBlue",    "GrayPurple",    "GrayKhaki",    "GrayApricot",    "GrayPink",    "GrayRed",    "GrayBrown",
  117.         "LightGray",    "LGrayBlue","Plum",            "LGrayGreen",    "Yellow",        "LGrayPink","LGrayRed",    "LGrayBrown",
  118.         "Whiter",        "Cyan",        "WhiterPurple",    "WhiterKhaki",    "WhiterApricot","WhiterPink","WhiterRed","WhiterBrown"};
  119.     for (i = 0; i < 64; i++)
  120.     {
  121.         if (data == number[i])
  122.         {
  123.             break;
  124.         }
  125.     }
  126.     qstring = name[i];
  127.     return qstring;
  128. }
遇到两个问题
1>在tableview中设置了setEditTriggers为 每种类型都试过了 ,但是经常在tableview中点不出delegate。
2>当点出了delegate的时候选择颜色后,必需点击tableview的其他块才能让delegate更新model。


求解???

离线蠢蠢欲懂
只看该作者 1楼 发表于: 2012-05-04
应该要重写Model中的 flags函数吧
Qt::ItemFlags flags(const QModelIndex &index) const;
快速回复
限100 字节
 
上一个 下一个