C++ 为QLineEdit创建字符计数器
我试图在QT中制作一个简单的字符计数器,就像twitter中的一样,特别是使用QLineEdit特性。理想情况下,它应该记录在QLineEdit中输入的字符数,并在单独的显示标签中显示记录的数字。例如,spotify在命名和向播放列表添加描述时有一个字符计数器 我声明用于计算QLineEdit中输入的字符数的函数定义如下:C++ 为QLineEdit创建字符计数器,c++,qt,qt5,C++,Qt,Qt5,我试图在QT中制作一个简单的字符计数器,就像twitter中的一样,特别是使用QLineEdit特性。理想情况下,它应该记录在QLineEdit中输入的字符数,并在单独的显示标签中显示记录的数字。例如,spotify在命名和向播放列表添加描述时有一个字符计数器 我声明用于计算QLineEdit中输入的字符数的函数定义如下: void MainWindow::countChar() { QString tempString = ui->displayLabel->text()
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();
}
我在main.cpp的对象w下调用这个函数
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”更改为任何值
编辑:用户界面文件:
<?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>
如果要计算文本,则必须在每次文本更改时进行计数,并且必须使用
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"
#包括
#包括
#包括
#包括
#包括
类CounterWidget:publicqwidget
{
Q_对象
Q_属性(QString text READ text WRITE setText NOTIFY textChanged)
Q_属性(int maxLenght READ maxLenght WRITE setMaxLenght)
Q_属性(整数长度读取长度)
公众:
CounterWidget(QWidget*parent=nullptr):
CounterWidget(200,父级)
{
}
计数器小部件(int maxLength,QWidget*parent=nullptr):
QWidget(母公司),
布局(本)
{
layout.addWidget(&lineEdit);
layout.addWidget(&counterLabel,0,Qt::AlignTop | Qt::AlignRight);
连接(&lineEdit、&QLineEdit::textChanged、this和CounterWidget::countChar);
连接(&lineEdit、&QLineEdit::textChanged、this和CounterWidget::textChanged);
lineEdit.setMaxLength(maxLength);
countChar(“”);
}
QString text()常量{
返回lineEdit.text();
}
void setText(常量QString和text){
lineEdit.setText(文本);
}
int maxLenght()常量{
返回lineEdit.maxLength();
}
无效设置最大长度(整数最大长度){
lineEdit.setMaxLength(MaxLength);
}
int length()常量{
返回lineEdit.text().size();
}
信号:
无效文本已更改(常量字符串和文本);
专用插槽:
void countChar(常量q字符串和文本){
QString text_label=QString(“%1/%2”).arg(text.size()).arg(lineEdit.maxLength());
counterLabel.setText(文本标签);
}
私人:
QVBox布局;
QLineEdit-lineEdit;
QLabel标签;
};
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
计数器w;
w、 show();
返回a.exec();
}
#包括“main.moc”
我是在读QLabel的文本,而不是QLineEdit。因此,我的代码中的部分:
QString tempString = ui->displayLabel->text();
当我想读取在QLineEdit中输入的文本时,我只是从标签中读取文本。因此,我将代码更改为:
QString tempString = ui->inputText->text();
这就解决了问题。
谢谢@FrozenM和@eyllanesc您的信号/插槽连接在哪里?看起来您想在这个字符串QString tempString=ui->displayLabel->text()中计算qlineedit,而不是qlabel;刚刚添加了我的信号插槽连接。“ui->displayLabel->text();错误。这解决了问题。谢谢,刚刚解决了问题。现在效果很好。谢谢,我刚刚添加了textChanged()信号,程序仍然不会计算输入QLineEdit的字符数。您可以看到信号槽连接。我也在参考您的示例。@IneedHelp经常更改
QString tempString=ui->displayLabel->text();
为QString tempString=ui->inputText->text();
。您计算的是QLabel文本,而不是QLineEdit。
QString tempString = ui->inputText->text();