exodehm

Untitled

May 23rd, 2019
194
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 = 0);
  12.     ~MainWindow();
  13.  
  14. };
  15.  
  16. #endif // MAINWINDOW_H
  17.  
  18. #include "mainwindow.h"
  19.  
  20. #include "./mitabla.h"
  21.  
  22. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  23. {
  24.     MiTabla *tabla = new MiTabla;
  25.     setCentralWidget(tabla);
  26. }
  27.  
  28. MainWindow::~MainWindow()
  29. {
  30.  
  31. }
  32.  
  33. #ifndef MIMODELO_H
  34. #define MIMODELO_H
  35.  
  36. #include <QAbstractTableModel>
  37.  
  38. class MiModelo : public QAbstractTableModel
  39. {
  40. public:
  41.     MiModelo(QObject *parent = nullptr);
  42.     int rowCount(const QModelIndex& parent) const;
  43.     int columnCount(const QModelIndex& parent) const;
  44.     QVariant data(const QModelIndex& index,int role = Qt::DisplayRole) const;
  45.  
  46. private:
  47.     int filas,columnas;
  48. };
  49.  
  50. #endif // MIMODELO_H
  51.  
  52. #include "mimodelo.h"
  53.  
  54. MiModelo::MiModelo(QObject *parent):QAbstractTableModel(parent)
  55. {
  56.     filas = 4;
  57.     columnas = 4;
  58. }
  59.  
  60. int MiModelo::columnCount(const QModelIndex &parent) const
  61. {
  62.     return columnas;
  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 filas;
  73. }
  74.  
  75.  
  76. #ifndef MITABLA_H
  77. #define MITABLA_H
  78.  
  79. #include <QTableView>
  80.  
  81. class MiTabla : public QTableView
  82. {
  83.     Q_OBJECT
  84. public:
  85.     MiTabla(QWidget *parent=nullptr);
  86.     void mouseMoveEvent(QMouseEvent *event);
  87.  
  88. signals:
  89.     void hoverIndexChanged(QModelIndex);
  90. };
  91.  
  92. #endif // MITABLA_H
  93.  
  94.  
  95. #include "mitabla.h"
  96. #include "./myeventfilter.h"
  97. #include "./mimodelo.h"
  98. #include <QMouseEvent>
  99. #include <QDebug>
  100.  
  101. MiTabla::MiTabla(QWidget *parent):QTableView(parent)
  102. {
  103.     MyEventFilter* filter = new MyEventFilter;
  104.     MiModelo* modelo = new MiModelo;
  105.     this->setModel(modelo);
  106.     this->setItemDelegateForColumn(3,filter);
  107.     this->setMouseTracking(true);
  108.     this->setAttribute(Qt::WA_Hover);
  109.  
  110.     QObject::connect(this,SIGNAL(hoverIndexChanged(QModelIndex)),filter,SLOT(onHoverIndexChanged(QModelIndex)));
  111.  
  112. }
  113.  
  114. void MiTabla::mouseMoveEvent(QMouseEvent *event)
  115. {
  116.     QPoint pos = event->pos();
  117.     QModelIndex index = indexAt(pos);
  118.     if (index.isValid())
  119.     {
  120.         if (index.column() == 3)
  121.         {
  122.             //setCursor(Qt::DragMoveCursor);
  123.             emit hoverIndexChanged(index);
  124.         }
  125.         else
  126.         {
  127.             //setCursor(Qt::ArrowCursor);
  128.             emit hoverIndexChanged(index);
  129.         }
  130.     }
  131.     QTableView::mouseMoveEvent(event);
  132. }
  133.  
  134. #ifndef MYEVENTFILTER_H
  135. #define MYEVENTFILTER_H
  136.  
  137. #include <QObject>
  138. #include <QStyledItemDelegate>
  139.  
  140. class QModelIndex;
  141.  
  142. class MyEventFilter : public QStyledItemDelegate
  143. {
  144.     Q_OBJECT
  145. public:
  146.     explicit MyEventFilter(QObject* parent=nullptr);
  147.     void paint( QPainter *painter,const QStyleOptionViewItem &option, const QModelIndex &index ) const;
  148.     bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
  149.  
  150. private:
  151.      QModelIndex m_indiceActivo;
  152.     int m_ancho_boton;
  153.  
  154. public slots:
  155.     void onHoverIndexChanged(const QModelIndex& indice);
  156.  
  157. };
  158.  
  159. #endif // MYEVENTFILTER_H
  160.  
  161. #include "myeventfilter.h"
  162. #include <QPainter>
  163. #include <QApplication>
  164. #include <QMouseEvent>
  165. #include <QDebug>
  166.  
  167. MyEventFilter::MyEventFilter(QObject* parent):QStyledItemDelegate(parent)
  168. {
  169.     m_ancho_boton = 15;
  170. }
  171.  
  172.  
  173. bool MyEventFilter::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
  174. {
  175.     if( event->type() == QEvent::MouseButtonRelease )
  176.     {
  177.         QMouseEvent * e = (QMouseEvent *)event;
  178.         int clickX = e->x();
  179.         int clickY = e->y();
  180.  
  181.         QRect r = option.rect;//getting the rect of the cell
  182.         int x,y,w,h;
  183.         x = r.left() + r.width() - m_ancho_boton;//the X coordinate
  184.         y = r.top();//the Y coordinate
  185.         w = m_ancho_boton;//button width
  186.         h = r.height();//button height
  187.  
  188.         if( clickX > x && clickX < x + w )
  189.             if( clickY > y && clickY < y + h )
  190.             {
  191.                 qDebug()<<"Hacer algo";
  192.  
  193.             }
  194.         return true;
  195.     }
  196.     return false;
  197.  
  198. }
  199.  
  200. void MyEventFilter::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  201. {
  202.     if (index.isValid() && index == m_indiceActivo && index.column()==3)
  203.     {
  204.         painter->drawText(option.rect,index.data().toString());
  205.         QStyleOptionButton boton;
  206.         QRect r = option.rect;//getting the rect of the cell
  207.         int x,y,w,h;
  208.         w = m_ancho_boton;
  209.         h = r.height();//button height
  210.         x = r.left() + r.width() - w;//the X coordinate
  211.         y = r.top();//the Y coordinate
  212.         boton.rect = QRect(x,y,w,h);
  213.         boton.text = "...";
  214.         boton.state = QStyle::State_MouseOver;
  215.         QApplication::style()->drawControl( QStyle::CE_PushButton, &boton, painter);
  216.     }
  217.     else
  218.     {
  219.         QStyledItemDelegate::paint(painter, option, index);
  220.     }
  221.  
  222. }
  223.  
  224. void MyEventFilter::onHoverIndexChanged(const QModelIndex &indice)
  225. {
  226.     m_indiceActivo = indice;
  227. }
  228.  
  229.  
  230. #include "mainwindow.h"
  231. #include <QApplication>
  232.  
  233. int main(int argc, char *argv[])
  234. {
  235.     QApplication a(argc, argv);
  236.     MainWindow w;
  237.     w.show();
  238.  
  239.     return a.exec();
  240. }
RAW Paste Data