【发布时间】:2018-09-23 05:48:27
【问题描述】:
我正在尝试使用 QT 中的 QLineEdit 功能制作一个简单的字符计数器,就像 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);
}
【问题讨论】:
-
您的信号/插槽连接在哪里?
-
看起来你想在这个字符串中计算 qlineedit,而不是 qlabel QString tempString = ui->displayLabel->text();
-
刚刚添加了我的信号槽连接。 “ui->displayLabel->text(); 错误。这解决了问题。谢谢
-
是的,刚刚解决了这个问题。现在就像一个魅力。谢谢