Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
- };
- #endif // MAINWINDOW_H
- #include "mainwindow.h"
- #include "./myeventfilter.h"
- #include "./mimodelo.h"
- #include <QTableView>
- MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
- {
- QTableView *tabla = new QTableView;
- tabla->setModel(new MiModelo());
- setCentralWidget(tabla);
- MyEventFilter* filter = new MyEventFilter(tabla, this);
- }
- MainWindow::~MainWindow()
- {
- }
- #ifndef MIMODELO_H
- #define MIMODELO_H
- #include <QAbstractTableModel>
- class MiModelo : public QAbstractTableModel
- {
- public:
- MiModelo(QObject *parent = nullptr);
- int rowCount(const QModelIndex& parent) const;
- int columnCount(const QModelIndex& parent) const;
- QVariant data(const QModelIndex& index,int role = Qt::DisplayRole) const;
- };
- #endif // MIMODELO_H
- #include "mimodelo.h"
- MiModelo::MiModelo(QObject *parent):QAbstractTableModel(parent)
- {
- }
- int MiModelo::columnCount(const QModelIndex &parent) const
- {
- return 4;
- }
- QVariant MiModelo::data(const QModelIndex &index, int role) const
- {
- return QVariant();
- }
- int MiModelo::rowCount(const QModelIndex &parent) const
- {
- return 4;
- }
- #ifndef MYEVENTFILTER_H
- #define MYEVENTFILTER_H
- #include <QObject>
- #include <QModelIndex>
- class QTableView;
- class QPushButton;
- class MyEventFilter : public QObject
- {
- Q_OBJECT
- public:
- explicit MyEventFilter(QTableView* table, QObject* parent=nullptr);
- bool eventFilter(QObject *obj, QEvent *event) override;
- private slots:
- void button_clicked();
- private:
- QTableView* table;
- QPushButton * button;
- QModelIndex currentIndex;
- };
- #endif // MYEVENTFILTER_H
- #include "myeventfilter.h"
- #include <QStyledItemDelegate>
- #include <QPushButton>
- #include <QTableView>
- #include <QModelIndex>
- #include <QEvent>
- #include <QHoverEvent>
- MyEventFilter::MyEventFilter(QTableView* table, QObject* parent): QObject(parent),table(table),button(new QPushButton(table))
- {
- button->setText("H");
- button->setFixedSize(20, 20);
- button->setVisible(false);
- table->installEventFilter(this);
- table->setAttribute(Qt::WA_Hover);
- connect(button, SIGNAL(clicked()), SLOT(button_clicked()));
- }
- bool MyEventFilter::eventFilter(QObject *obj, QEvent *event)
- {
- if( event->type() == QEvent::HoverMove)
- {
- QHoverEvent * hoverEvent = static_cast<QHoverEvent*>(event);
- QPoint pos = table->viewport()->mapFromParent(hoverEvent->pos());
- currentIndex = table->indexAt(pos);
- button->setVisible(currentIndex.isValid() && currentIndex.column() == 3);
- if( button->isVisible() )
- {
- QRect rect = table->visualRect(currentIndex);
- QPoint point = rect.topRight();
- point.setX(point.x() - button->width());
- button->move(table->viewport()->mapToParent(point));
- }
- event->accept();
- return true;
- }
- return false;
- }
- void MyEventFilter::button_clicked()
- {
- }
- #include "mainwindow.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement