为 QLineEdit 制作字符计数器

INe*_*tly 5 c++ qt qt5

我正在尝试在 QT 中专门使用 QLineEdit 功能制作一个简单的字符计数器,例如 twitter 中的字符计数器。理想情况下,它应该记录在 QLineEdit 中输入的字符数,并在单独的显示标签中显示记录的数字。例如,Spotify 在为播放列表命名和添加描述时有一个字符计数器。

我声明的用于计算 QLineEdit 中输入的字符数的函数定义如下:

void MainWindow::countChar()
{
    QString tempString = ui->displayLabel->text(); //temp variable to hold the lineEdit's text
    int output = tempString.size(); //to get the number of characters in the lineEdit
    QString s = QString::number(output);//convert an int to QString
    ui->CharCounter->setText(s); //display the number in the displayLabel(CharCounter)
    ui->CharCounter->adjustSize();
}
Run Code Online (Sandbox Code Playgroud)

我在 main.cpp 中的对象 w 下调用此函数。

w.countChar();
Run Code Online (Sandbox Code Playgroud)

我想创建字符计数器函数的原因是因为我为 QLineEdit 设置了字符限制,因此用户输入的任何内容都可以以窗口的最小尺寸填充到主 displayLabel 中。我通过编写另一个函数来做到这一点:

void MainWindow::setlineInputTextCharLimit(int limit)
{
    ui->inputText->setMaxLength(limit);
}
Run Code Online (Sandbox Code Playgroud)

我在同一个对象下调用它,w:

w.setLineInputTextCharLimit(200);
Run Code Online (Sandbox Code Playgroud)

QTCreator 能够成功构建,但在我在 QLineEdit 中输入一定量的文本后,charCounter displayLabel 的值没有改变。

构建的应用程序的图像

很明显,字符计数器的显示标签正在被读取并已被激活,但是当我输入任意数量的文本时,该值不会改变。

在 QLineEdit 中输入一些文本后的结果

因此,如果 displayLabel 已注册并且正在显示一个值,则该函数应该可以工作,但肯定也有问题,因为该值不会从“0”变为任何值?

编辑:UI 文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>554</width>
    <height>463</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QWidget" name="layoutWidget">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>20</y>
      <width>311</width>
      <height>211</height>
     </rect>
    </property>
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QLineEdit" name="inputText"/>
     </item>
     <item>
      <widget class="QPushButton" name="textBtn">
       <property name="text">
        <string>Display Text</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLabel" name="displayLabel">
       <property name="sizePolicy">
        <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <property name="minimumSize">
        <size>
         <width>100</width>
         <height>100</height>
        </size>
       </property>
       <property name="text">
        <string/>
       </property>
       <property name="scaledContents">
        <bool>true</bool>
       </property>
       <property name="wordWrap">
        <bool>true</bool>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLabel" name="CharCounter">
       <property name="enabled">
        <bool>true</bool>
       </property>
       <property name="sizePolicy">
        <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <property name="minimumSize">
        <size>
         <width>0</width>
         <height>0</height>
        </size>
       </property>
       <property name="text">
        <string/>
       </property>
       <property name="scaledContents">
        <bool>true</bool>
       </property>
       <property name="wordWrap">
        <bool>true</bool>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>554</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>
Run Code Online (Sandbox Code Playgroud)

这是信号槽连接:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->textBtn, &QPushButton::clicked, this, &MainWindow::setText);
    connect(ui->inputText, &QLineEdit::textChanged, this, &MainWindow::countChar);
}
Run Code Online (Sandbox Code Playgroud)

eyl*_*esc 3

如果您想对文本进行计数,则必须在每次文本更改时进行计数,为此您必须使用信号textChanged(),在下面的代码中我展示了一个示例:

#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QWidget>

class CounterWidget: public QWidget
{
    Q_OBJECT
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
    Q_PROPERTY(int maxLenght READ maxLenght WRITE setMaxLenght)
    Q_PROPERTY(int length READ length)
public:
    CounterWidget(QWidget *parent=nullptr):
        CounterWidget(200, parent)
    {
    }
    CounterWidget(int maxLength, QWidget *parent=nullptr):
        QWidget(parent),
        layout(this)
    {
        layout.addWidget(&lineEdit);
        layout.addWidget(&counterLabel, 0, Qt::AlignTop | Qt::AlignRight);
        connect(&lineEdit, &QLineEdit::textChanged, this, &CounterWidget::countChar);
        connect(&lineEdit, &QLineEdit::textChanged, this, &CounterWidget::textChanged);
        lineEdit.setMaxLength(maxLength);
        countChar("");
    }
    QString text() const{
        return lineEdit.text();
    }
    void setText(const QString &text){
        lineEdit.setText(text);
    }
    int maxLenght() const{
        return  lineEdit.maxLength();
    }
    void setMaxLenght(int maxLenght){
        lineEdit.setMaxLength(maxLenght);
    }
    int length() const{
        return lineEdit.text().size();
    }
signals:
    void textChanged(const QString & text);
private slots:
    void countChar(const QString & text){
        QString text_label = QString("%1/%2").arg(text.size()).arg(lineEdit.maxLength());
        counterLabel.setText(text_label);
    }
private:
    QVBoxLayout layout;
    QLineEdit lineEdit;
    QLabel counterLabel;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CounterWidget w;
    w.show();

    return a.exec();
}

#include "main.moc"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述