Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Why does the mousePressEvent function not be called when the mouse clicks on textedit?
Forum Updated to NodeBB v4.3 + New Features

Why does the mousePressEvent function not be called when the mouse clicks on textedit?

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
7 Posts 2 Posters 412 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ADgai
    wrote on last edited by
    #1
          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() << "点击了其他地方。";
           
                  }
           
              }
    
    jsulmJ 1 Reply Last reply
    0
    • A ADgai
            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() << "点击了其他地方。";
             
                    }
             
                }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      3
      • jsulmJ jsulm

        @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

        A Offline
        A Offline
        ADgai
        wrote on last edited by
        #3

        @jsulm
        if(textedit){
        textedit->installEventFilter(&filterObject);}
        and it still shows nothing

        jsulmJ 1 Reply Last reply
        0
        • A ADgai

          @jsulm
          if(textedit){
          textedit->installEventFilter(&filterObject);}
          and it still shows nothing

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • jsulmJ jsulm

            @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.

            A Offline
            A Offline
            ADgai
            wrote on last edited by
            #5

            @jsulm

            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

            jsulmJ 1 Reply Last reply
            0
            • A ADgai

              @jsulm

              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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply
              0
              • jsulmJ jsulm

                @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

                A Offline
                A Offline
                ADgai
                wrote on last edited by
                #7

                @jsulm it doesn't go into the if .I guess it was intercepted by something when I clicked.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved