qt中如何使鼠标点击后显示一个字符到TextEdit(我在做一个计算器程序)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qt中如何使鼠标点击后显示一个字符到TextEdit(我在做一个计算器程序)相关的知识,希望对你有一定的参考价值。

如点击数字键‘1“,字符’1‘就会显示在textedit对话框中

参考技术A 要将button的信号事件和对应的槽函数连接起来:用这句 
QObject::connect( button, SIGNAL(clicked()), textedit, SLOT(on_button_clicked()) );
其中button的类申明内要加信号函数clicked的申明,要用signals关键字,处理该事件的类(你这边应该是计算器类吧)内申明on_button_clicked()
显示的操作在  on_button_clicked()内处理。

在Qt中显示对话框的密码

我需要创建一个这样的登录对话框

enter image description here enter image description here

图像文件:eyeOn.pngeyeOff.png

要求:

  • 只有当我们点击并按住eyeOn图标时才会显示密码(即使我们点击并按住并将鼠标拖动到对话框外的区域,密码仍会显示),当我们释放鼠标时,密码再次被覆盖。
  • 显示密码时,眼睛图标为eyeOn。覆盖密码时,眼睛图标为eyeOff。

我刚刚建立了布局。

QGridLayout *mainlogin = new QGridLayout();

QLabel *usernameLabel = new QLabel;
usernameLabel->setWordWrap(true);
usernameLabel->setText("Username");
mainlogin->addWidget(usernameLabel, 0, 0);

QComboBox *usernameLineEdit = new QComboBox;
usernameLineEdit->setEditable(true);
usernameLabel->setBuddy(usernameLineEdit);
mainlogin->addWidget(usernameLineEdit, 0, 1);

QLabel *capslockShow = new QLabel;
capslockShow->setWordWrap(true);
capslockShow->setText(" ");
mainlogin->addWidget(capslockShow, 1, 1);

QLabel *passwordLabel = new QLabel;
passwordLabel->setWordWrap(true);
passwordLabel->setText("Password");
mainlogin->addWidget(passwordLabel, 2, 0);
QLineEdit *passwordLineEdit = new QLineEdit;
passwordLineEdit->setEchoMode(QLineEdit::Password);
QAction *myAction = passwordLineEdit->addAction(QIcon(":/eyeOff.png"), QLineEdit::TrailingPosition);
passwordLabel->setBuddy(passwordLineEdit);
mainlogin->addWidget(passwordLineEdit, 2, 1);

接下来我该怎么办?请帮助我使用代码段。

答案

解决方案是将QAction添加到QLineEdit,这将创建一个QToolButton,我们可以从associatedWidgets()获得(它将是第二个小部件,因为第一个小部件是与clearButton相关的小部件)。已经有QToolButton你必须使用pressedreleased信号。

passwordlineedit.h

#ifndef PASSWORDLINEEDIT_H
#define PASSWORDLINEEDIT_H

#include <QAction>
#include <QLineEdit>
#include <QToolButton>

class PasswordLineEdit: public QLineEdit
{
public:
    PasswordLineEdit(QWidget *parent=nullptr);
private slots:
    void onPressed();
    void onReleased();
protected:
    void enterEvent(QEvent *event);
    void leaveEvent(QEvent *event);
    void focusInEvent(QFocusEvent *event);
    void focusOutEvent(QFocusEvent *event);
private:
    QToolButton *button;
};

#endif // PASSWORDLINEEDIT_H

passwordlineedit.cpp

#include "passwordlineedit.h"

PasswordLineEdit::PasswordLineEdit(QWidget *parent):
    QLineEdit(parent)
{
    setEchoMode(QLineEdit::Password);
    QAction *action = addAction(QIcon(":/eyeOff"), QLineEdit::TrailingPosition);
    button = qobject_cast<QToolButton *>(action->associatedWidgets().last());
    button->hide();
    button->setCursor(QCursor(Qt::PointingHandCursor));
    connect(button, &QToolButton::pressed, this, &PasswordLineEdit::onPressed);
    connect(button, &QToolButton::released, this, &PasswordLineEdit::onReleased);
}

void PasswordLineEdit::onPressed(){
    QToolButton *button = qobject_cast<QToolButton *>(sender());
    button->setIcon(QIcon(":/eyeOn"));
    setEchoMode(QLineEdit::Normal);
}

void PasswordLineEdit::onReleased(){
    QToolButton *button = qobject_cast<QToolButton *>(sender());
    button->setIcon(QIcon(":/eyeOff"));
    setEchoMode(QLineEdit::Password);
}

void PasswordLineEdit::enterEvent(QEvent *event){
    button->show();
    QLineEdit::enterEvent(event);
}

void PasswordLineEdit::leaveEvent(QEvent *event){
    button->hide();
    QLineEdit::leaveEvent(event);
}

void PasswordLineEdit::focusInEvent(QFocusEvent *event){
    button->show();
    QLineEdit::focusInEvent(event);
}

void PasswordLineEdit::focusOutEvent(QFocusEvent *event){
    button->hide();
    QLineEdit::focusOutEvent(event);
}

完整的示例可以从以下link下载。

另一答案

而不是试图利用QAction返回的QLineEdit::addAction,你可以使用QWidgetAction与适当的事件过滤器结合使用...

class eye_spy: public QWidgetAction {
  using super = QWidgetAction;
public:
  explicit eye_spy (QLineEdit *control, QWidget *parent = nullptr)
    : super(parent)
    , m_control(control)
    , m_on(":/eyeOn")
    , m_off(":/eyeOff")
    , m_pixmap_size(50, 50)
    {
      m_label.setScaledContents(true);
      m_control->setEchoMode(QLineEdit::Password);
      m_label.setPixmap(m_off.pixmap(m_pixmap_size));
      m_label.installEventFilter(this);
      setDefaultWidget(&m_label);
    }
protected:
  virtual bool eventFilter (QObject *obj, QEvent *event) override
    {
      if (event->type() == QEvent::MouseButtonPress) {
        m_control->setEchoMode(QLineEdit::Normal);
        m_label.setPixmap(m_on.pixmap(m_pixmap_size));
      } else if (event->type() == QEvent::MouseButtonRelease) {
        m_control->setEchoMode(QLineEdit::Password);
        m_label.setPixmap(m_off.pixmap(m_pixmap_size));
      }
      return(super::eventFilter(obj, event));
    }
private:
  QLineEdit *m_control;
  QLabel     m_label;
  QIcon      m_on;
  QIcon      m_off;
  QSize      m_pixmap_size;
};

现在,而不是......

QAction *myAction = passwordLineEdit->addAction(QIcon(":/eyeOff.png"), QLineEdit::TrailingPosition);

使用...

eye_spy eye_spy(passwordLineEdit);
passwordLineEdit->addAction(&eye_spy, QLineEdit::TrailingPosition);

似乎提供了所需的行为。

以上是关于qt中如何使鼠标点击后显示一个字符到TextEdit(我在做一个计算器程序)的主要内容,如果未能解决你的问题,请参考以下文章

Qt 如何获取滚动窗体中鼠标点击的坐标

qt中mpv的鼠标点击

Qt Creator 鼠标悬浮信号怎么生成?就像MFC里的OVERMOUSE。

Qt OpenGL-如何根据鼠标点击获取对象

Qt自定义界面边框后,移动鼠标拖动界面,界面会拖到任务栏以下。。。如何解决?

QT编程的QDialog对话框如何设置为不能用鼠标点击后拉大或缩小,也就是我想要一个不能伸缩的对话