1

I tried to get any key using keyPressEvent() function, but I found out in debug mode that this function is called only when the key "shift/alt/ctrl" is pressed. I tried to press the key in textEdit box.

tipmanager.h :

namespace Ui {
class TipManager;
}

class TipManager : public QWidget
{
    Q_OBJECT

public:
    explicit TipManager(QWidget *parent = 0);
    ~TipManager();

    ...

protected:
    virtual void keyPressEvent(QKeyEvent*);

    ...

private:
    void on_titleEdit_textChanged(const QString &title);
    void on_codeTextEdit_textChanged();
    void on_memoTextEdit_textChanged();
    void enableButtons();
    ...
private:
    Ui::TipManager *ui;
    ...
    QDirModel *model;

};

#endif // TIPMANAGER_H

tipmanager.cpp :

#include "tipmanager.h"
#include "ui_tipmanager.h"

TipManager::TipManager(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TipManager)
{
    ui->setupUi(this);

    ui->dirEdit->setReadOnly(true);

    ...
}

...

void TipManager::enableButtons()
{
    //disable new button when all text boxes are empty
    flagTitleEdit|flagCodeTextEdit|flagMemoTextEdit
            ? ui->newButton->setEnabled(true)
            : ui->newButton->setEnabled(false);
    //enable save button when only both title and codeText bit are true
    flagTitleEdit&flagCodeTextEdit
            ? ui->saveButton->setEnabled(true)
            : ui->saveButton->setEnabled(false);
    //enable edit button when even a single text box is not empty
    flagTitleEdit|flagCodeTextEdit|flagMemoTextEdit
            ? ui->editButton->setEnabled(true)
            : ui->editButton->setEnabled(false);
}

void TipManager::on_titleEdit_textChanged(const QString &title)
{
    //when title text is empty, title bit is false
    QString text = title;
    text.trimmed().isEmpty() ? flagTitleEdit = false
                   : flagTitleEdit = true;
    //memo title edit is modified
    flagModified = true;

    enableButtons();
}

void TipManager::on_codeTextEdit_textChanged()
{
    //when code text is empty, codeText bit is false
    QString text = ui->codeTextEdit->toPlainText();
    text.trimmed().isEmpty() ? flagCodeTextEdit = false
                    : flagCodeTextEdit = true;
    //code text edit is modified
    flagModified = true;

    enableButtons();
}

void TipManager::on_memoTextEdit_textChanged()
{
    //when memo text is empty, memoText bit is false
    QString text = ui->memoTextEdit->toPlainText();
    text.trimmed().isEmpty() ? flagMemoTextEdit = false
                    : flagMemoTextEdit = true;
    //memo text edit is modified
    flagModified = true;

    enableButtons();
}

void TipManager::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Tab)
    {

    }

}

tipmanager.ui :

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>TipManager</class>
 <widget class="QWidget" name="TipManager">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>900</width>
    <height>558</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="minimumSize">
   <size>
    <width>720</width>
    <height>0</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>TipManager</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="1">
    <widget class="Line" name="line">
     <property name="enabled">
      <bool>true</bool>
     </property>
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
    </widget>
   </item>
   <item row="0" column="2">
    <widget class="QWidget" name="RightLayout" native="true">
     <property name="sizePolicy">
      <sizepolicy hsizetype="Fixed" vsizetype="Preferred">
       <horstretch>0</horstretch>
       <verstretch>0</verstretch>
      </sizepolicy>
     </property>
     <layout class="QVBoxLayout" name="rightLayout">
      <property name="spacing">
       <number>10</number>
      </property>
      <property name="sizeConstraint">
       <enum>QLayout::SetMinimumSize</enum>
      </property>
      <property name="leftMargin">
       <number>1</number>
      </property>
      <item>
       <layout class="QVBoxLayout" name="verticalLayout_3">
        <item>
         <widget class="QLabel" name="label_memo">
          <property name="text">
           <string>Memo</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QTextEdit" name="memoTextEdit">
          <property name="enabled">
           <bool>true</bool>
          </property>
          <property name="minimumSize">
           <size>
            <width>0</width>
            <height>0</height>
           </size>
          </property>
          <property name="cursor" stdset="0">
           <cursorShape>IBeamCursor</cursorShape>
          </property>
         </widget>
        </item>
       </layout>
      </item>
      <item>
       <layout class="QVBoxLayout" name="verticalLayout_2">
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout_3">
          <item>
           <widget class="QLabel" name="label_file">
            <property name="text">
             <string>File</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QLineEdit" name="dirEdit">
            <property name="minimumSize">
             <size>
              <width>0</width>
              <height>0</height>
             </size>
            </property>
            <property name="cursor">
             <cursorShape>ArrowCursor</cursorShape>
            </property>
            <property name="mouseTracking">
             <bool>false</bool>
            </property>
            <property name="acceptDrops">
             <bool>false</bool>
            </property>
           </widget>
          </item>
         </layout>
        </item>
        <item>
         <widget class="QTreeView" name="treeView">
          <property name="minimumSize">
           <size>
            <width>0</width>
            <height>0</height>
           </size>
          </property>
          <property name="cursor" stdset="0">
           <cursorShape>ArrowCursor</cursorShape>
          </property>
         </widget>
        </item>
       </layout>
      </item>
      <item>
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout">
          <item>
           <widget class="QPushButton" name="newButton">
            <property name="enabled">
             <bool>false</bool>
            </property>
            <property name="text">
             <string>New</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="editButton">
            <property name="enabled">
             <bool>false</bool>
            </property>
            <property name="text">
             <string>Edit</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="searchButton">
            <property name="text">
             <string>Search</string>
            </property>
           </widget>
          </item>
         </layout>
        </item>
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout_2">
          <item>
           <widget class="QPushButton" name="loadButton">
            <property name="text">
             <string>Load Dir</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="saveButton">
            <property name="enabled">
             <bool>false</bool>
            </property>
            <property name="text">
             <string>Save</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="aboutButton">
            <property name="enabled">
             <bool>false</bool>
            </property>
            <property name="text">
             <string>About</string>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </item>
     </layout>
    </widget>
   </item>
   <item row="0" column="0">
    <widget class="QWidget" name="LeftLayout" native="true">
     <property name="sizePolicy">
      <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
       <horstretch>0</horstretch>
       <verstretch>0</verstretch>
      </sizepolicy>
     </property>
     <property name="minimumSize">
      <size>
       <width>550</width>
       <height>480</height>
      </size>
     </property>
     <layout class="QVBoxLayout" name="leftLayout">
      <property name="spacing">
       <number>10</number>
      </property>
      <property name="sizeConstraint">
       <enum>QLayout::SetMinAndMaxSize</enum>
      </property>
      <property name="leftMargin">
       <number>1</number>
      </property>
      <property name="rightMargin">
       <number>1</number>
      </property>
      <item>
       <layout class="QVBoxLayout" name="verticalLayout_4">
        <item>
         <widget class="QLabel" name="label">
          <property name="text">
           <string>Title</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QLineEdit" name="titleEdit">
          <property name="enabled">
           <bool>true</bool>
          </property>
          <property name="minimumSize">
           <size>
            <width>0</width>
            <height>0</height>
           </size>
          </property>
         </widget>
        </item>
       </layout>
      </item>
      <item>
       <layout class="QVBoxLayout" name="verticalLayout_5">
        <item>
         <widget class="QLabel" name="label_code">
          <property name="text">
           <string>Code</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QTextEdit" name="codeTextEdit">
          <property name="enabled">
           <bool>true</bool>
          </property>
          <property name="minimumSize">
           <size>
            <width>0</width>
            <height>0</height>
           </size>
          </property>
          <property name="cursor" stdset="0">
           <cursorShape>IBeamCursor</cursorShape>
          </property>
         </widget>
        </item>
       </layout>
      </item>
     </layout>
    </widget>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

I declared #include <QKeyEvent> of course, and also wrote setFocusPolicy() but it didn't help. I'm using UI with a dialog, so not in the case of MainWindows. I want to know why this detects only shift/alt/ctrl key. I'm using Qt 5.3.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Sean
  • 301
  • 1
  • 2
  • 10

1 Answers1

1

If you want to capture the keys when you click on the codeTextEdit you must implement an event filter as shown below:

tipmanager.h

#ifndef TIPMANAGER_H
#define TIPMANAGER_H

#include <QWidget>

namespace Ui {
class TipManager;
}

class TipManager : public QWidget
{
    Q_OBJECT

public:
    explicit TipManager(QWidget *parent = 0);
    ~TipManager();

    bool eventFilter(QObject *watched, QEvent *event);

private:
    Ui::TipManager *ui;
};

#endif // TIPMANAGER_H

tipmanager.cpp

#include "tipmanager.h"
#include "ui_tipmanager.h"

#include <QKeyEvent>
#include <QTextEdit>

#include <QDebug>

TipManager::TipManager(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TipManager)
{
    ui->setupUi(this);
    ui->codeTextEdit->installEventFilter(this);
}

TipManager::~TipManager()
{
    delete ui;
}

bool TipManager::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->codeTextEdit && event->type() == QEvent::KeyPress){
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        qDebug()<<keyEvent->key();
    }
    return QWidget::eventFilter(watched, event);
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you, I implemented the code. and I modified your code in **eventFilter()** function like this in order to get _Key_Tab_, but it didn't help. `bool TipManager::eventFilter(QObject *watched, QEvent *event) { if(watched == ui->codeTextEdit && event->type() == QEvent::KeyPress){ QKeyEvent *keyEvent = static_cast(event); qDebug()<key(); if(keyEvent->key() == Qt::Key_Tab) { cleanTextBox(); } } return QWidget::eventFilter(watched, event); } ` – Sean Jun 25 '18 at 09:18
  • When I tested in Debug mode, it's strange that it's in this condition `if(keyEvent->key() == Qt::Key_Tab) { cleanTextBox(); }` over and over again. – Sean Jun 25 '18 at 09:18
  • @Sean Do you want to clean the QtextEdit when you press tab? – eyllanesc Jun 25 '18 at 09:19
  • actually, I want to do another option, but I'm just trying to use **cleanTextBox()** in order to test if it works well. – Sean Jun 25 '18 at 09:20
  • @Sean I just tried and it works, where is the cursor when you press the tab key? – eyllanesc Jun 25 '18 at 09:22
  • @Sean You can share your entire project through github, drive, dropbox, etc, I think the error is caused by another part of your code – eyllanesc Jun 25 '18 at 09:27
  • now it works. It's strange that changed code is not applied, even though I typed a new code. – Sean Jun 25 '18 at 09:28
  • After I deleted this code : `# install target.path = $$[QT_INSTALL_EXAMPLES]/widgets/dialogs/findfiles INSTALLS += target` `requires(qtConfig(filedialog))` in **.pro** file, it works fine. Do you know why this happens? – Sean Jun 25 '18 at 09:28
  • @Sean Sometimes the moc is not updated or it is probably filtering the event in that code that you deleted and generates this type of problems, so it is advisable to delete the build, if my answer helps you do not forget to mark it as correct, if you do not know how to do it review the [tour], that's the best way to thank. – eyllanesc Jun 25 '18 at 09:31
  • I wanted to move 4 blanks instead of 8 blanks to the right when I press Tap key. But I founded out that this event function is called first, and then the tap key is applied. So, not like **+8 => -4 = +4** but **-4 => +8 = +4**. But the problem is that words are also shifted with the tap key because of the latter mechanism. Do you have idea how to solve this? Sorry, I am now sure if it is right to ask here. – Sean Jun 25 '18 at 09:46
  • @Sean Do you want to change the amount of spaces generated when the tab key is pressed? When you ask always points out your underlying problem, maybe the current method is unsuitable for what you want, that is called [XY Problem](http://xyproblem.info/) – eyllanesc Jun 25 '18 at 09:49
  • I thought my questions would be a previous step for this issue. But it seems that it was totally wrong. Sorry, I didn't know that this easy method existed. – Sean Jun 25 '18 at 09:58
  • 2
    @Sean Do not assume, be direct, focus on your fundamental question and within the development of your question you should point out your attempt at solution, then we could go through the 2 paths, seeing and discarding possibilities, for your next ones take these considerations, and avoid occultism We will not steal anything. ;) – eyllanesc Jun 25 '18 at 10:01