Why does the mousePressEvent function not be called when the mouse clicks on textedit?
-
if (fileExtension == "txt") { // 文本文件 ui->tabWidget->setCurrentIndex(0); textedit = new QTextEdit; QTextStream in(&file); in.setCodec("UTF-8"); // 设置文档编码 textedit->setPlainText(in.readAll()); textedit->setMouseTracking(true); textLayout->addWidget(textedit); } void MainWindow::mousePressEvent(QMouseEvent *event) { // 获取点击事件的目标对象 if (menu_in||tableName.isEmpty()) return; QObject *target = qApp->widgetAt(event->globalPos()); if (target == imageLabel) { // 图片容器 // 将全局位置转换为容器内的相对位置 if (imageLabel->pixmap() == nullptr) { qDebug() << "Pixmap is null!"; return; } QPoint localPos = imageLabel->mapFromGlobal(event->globalPos()); // 获取像素的行列号 int x = localPos.x(); int y = localPos.y(); QPixmap pixmap = imageLabel->pixmap()->copy(); // 复制图像以避免原始图像被修改 QRgb pixelValue = pixmap.toImage().pixel(localPos); int red = qRed(pixelValue); int green = qGreen(pixelValue); int blue = qBlue(pixelValue); QString pixelStr = QString("行 %1 列 %2,RGB值:R:%3 G:%4 B:%5\n").arg(y).arg(x).arg(red).arg(green).arg(blue); ui->textEdit_1->append(pixelStr); } else if (target==textedit) { // 检查点击事件的位置是否在 textedit 区域内 // 将全局位置转换为容器内的相对位置 QPoint localPos = textedit->mapFromGlobal(event->globalPos()); // 获取点击位置的光标 QTextCursor cursor = textedit->cursorForPosition(localPos); // 获取当前字 cursor.movePosition(QTextCursor::StartOfWord); // 将光标移动到当前字的开头 cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); // 选择当前字 QString currentWord = cursor.selectedText(); qDebug() << "当前字:" << currentWord; }else { qDebug() << "点击了其他地方。"; } }
-
@ADgai Please format you code properly!
If you click on textedit the event goes to that widget, not mainwindow.
You can install an event filter in mainwindow, see https://doc.qt.io/qt-6/eventsandfilters.html -
@ADgai said in Why does the mousePressEvent function not be called when the mouse clicks on textedit?:
and it still shows nothing
Should we guess why not? Where is this code? What does filterObject do?
If you want to get help then please provide needed information instead of posting small peace of code without any context.
And please format your code properly. -
bool FilterObject::eventFilter(QObject *object, QEvent *event) { qDebug() << "Event filter called"; if(!object) qDebug() << "error1"; if(!(event->type() == QEvent::MouseButtonPress)) qDebug()<<"error2"; if (object && event->type() == QEvent::MouseButtonPress) { QTextEdit *textedit = qobject_cast<QTextEdit *>(object); if(!textedit) qDebug()<<"error3"; if (textedit) { QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); if (mouseEvent->button() == Qt::LeftButton) { // 获取点击位置的全局坐标 QPoint globalPos = mouseEvent->globalPos(); // 将全局坐标转换为文本编辑器内的相对位置 QPoint localPos = textedit->viewport()->mapFromGlobal(globalPos); // 获取点击位置的光标 QTextCursor cursor = textedit->cursorForPosition(localPos); // 获取当前单词 if (!cursor.isNull()) { cursor.select(QTextCursor::WordUnderCursor); QString currentWord = cursor.selectedText(); qDebug() << "当前单词:" << currentWord; } qDebug() << "Left mouse button clicked on QTextEdit!"; return true; } } } return false; }
This is the definition of my function, and when I just put the mouse on the textedit and do not click it, it will display the Event filter called,error2. But when I click it shows nothing
-
@ADgai said in Why does the mousePressEvent function not be called when the mouse clicks on textedit?:
if (object && event->type() == QEvent::MouseButtonPress) {
Put debug output right after this line so that we know whether this if condition is entered