mytablewidget.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include <QTableWidget>
  3. #include <QHeaderView>
  4. #include <QDebug>
  5. #include <QCheckBox>
  6. #include <QHBoxLayout>
  7. class MyTableWidget : public QTableWidget
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit MyTableWidget(QWidget *parent = 0);
  12. void leaveEvent(QEvent *event);
  13. void setRowColor(int row, QColor color);
  14. void addTool(int rowindex,QString toolname, QString toolvalue)
  15. {
  16. QWidget* widget = new QWidget;
  17. QCheckBox* checkbox = new QCheckBox(widget);
  18. checkbox->setChecked(true);
  19. checkbox->setObjectName(QString::number(rowindex));
  20. checkbox->setStyleSheet("background: transparent;");
  21. checkbox->setMinimumSize(16, 16);
  22. checkbox->setMaximumSize(16, 16);
  23. connect(checkbox, SIGNAL(clicked(bool)), this, SLOT(updateClick(bool)));
  24. QHBoxLayout* layout = new QHBoxLayout(widget);
  25. layout->addWidget(checkbox);
  26. layout->setSpacing(0);
  27. layout->setContentsMargins(0, 0, 0, 0);
  28. widget->setLayout(layout);
  29. this->setRowCount(rowindex + 1);
  30. this->setCellWidget(rowindex, 0, widget);
  31. this->setItem(rowindex, 1, new QTableWidgetItem(toolname));
  32. this->item(rowindex, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  33. this->setItem(rowindex, 2, new QTableWidgetItem(toolvalue));
  34. this->item(rowindex, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  35. }
  36. private slots:
  37. void cellenterSlot(int row, int col);
  38. void updateClick(bool ok);
  39. signals:
  40. void sigClick(bool ok);
  41. private:
  42. QColor lastrowbgcolor;
  43. int lastrow;
  44. };