Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a Character Counter for a QLineEdit

Tags:

c++

qt

qt5

I am trying to make a simple character counter, like the one in twitter, in QT specifically with the QLineEdit feature. Ideally, it should record the number of characters entered in a QLineEdit and display the number recorded in a separate display label. For example, spotify has a character counter when naming and adding a description to a playlist.

The function that I have declared to count the number of characters entered in the QLineEdit is defined like so:

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

I call this function in my main.cpp under the object, w.

w.countChar();

The reason I want to create a character counter function is because I have set a character limit for the QLineEdit so whatever the user enters can be filled into the main displayLabel at the minimum size of the window. I have done that by writing another function:

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

Which I called under the same object, w:

w.setLineInputTextCharLimit(200);

QTCreator is able to successfully build but the charCounter displayLabel does not change in value after I enter some amount of text into the QLineEdit.

image of the application built

It is clear that the displayLabel for the character counter is being read and has been activated however when I enter any amount of text, the value does not change.

The result after there is some text entered into the QLineEdit

So if the displayLabel is registered and a value is being shown, the function should be working but there is definitely something wrong with it too because the value not change to anything from that '0'?.

Edit: the UI file:

<?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>

And heres the signal-slot connection:

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);
}
like image 512
INeedHelpFrequently Avatar asked Oct 16 '22 12:10

INeedHelpFrequently


1 Answers

If you want to count the texts you must count each time the text changes and for that you must use the textChanged() signal, in the following code I show an example:

#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

like image 133
eyllanesc Avatar answered Oct 27 '22 19:10

eyllanesc