exodehm

Untitled

May 23rd, 2019
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5.  
  6. class MainWindow : public QMainWindow
  7. {
  8.     Q_OBJECT
  9.  
  10. public:
  11.     explicit MainWindow(QWidget *parent = nullptr);
  12.     ~MainWindow();
  13. };
  14.  
  15. #endif // MAINWINDOW_H
  16.  
  17. #include "mainwindow.h"
  18.  
  19. #include "./myeventfilter.h"
  20. #include "./mimodelo.h"
  21. #include <QTableView>
  22.  
  23. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  24. {
  25.     QTableView *tabla = new QTableView;
  26.     tabla->setModel(new MiModelo());
  27.     setCentralWidget(tabla);
  28.     MyEventFilter* filter = new MyEventFilter(tabla, this);
  29. }
  30.  
  31. MainWindow::~MainWindow()
  32. {
  33.  
  34. }
  35.  
  36. #ifndef MIMODELO_H
  37. #define MIMODELO_H
  38.  
  39. #include <QAbstractTableModel>
  40.  
  41. class MiModelo : public QAbstractTableModel
  42. {
  43. public:
  44.     MiModelo(QObject *parent = nullptr);
  45.     int rowCount(const QModelIndex& parent) const;
  46.     int columnCount(const QModelIndex& parent) const;
  47.     QVariant data(const QModelIndex& index,int role = Qt::DisplayRole) const;
  48.  
  49. };
  50.  
  51. #endif // MIMODELO_H
  52.  
  53. #include "mimodelo.h"
  54.  
  55. MiModelo::MiModelo(QObject *parent):QAbstractTableModel(parent)
  56. {
  57.  
  58. }
  59.  
  60. int MiModelo::columnCount(const QModelIndex &parent) const
  61. {
  62.     return 4;
  63. }
  64.  
  65. QVariant MiModelo::data(const QModelIndex &index, int role) const
  66. {
  67.     return QVariant();
  68. }
  69.  
  70. int MiModelo::rowCount(const QModelIndex &parent) const
  71. {
  72.     return 4;
  73. }
  74.  
  75. #ifndef MYEVENTFILTER_H
  76. #define MYEVENTFILTER_H
  77.  
  78. #include <QObject>
  79. #include <QModelIndex>
  80.  
  81. class QTableView;
  82. class QPushButton;
  83.  
  84. class MyEventFilter : public QObject
  85. {
  86.     Q_OBJECT
  87. public:
  88.     explicit MyEventFilter(QTableView* table, QObject* parent=nullptr);
  89.     bool eventFilter(QObject *obj, QEvent *event) override;
  90.  
  91. private slots:
  92.     void button_clicked();
  93.  
  94. private:
  95.     QTableView* table;
  96.     QPushButton * button;
  97.     QModelIndex currentIndex;
  98. };
  99.  
  100. #endif // MYEVENTFILTER_H
  101.  
  102. #include "myeventfilter.h"
  103. #include <QStyledItemDelegate>
  104. #include <QPushButton>
  105. #include <QTableView>
  106. #include <QModelIndex>
  107. #include <QEvent>
  108. #include <QHoverEvent>
  109.  
  110. MyEventFilter::MyEventFilter(QTableView* table, QObject* parent): QObject(parent),table(table),button(new QPushButton(table))
  111. {
  112.   button->setText("H");
  113.   button->setFixedSize(20, 20);
  114.   button->setVisible(false);
  115.  
  116.   table->installEventFilter(this);
  117.   table->setAttribute(Qt::WA_Hover);
  118.  
  119.   connect(button, SIGNAL(clicked()), SLOT(button_clicked()));
  120. }
  121.  
  122. bool MyEventFilter::eventFilter(QObject *obj, QEvent *event)
  123. {
  124.     if( event->type() == QEvent::HoverMove)
  125.     {
  126.         QHoverEvent * hoverEvent = static_cast<QHoverEvent*>(event);
  127.         QPoint pos = table->viewport()->mapFromParent(hoverEvent->pos());
  128.  
  129.         currentIndex = table->indexAt(pos);
  130.  
  131.         button->setVisible(currentIndex.isValid() && currentIndex.column() == 3);
  132.  
  133.         if( button->isVisible() )
  134.         {
  135.             QRect rect = table->visualRect(currentIndex);
  136.             QPoint point = rect.topRight();
  137.             point.setX(point.x() - button->width());
  138.             button->move(table->viewport()->mapToParent(point));
  139.         }
  140.         event->accept();
  141.         return true;
  142.     }
  143.     return false;
  144. }
  145.  
  146. void MyEventFilter::button_clicked()
  147. {
  148.  
  149. }
  150.  
  151.  
  152. #include "mainwindow.h"
  153. #include <QApplication>
  154.  
  155. int main(int argc, char *argv[])
  156. {
  157.     QApplication a(argc, argv);
  158.     MainWindow w;
  159.     w.show();
  160.  
  161.     return a.exec();
  162. }
RAW Paste Data