QTableWidget无法响应鼠标点击(QMousePressEvent)的问题

1 前言
使用QTableWidget显示内容时想要通过触发itemSelectionChanged()信号时执行某些操作,但是在表格只有一行时发现此信号无法触发,即使点击空白区域。 
因此想要捕获父窗口的QMousePressEvent() 来触发自定义信号,发现在表格内的鼠标点击事件无法被捕获。

2 解决方案
使用Qt的事件过滤器,捕获QTableWidget的视窗(viewPort)所发生的事件。 
1、为QTableWidget安装事件过滤器

tablewidget->viewport()->installEventFilter(this);
1
此处需要注意,必须是为tableWidget的viewport安装事件过滤器

2、父窗口重写bool eventFilter(QObject *obj, QEvent *e) 函数

bool MainWidget::eventFilter(QObject *obj, QEvent *e)
{
    if (obj == tablewidget->viewport())
    {
        if (e->type() == QEvent::MouseButtonPress){
            emit sig_focusChanged();
        }
    }
    return QWidget::eventFilter(obj,e);
}
现在在tableWidget内的鼠标点击事件都可以被捕获了
--------------------- 
作者:flist 
来源:CSDN 
原文:https://blog.csdn.net/baidu_33850454/article/details/80838305?utm_source=copy 
版权声明:本文为博主原创文章,转载请附上博文链接!

展开阅读全文

菜鸟的程序---子控件无法响应鼠标点击消息

08-22

我想实现鼠标右键点击菜单功能,但是连鼠标响应消息都没反应,请大虾指导rn[code=c]MainWindow::MainWindow(QWidget *parent) :rn QMainWindow(parent),rn ui(new Ui::MainWindow)rnrn curveWidget = new Curve_Frame(this);rn //QPushButton *curveWidge = new QPushButton(this);rn //QHBoxLayout *layout = new QHBoxLayout;rn //QWidget *centerWindow = new QWidget;rn //curveWidge->setCentralWidget(centerWindow);rnrn //curveWidge->setText("test!");rn //this->setGeometry(QRect(0,0,800,600));rn //layout->addWidget(curveWidge);rn //curveWidget->show();rn ui->setupUi(this);rn //this->setLayout(layout);rn //centerWindow->setLayout(layout);rnrnrnvoid MainWindow::paintEvent(QPaintEvent *)rnrn //setMinimumSize(800,600);rn //setMaximumSize(800,600);rn setGeometry(QRect(200,100,1000,700));rn setFixedSize(800,600);rn curveWidget->changeBkColor(QColor(0,0,0));rn curveWidget->changeArea(QRect(0,50,width() - 100, 100));rnrnrnMainWindow::~MainWindow()rnrn delete ui;rn delete curveWidget;rn[/code]rnrn[code=c]#include "curve_frame.h"rnrnCurve_Frame::Curve_Frame(QWidget *parent) :rn QWidget(parent)rnrn curX = 0;rn drawTimerId = startTimer(10);rn virtualTimeId = startTimer(10);rn cursorWidth = 10;rn qDebug() <= width())rn rn for(i = (width()-1); i > x; i--)rn rn drawer.drawLine(i,curveDataBuff[i-x-1],rn i,curveDataBuff[i-x]);rn //qDebug() << i << curveDataBuff[i]<timerId())rn rn /*必须要有数据才开始画*/rn if(curveDataBuff.count() > 1)rn rn //update(curX, 0, cursorWidth, height());rnrn curX++;rn if(curX >= x()+width()-1)rn rn curX = x();rn rn rn return;rn rn else if(virtualTimeId == event->timerId())rn rn curveDataBuff.append((i++)%60);rn if(curveDataBuff.count()>width())rn rn curveDataBuff.removeFirst();rn rn return;rn rn elsern rn QWidget::timerEvent(event);rn return;rn rn #endifrnrnrn#if 0rnvoid Curve_Frame::contextMenuEvent(QContextMenuEvent *)rnrn QMenu *menu = new QMenu(this);rn QAction *freeze = new QAction(this);rn QAction *clear = new QAction(this);rnrn freeze->setText("freeze");rn clear->setText("clear");rn menu->addAction(freeze);rn menu->addAction(clear);rn menu->exec(QCursor::pos());rnrnrn#endifrnrnvoid Curve_Frame::showMenu()rnrn QMenu *menu = new QMenu(this);rn QAction *freeze = new QAction(this);rn QAction *clear = new QAction(this);rnrn freeze->setText("freeze");rn clear->setText("clear");rn menu->addAction(freeze);rn menu->addAction(clear);rn menu->exec(QCursor::pos());rnrnrnvoid Curve_Frame::mousePressEvent(QMouseEvent * event)rnrn qDebug()<<"test";rn if(event->button()==Qt::RightButton)rn rn showMenu();rn rn[/code]rnrn[code=c]#include rn#include rn#include rn#include rnrnclass Curve_Frame : public QWidgetrnrn Q_OBJECTrnpublic:rn explicit Curve_Frame(QWidget *parent = 0);rn ~Curve_Frame();rn void paintEvent(QPaintEvent *);rn void changeBkColor(const QColor &iColor);rn void changeArea(const QRect & rect);rn void timerEvent(QTimerEvent *event);rn /*画波形的定时器id*/rn int drawTimerId;rn void mousePressEvent(QMouseEvent * event);rnprivate:rn void drawCurve();rn void showMenu();rnrn /*模拟数据产生的定时器*/rn int virtualTimeId;rn int curX;rn /*扫描光标的宽度*/rn int cursorWidth;rn QColor curveColor;rn QColor bkColor;rn /*虚拟的波形数据,与屏幕上点一一对应*/rn QList curveDataBuff;rnsignals:rnrnpublic slots:rn //slotRightClickMenu();rn;rnrn#endif // CURVE_FRAME_Hrn[/code] 论坛

没有更多推荐了,返回首页