一.Qt中的字符串类
QString vs string:
QString在Qt库中几乎是无所不在的
所有的Qt图形用户组件都依赖于QString
实验1 :QString 初体验
#include <QDebug>
void Sample_1()
{
QString s = "add";
s.append(" "); // "add "
s.append("Qt"); // "add Qt"
s.prepend(" "); // " add Qt"
s.prepend("C++"); // "C++ add Qt"
qDebug() << s;
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"
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()
{
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 0;
}
运行结果:
实验2:为计算器实例添加消息响应–获取按键输入回显到QLineEdit 文本框
目录:
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 <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;
}