QT实验分析教程 Qt中的字符串类,QString(9)
Qt中的字符串类
采用Unicode编码。
QString直接支持字符串和数字的相互转换
QString直接支持字符串的大小比较
QString直接支持不同字符编码间的相互转换
QString直接支持std::string和std::wstring的相互转换
QString直接支持正则表达式的应用
QString的函数
String & QString::append ( const QString & str )
将字符串str追加到此字符串的末尾。
QString & QString::prepend ( const QString & str )
将字符串str前置到此字符串的开头,并返回对此字符串的引用。
QString & QString::replace ( const QString & before, const QString & after, Qt::CaseSensitivity cs = Qt::CaseSensitive )
将前面的每个字符串替换为后面的字符串,并返回对此字符串的引用。
QString & QString::sprintf ( const char * cformat, ... )
从格式字符串cformat和任意参数列表安全地构建格式化字符串。
int QString::indexOf ( const QLatin1String & str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
返回字符串str在该字符串中第一次出现的索引位置,从中向前搜索。如果找不到str,则返回-1。
QString QString::mid ( int position, int n = -1 ) const
返回一个子字符串,该字符串包含n个字符,从指定的位置索引开始。
QString QString::trimmed () const
返回从开头和结尾删除空白的字符串。
int QString::length () const
返回此字符串中的字符数。相当于size()。
QString & QString::remove ( int position, int n )
从给定位置索引开始,从字符串中删除n个字符,并返回对该字符串的引用。
QLineEdit
void setText ( const QString & )
设置字符串
QString text () const
得到字符串
void setAlignment ( Qt::Alignment flag )
此属性保留行编辑的对齐方式。
QObject
QObject * QObject::sender () const [protected]
如果在由信号激活的槽中调用,则返回指向发送信号的对象的指针;否则返回0。指针仅在执行从该对象的线程上下文调用该函数的槽期间有效。
示例程序:
#include <QtCore/QCoreApplication>
#include <QDebug>
void Sample_1()
{
//字符串变量s
QString s = "add";
//追加append
s.append(" "); //"add "
s.append("Qt"); //"add Qt"
//前面插入prepend
s.prepend(" "); //" add Qt"
s.prepend("C++"); //"C++ add Qt"
qDebug() << s;
//替换replace,将字符串s的add替换为&
s.replace("add", "&"); //“C++ & Qt”
qDebug() << s;
}
void Sample_2()
{
QString s = "";
int index = 0;
//格式化
s.sprintf("%d. I'm %s, thank you!", 1, "Delphi Tang");// 1. I'm Delphi Tang, thank you!
qDebug() << s;
//,的下标是多少
index = s.indexOf(",");
//取子串
s = s.mid(0, index);//"1. I'm Delphi Tang"
qDebug() << s;
//.的位置
index = s.indexOf(".");
//提取子串
s = s.mid(index + 1, s.length()); //" I'm Delphi Tang"
//将字符串前后空格去掉
s = s.trimmed(); //"I'm Delphi Tang"
qDebug() << s;
index = s.indexOf(" ");
s = s.mid(index + 1, s.length()); //"Delphi Tang"
s = s.trimmed();
qDebug() << s;
}
void Sample_3(QString* a, int len)
{
//选择排序
for(int i = 0; i < len; i++)
{
for(int j = i + 1; j < len; j++)
{
if (a[j] < a[i])
{
QString tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Sample_1:";
Sample_1();
qDebug() << endl;
qDebug() << "Sample_2:";
Sample_2();
qDebug() << endl;
qDebug() << "Sample_3:";
QString company[5] =
{
QString("Oracle"),
QString("Borland"),
QString("Microsoft"),
QString("IBM"),
QString("D.T.Software"),
};
Sample_3(company, 5);
for(int i = 0; i < 5; i++)
{
qDebug() << company[i];
}
return a.exec();
}
结果如下:

为计算器示例添加消息响应
关键代码:
//按键消息处理函数
void QCalculatorUI::onButtonClicked()
{
//得到被点击的按钮指针
QPushButton* btn = (QPushButton*)sender();
QString clickText = btn->text();
//回退
if(clickText == "<-")
{
QString text = m_edit->text();
if(text.length() > 0)
{
//删除
text.remove(text.length()-1, 1);
m_edit->setText(text);
}
}
//清空
else if(clickText == "C")
{
m_edit->setText("");
}
else if(clickText == "=")
{
}
else
{
m_edit->setText(m_edit->text() + clickText);
}
}
完整代码:
QCalculatorUI.h
#ifndef _QCALCULATORUI_H_
#define _QCALCULATORUI_H_
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
class QCalculatorUI : public QWidget
{
Q_OBJECT
private:
QLineEdit* m_edit;
QPushButton* m_buttons[20];
QCalculatorUI();
bool construct();
private slots:
void onButtonClicked();
public:
static QCalculatorUI* NewInstance();
void show();
~QCalculatorUI();
};
#endif
QCalculatorUI.cpp
#include "QCalculatorUI.h"
#include <QDebug>
QCalculatorUI::QCalculatorUI() : QWidget(NULL, Qt::WindowCloseButtonHint)
{
}
bool QCalculatorUI::construct()
{
bool ret = true;
const char* btnText[20] =
{
"7", "8", "9", "+", "(",
"4", "5", "6", "-", ")",
"1", "2", "3", "*", "<-",
"0", ".", "=", "/", "C",
};
m_edit = new QLineEdit(this);
if (m_edit != NULL)
{
m_edit->move(10, 10);
m_edit->resize(240, 30);
m_edit->setReadOnly(true);//文本框只能读,不能写
m_edit->setAlignment(Qt::AlignRight);
}
else
{
ret = false;
}
for(int i = 0; (i < 4) && ret; i++)
{
for(int j = 0; (j < 5) && ret; j++)
{
m_buttons[i * 5 + j] = new QPushButton(this);
if(m_buttons[i * 5 + j] != NULL)
{
m_buttons[i * 5 + j]->resize(40, 40);
m_buttons[i * 5 + j]->move(10 + (10 + 40) * j, 50 + (10 + 40) * i);
m_buttons[i * 5 + j]->setText(btnText[i * 5 + j]);
connect(m_buttons[i * 5 + j], SIGNAL(clicked()), this, SLOT(onButtonClicked()));
}
else
{
ret = false;
}
}
}
return ret;
}
QCalculatorUI* QCalculatorUI::NewInstance()
{
QCalculatorUI* ret = new QCalculatorUI();
//如果资源申请失败
if ((ret == NULL) || !ret->construct())
{
delete ret;
ret = NULL;
}
return ret;
}
void QCalculatorUI::show()
{
QWidget::show();
setFixedSize(width(), height());//固定大小
}
//按键消息处理函数
void QCalculatorUI::onButtonClicked()
{
//得到被点击的按钮指针
QPushButton* btn = (QPushButton*)sender();
QString clickText = btn->text();
//回退
if(clickText == "<-")
{
QString text = m_edit->text();
if(text.length() > 0)
{
//删除
text.remove(text.length()-1, 1);
m_edit->setText(text);
}
}
//清空
else if(clickText == "C")
{
m_edit->setText("");
}
else if(clickText == "=")
{
}
else
{
m_edit->setText(m_edit->text() + clickText);
}
}
QCalculatorUI::~QCalculatorUI()
{
}
main.cpp
#include <QtGui/QApplication>
#include "QCalculatorUI.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCalculatorUI* cal = QCalculatorUI::NewInstance();
int ret = -1;
if (cal != NULL)
{
cal->show();
ret = a.exec();
delete cal;
}
return ret;
}
结果如下:

1434

被折叠的 条评论
为什么被折叠?



