为QLineEdit设置字符计数器

时间:2018-09-23 05:48:27

标签: c++ qt qt5

我正在尝试使用QLineEdit功能在QT中创建一个简单的字符计数器,例如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();
}

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

w.countChar();

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

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

我在同一个对象下打电话给w:

w.setLineInputTextCharLimit(200);

QTCreator能够成功构建,但是在QLineEdit中输入一些文本后,charCounter 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);
}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我正在从QLabel而不是QLineEdit阅读文本。所以我的代码部分:

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

仅当我想读取QLineEdit中输入的文本时才从标签中读取文本。所以我将代码更改为:

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

并解决了该问题。 谢谢@FrozenM和@eyllanesc