一、qt中QString类
功能:显示一个字符串内容
常用的接口函数
1)构造函数
QString(const char *str) ------> QString str = QString("hello");
QString(const QString &other) ----> QString str2 = str;
QString & | operator=(const QString &other) |
比如:QString st3;st3 = str2;
2)功能函数
基本数据类型(int char double ...)转换成字符串
- number(long , int ) : QString
- number(int , int ) : QString
- number(uint , int ) : QString
- number(ulong , int ) : QString
- number(qlonglong , int ) : QString
- number(qulonglong , int ) : QString
- number(double , char , int ) : QString
比如:
[static] QString QString::number(int n, int base = 10)
所以,这些函数都是静态成员函数
参数说明:
int n ----- 》 基本数据类型的值
int base ---》进制,默认为十进制
返回值:QString
字符串转换成基本数据类型
- toInt(bool *, int ) const : int
- toUInt(bool *, int ) const : uint
- toULong(bool *, int ) const : ulong
- toULongLong(bool *, int ) const : qulonglong
- toUShort(bool *, int ) const : ushort
ushort QString::toUShort(bool *ok = nullptr, int base = 10) const
以上这些函数是成员函数
参数说明:
bool *ok = nullptr ----》转换结果输出
int base ----》进制,默认为十进制
返回值:
基本数据类型
比如:
QString str = "FF";
bool ok;
ushort hex = str.toUShort(&ok, 16); // hex == 255, ok == true
ushort dec = str.toUShort(&ok, 10); // dec == 0, ok == false
QString与c++中 string之间转换
在将C++的项目移植到QT平台上,那么就是使用它们来兼容
- fromStdString(const std::string &) : QString -----> string ---->QString
函数 原型:
[static] QString QString::fromStdString(const std::string &str)---》静态成员函数
- toStdString() const : std::string ----> QString ---->string
std::string QString::toStdString() const ----> 普通成员函数
案例分析:将C++中实现的学生模型(类),将学生信息更新到UI控件
//初始化学生信息 Student s;
s.setName("zhang3");
s.setAge(20);
s.setSex('W');
//s.name() ---- string string --->QString
ui->nameLabel->setText(QString::fromStdString(s.name())); //string --->QString
ui->ageLabel->setText(QString::number(s.age())); //int ---> QString
ui->sexLabel->setText(QString::number(s.sex())); //char --->QString
arg函数:
给一个字符串可以设置一个参数值,相当于C语言字符串格式函数--printf,它也是通过占位符来实现
- arg(const QString &, int , QChar ) const : QString
- arg(qlonglong , int , int , QChar ) const : QString
- arg(qulonglong , int , int , QChar ) const : QString
- arg(long , int , int , QChar ) const : QString
- arg(ulong , int , int , QChar ) const : QString
- arg(int , int , int , QChar ) const : QString
- arg(uint , int , int , QChar ) const : QString
- arg(short , int , int , QChar ) const : QString
- arg(ushort , int , int , QChar ) const : QString
- arg(double , int , char , int , QChar ) const : QString
- arg(char , int , QChar ) const : QString
。。。。。。
他们是普通成员函数
QString str;
str = QString("Decimal 63 is %1 in hexadecimal")
.arg(63, 0, 16); // str == "Decimal 63 is 3f in hexadecimal"
占位符:%n n=1, 2, ..., 99.
比如:当前下载的进度为50
QString str = QString("当前下载的进度为:%1").arg(50);
str ---> 当前下载的进度为:50
二 信号和槽(Signals & Slots)
在qt中,不同的两个对象之间的通信,可以使用信号和槽来完成,比如:两个不同界面
2.1 信号和槽的基本概念
信号和槽是qt中独有的机制,用于两个不同对象之间通信。跟其他框架不同,相对其他通信机制,类型更加安全。
信号 -----> 函数-----该函数 不需要实现
槽 ----> 函数 ----- 该函数需要实现
2.1 信号和槽使用
第一步:准备信号 --- 可以是QT中对象预定义,也可以自己增加的信号
第二步:槽---可以是QT中对象预定义的,也可以自己增加的槽
第三步:建立两个对象之间的信号和槽的关系--- connect函数,该函数的目的只是说明对象之间的信号和槽建立,在代码中,执行到该函数时,不会调用执行,只是告诉系统,两个对象之间可以进行通信
如何定义自己的信号:关键字(signals)
如何定义自己的槽:关键字(slots)
信号和槽使用的注意事项:该类必须直接或者间接继承QObject类,并且在类的最开始处要增加宏声明:Q_OBJECT
class Test:public QObject
{
Q_OBJECT
signals:
void test();
private /public slots:
void func();
};
void Test::func()
{
}
建立两个对象之间的信号和槽
connect(sender, &对象一类名::信号名, recvice, &对象二类名::槽函数名);
参数说明:
参数一:sender --- 信号发送者 -- 对象一 指针类型
参数二:&QObject::destroyed --- 对象一要发送的信号
写法:&对象一类名::信号名
参数三:recvice ---- 信号接收者---- 对象二 指针类型
参数四:&对象二类名::槽函数名
发送信号:emit
用法: emit 信号名(参数列表) -----> 相当于函数的调用
信号和槽案例:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_startButton_clicked();
void getData(int data);
signals:
void sendData(int data);
private:
Ui::MainWindow *ui;
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//建立了信号sendData和槽getData关系
connect(this,&MainWindow::sendData,this,&MainWindow::getData);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_startButton_clicked()
{
static int i = 0;
//当每次点击按钮时,发送一个数字,与它建立的槽函数
emit sendData(i);
i++;
}
void MainWindow::getData(int data)
{
ui->countLabel->setText(QString::number(data));
}