qt 无框dockwidget_Qt QDockWidget实现鼠标移出自动隐藏

//不解释,自己看。不保证完整,仅供思路参考#include #include "TableView.h"#include #include int main(int argc, char *argv[]){ QApplication a(argc, argv); QStandardItemModel model; for ( int col = 0; col < 3; col++ ) { QList list; for ( int row = 0; row < 5; row++ ) { list.append(new QStandardItem); } model.appendColumn(list); } TableView view; view.setModel(&model); view.show(); return a.exec();}#ifndef IVIEW_H#define IVIEW_Hclass IView {public: virtual void setMouseOver(const int) =0;};#endif // IVIEW_H #ifndef TABLEVIEW_H#define TABLEVIEW_H#include #include #include #include "IView.h"#include "Delegate.h"class TableView : public QTableView, public IView { Q_OBJECTprivate: int currHovered; void mouseMoveEvent(QMouseEvent *event); void disableMouseOver();public: TableView(QWidget *parent = 0); void setMouseOver(const int);};#endif // TABLEVIEW_H #include "TableView.h"#include TableView::TableView(QWidget *parent) : QTableView(parent), currHovered(-1){ Delegate *delegate = new Delegate; delegate->setView(this); setItemDelegate(delegate); setMouseTracking(true);} void TableView::setMouseOver(const int row){ if ( row == currHovered) return; QStandardItemModel *_model = static_cast(model()); for ( int col = 0; col < _model->columnCount(); col++ ) { QStandardItem *item = _model->item(row, col); item->setBackground(QBrush(QColor("red"))); } if ( currHovered != -1 ) { disableMouseOver(); } currHovered = row;} void TableView::disableMouseOver(){ QStandardItemModel *_model = static_cast(model()); for ( int col = 0; col < _model->columnCount(); col++ ) { QStandardItem *item = _model->item(currHovered, col); item->setBackground(QBrush(QColor("white"))); }} void TableView::mouseMoveEvent(QMouseEvent *event){ // TODO: you need know when mouse are not in table rect // then you need disable over QTableView::mouseMoveEvent(event);}#ifndef DELEGATE_H#define DELEGATE_H #include #include "IView.h"class Delegate : public QStyledItemDelegate {private: IView *view;public: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; void setView(IView *view) { this->view = view; }};#endif // DELEGATE_H #include "Delegate.h"#include void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QStyleOptionViewItemV4 o = option; initStyleOption(&o, index); if ( o.state & QStyle::State_MouseOver ) { view->setMouseOver(index.row()); } o.state &= ~QStyle::State_MouseOver; QStyledItemDelegate::paint(painter, o, index);}

取消

评论

你可能感兴趣的:(qt,无框dockwidget)