我正在尝试制作一个简单的字符计数器,就像Twitter中的那个一样,在QT中使用QLineEdit功能。理想情况下,它应该记录在QLineEdit中输入的字符数,并在单独的显示标签中显示记录的数字。例如,Spotify在命名和添加播放列表描述时有一个字符计数器。
我已经声明了一个函数来计算在QLineEdit中输入的字符数,定义如下:
我在我的主.cpp文件中,在对象w下调用了这个函数。
我已经声明了一个函数来计算在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);
}