
长了脑子是要用的。
打开QtCreator的帮助页面,找到
Qt Reference Documentation ---》Classes--》QLineEdit Class Reference
看到QLineEdit的帮助文档,
Signals
void cursorPositionChanged ( int old, int new )
void editingFinished ()
void returnPressed ()
void selectionChanged ()
void textChanged ( const QString & text )
void textEdited ( const QString & text )
textEdited()是一个信号,不是函数,不能干任何事,这是用于触发其他槽函数的。
获取文本的函数:
Public Functions
QString text () const
获取文本用lineEdit->text() ;
这么简单都不用脑子?
Access functions:访问文本的函数
QString text () const
void setText ( const QString & )
Notifier signal:通知信号
void textChanged ( const QString & text )
每一个ui都对应一个ui对象,然后添加一个ui对象到你的类里面,然后在类里面就可以使用muilineedittext。不过这个文本框的名字需要你在designer里面查看一下类名是什么。
QT中提取QTextEdit文本框中的内容,代码如下
int a[100];
QString str = ui->textEdit->toPlainText();
int len = strlength();
for(int i = 0; i < len; ++i)
{
QChar t = strat(i);
a[i] = ttoAscii() - '0';
}
'textEdit的数据合理性以及数组大小自己去设定
自定义信号和槽
signals:
SendText(QString text);
privite slots:
ReciveText(QString text);
界面类的构造函数中connect信号和槽。
按钮的点击处理函数中获取lineEdit的内容,并作为信号SendText的参数。
然后发射信号 emit SendText(text);
槽函数SendText中 *** 作文本编辑器。
用QT查找字符串并标记要查找的内容,使用以下代码即可实现:
QString searchString = ui->lineEdit_2->text();QTextDocument document = ui->description->document();
ui->description->setHtml(ui->description->document()->toPlainText());
int number=0;
bool found = false;
QTextCursor highlightCursor(document);
QTextCharFormat plainFormat(highlightCursorcharFormat());
QTextCharFormat colorFormat = plainFormat;
colorFormatsetForeground(Qt::red);
if(ui->description->toPlainText()==""){
QMessageBox::information(this, tr("description first"),
"Sorry, please display the description first!");
}
else{
ui->result->setPlainText("");
QString resultstring="搜索结果:";
QString laststring;
/while循环体是本代码的关键-——开始——/
while (!highlightCursorisNull() && !highlightCursoratEnd()) {
if(ui->daxiaocheckBox->isChecked()==true){
highlightCursor = document->find(searchString, highlightCursor,QTextDocument::FindCaseSensitively);
}//这个是实现大小写区分效果的代码,你可能不需要
else
highlightCursor = document->find(searchString, highlightCursor);
if (!highlightCursorisNull()) {
number++;
found = true;
highlightCursormovePosition(QTextCursor::Right,QTextCursor::KeepAnchor,0);
highlightCursormergeCharFormat(colorFormat);
laststring=QString::number(highlightCursorposition(),10);
if(ui->onlyTwo->isChecked()==true){
if(number<=1)
resultstring+="n occurrence"+QString::number(number,10)+":—— position:"+QString::number(highlightCursorposition(),10);
}//这个是实现大小写区分效果的代码,你可能不需要
else
resultstring+="n occurrence"+QString::number(number,10)+":—— position:"+QString::number(highlightCursorposition(),10);
}
}
/关键代码结束/
if(number>1&&ui->onlyTwo->isChecked()==true)
resultstring+="n occurrence"+QString::number(number,10)+":—— position:"+laststring;
ui->result->setPlainText(resultstring);
if (found == false) {
QMessageBox::information(this, tr("Word Not Found"),
"Sorry, the word cannot be found");
}
else {
QMessageBox::information(this, tr("Word was Found"),
"the word '"+searchString+"' was found for "+QString::number(number,10)+((number>1) " times":" time"));
}
}
由于qt返回的是Unicode编码,譬如你在LineEdit中直接输入中文,返回的就是,qt中能直接使用QTextCodec来转换字符串的编码
QString string;
string=LineEdit1->text(); //取得LineEdit1返回的文字
QTextCodec codec=QTextCodec::codecForName("GBK");
以上就是关于qt中怎样将lineEdit的内容写入文件并保存全部的内容,包括:qt中怎样将lineEdit的内容写入文件并保存、Qt如何获得文本框内容、C++ qt如何获取编辑框内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)