为QLineEdit创建一个字符计数器

5
我正在尝试制作一个简单的字符计数器,就像Twitter中的那个一样,在QT中使用QLineEdit功能。理想情况下,它应该记录在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();
}

我在我的主.cpp文件中,在对象w下调用了这个函数。
w.countChar();

我想创建一个字符计数器函数的原因是因为我已经为QLineEdit设置了字符限制,所以无论用户输入什么内容都可以填充到主显示标签中,并且窗口大小可以最小化。我通过编写另一个函数来实现这一点:

void MainWindow::setlineInputTextCharLimit(int limit)
{
    ui->inputText->setMaxLength(limit);
}

我称之为同一对象的是w:

w.setLineInputTextCharLimit(200);

QTCreator可以成功构建,但是charCounter displayLabel在我输入一些文本后并没有更改其值。显示字符计数器的displayLabel已经被读取并已激活,但是当我输入任何文本时,值都不会改变。

应用程序生成的图像:

很明显,字符计数器的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>

以下是信号和槽的连接方式:

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);
}

你的信号/槽连接在哪里? - FrozenM
1
看起来你想要计算QLineEdit而不是QLabel,在这条语句中:QString tempString = ui->displayLabel->text(); - FrozenM
刚刚添加了我的信号槽连接。"ui->displayLabel->text();"是错误的。这解决了问题。谢谢。 - INeedHelpFrequently
好的,刚刚修复了。现在完美运行。谢谢。 - INeedHelpFrequently
2个回答

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"

enter image description here


是的,我刚刚添加了textChanged()信号,但程序仍然无法计算输入到QLineEdit中的字符数。您可以查看信号槽连接。我还参考了您的示例。 - INeedHelpFrequently
@INeedHelpFrequently 需要帮助。请将 QString tempString = ui->displayLabel->text(); 更改为 QString tempString = ui->inputText->text();。您正在计算QLabel文本而不是QLineEdit。 - eyllanesc

0
我正在阅读来自QLabel而不是QLineEdit的文本。因此,我的代码中的这一部分:
QString tempString = ui->displayLabel->text();

当我想要读取QLineEdit中输入的文本时,我只是从标签中读取了文本。因此,我将代码更改为:

QString tempString = ui->inputText->text();

这解决了问题。 感谢@FrozenM和@eyllanesc


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接