I want to display a button on my table ....

how i must set paint ?... to display direct QWidget ... not only on click.....

if (index.column() == 0) {
int xnummer = index.model()->data(index, Qt:isplayRole).toInt();
this->createEditor(tas, option, index);

}






Qt Code:
  1. BaseDelegate::BaseDelegate( QTableView *ta , QObject *parent )
  2. {
  3. tas = ta;
  4. }
  5.  
  6. void BaseDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  7. {
  8. /////////qDebug() << "### index-column " << index.column();
  9.  
  10. if (index.column() == 0) {
  11. int xnummer = index.model()->data(index, Qt::DisplayRole).toInt();
  12. this->createEditor(tas, option, index);
  13.  
  14. } else if (index.column() == 1) {
  15. QString coder = index.model()->data(index, Qt::DisplayRole).toString();
  16. QStyleOptionViewItem myOption = option;
  17. myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
  18. myOption.palette.setColor( QPalette::Text , Qt::red );
  19. drawDisplay(painter, myOption, myOption.rect,coder);
  20. drawFocus(painter, myOption, myOption.rect);
  21.  
  22. } else if (index.column() == 8) {
  23. QString anno = index.model()->data(index, Qt::DisplayRole).toString();
  24. //////////////QString coda = BerufHuman(coder);
  25. QStyleOptionViewItem myOption = option;
  26. myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
  27. if (anno !="0") {
  28. myOption.palette.setColor( QPalette::Text , Qt::gray );
  29. } else {
  30. myOption.palette.setColor( QPalette::Text , Qt::white );
  31. }
  32. drawDisplay(painter, myOption, myOption.rect,anno);
  33. drawFocus(painter, myOption, myOption.rect);
  34.  
  35. } else{
  36. QItemDelegate::paint(painter, option, index);
  37. }
  38. }
  39.  
  40. QWidget *BaseDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  41. {
  42. if (index.column() == 11 || index.column() == 7 ) {
  43. QDateTimeEdit *editor = new QDateTimeEdit(parent);
  44. editor->setDisplayFormat("dd.MM.yyyy");
  45. editor->setCalendarPopup(true);
  46. return editor;
  47. } else if (index.column() == 0) {
  48. Base_Button *editora = new Base_Button(parent);
  49. return editora;
  50. } else {
  51. return QItemDelegate::createEditor(parent, option, index);
  52. }
  53. }
  54.  
  55. void BaseDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  56. {
  57.  
  58. if (index.column() == 11 || index.column() == 7 ) {
  59. QString dateuser = index.model()->data(index, Qt::DisplayRole).toString();
  60. QDateTimeEdit *editorrun = qobject_cast<QDateTimeEdit *>(editor);
  61. if (editorrun) {
  62. editorrun->setDate(QDate::fromString(index.model()->data(index, Qt::EditRole).toString(), "dd.MM.yyyy"));
  63. }
  64.  
  65. } else if (index.column() == 0) {
  66. int xnummer = index.model()->data(index, Qt::DisplayRole).toInt();
  67. Base_Button *editorax = qobject_cast<Base_Button *>(editor);
  68. editorax->SetNummer(xnummer);
  69. } else {
  70. QItemDelegate::setEditorData(editor, index);
  71. }
  72. }
  73.  
  74. void BaseDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  75. {
  76. if (index.column() == 11 || index.column() == 7 ) {
  77. QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
  78. if (dateEditor) {
  79. model->setData(index,dateEditor->date().toString("dd.MM.yyyy"));
  80. }
  81. } else if (index.column() == 0) {
  82. Base_Button *editorax = qobject_cast<Base_Button *>(editor);
  83. model->setData(index,editorax->GetNummer());
  84. } else {
  85. QItemDelegate::setModelData(editor, model, index);
  86. }
  87. }
  88.  
  89.  
  90. class Base_Button: public QWidget
  91. {
  92. Q_OBJECT
  93. //
  94. public:
  95. QPushButton *pushButton;
  96.  
  97. Base_Button( QWidget *parent )
  98. : QWidget(parent)
  99. {
  100. setAttribute(Qt::WA_WindowPropagation);
  101. QVBoxLayout *widgetLayout = new QVBoxLayout(this);
  102. widgetLayout->setMargin(0);
  103. widgetLayout->setSpacing(0);
  104. pushButton = new QPushButton(this);
  105. pushButton->setText(tr("Edit Nr. 0"));
  106. widgetLayout->addWidget(pushButton);
  107. }
  108. void SetNummer( int numero ) {
  109. identificativo = numero;
  110. pushButton->setText(tr("Edit Nr. %1").arg(identificativo));
  111. }
  112. int GetNummer() {
  113. return identificativo;
  114. }
  115. private:
  116. int identificativo;
  117. QSize sizebase;
  118. protected:
  119. signals:
  120. public slots:
  121.  
  122. };
  123.  
  124.  
  125. class BaseDelegate : public QSqlRelationalDelegate
  126. {
  127. Q_OBJECT
  128.  
  129. public:
  130. BaseDelegate( QTableView *ta , QObject *parent );
  131. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
  132. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
  133. void setEditorData(QWidget *editor, const QModelIndex &index) const;
  134. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
  135. private slots:
  136. private:
  137. QTableView *tas;
  138. QObject *sap;
  139.  
  140. };
To copy to clipboard, switch view to plain text mode