QT学习笔记若干

1、QString的arg方法

在QT的QString中,arg方法类似于C中的printf中使用的格式输出符(只是有点类似)。
在QT5的帮助文档中,可以看出以下几点:
  1.使用arg(str1, str2, str3)这种方法进行替换。
  2.使用arg(str1).arg(str2).arg(str3)这种方法进行替换。
  3.​使用arg(int, int, int)这种方式进行替换。
解释以下两种方法:

1.1 使用arg(str1, str2, str3)

这种方法要求str1、str2、str3都为const QString &类型,即:

QString QString::arg(const QString & a1, const QString & a2, const QString & a3) const

同时,arg( )里的参数实现从1个到9个,也就是说最多只能有9个!即在实现中,arg的定义中参数最多的为:
···
QString QString::arg(const QString & a1, const QString & a2, const QString & a3, const QString & a4, const QString & a5, const QString & a6, const QString & a7, const QString & a8, const QString & a9) const
···
  这也就意味着,如果有如下代码:

QString str = QString("%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11").
            arg("1","2","3","4","5","6","7","8","9")

str将只会输出:

"1 2 3 4 5 6 7 8 9 %10 %11"

解决方法为在后面再加一个arg方法,放上后面两个对应的参数:

QString str = QString("%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11").
            arg("1","2","3","4","w","6","7","8","9").arg("a","b");
//输出为:
// "1 2 3 4 5 6 7 8 9 a b"

注意:在QT5帮助文档中,给出了如下代码:

QString str;
str = "%1 %2";
str.arg("%1f", "Hello");        // returns "%1f Hello"
str.arg("%1f").arg("Hello");    // returns "Hellof %2"

如果你copy这个代码去测试一下,会发现,输出结果还是:"%1 %2" 。根本没有其给出的结果。(没找到源码,有谁知道怎么查看QT的源码,求帮助!说找src文件夹的就不要来了,根本木有)
  所以在使用的时候一定要注意一下。要紧跟字符串使用arg( ) 。

1.2 使用arg(str1).arg(str2).arg(str3)

这种方法其实前面用到了,如下面例子:

QString str=QString("%1 %2 %3 %4").arg("A").arg("B").arg("C").arg("D");
//str=="A B C D"

简单说就是挨着替换呗。但请注意下面的形式:

str = QString("%1 %2").arg("%1World", "Hello");
qDebug()<<str;
//输出为:"%1World Hello"
 
str = QString("%1 %2").arg("%1World").arg("Hello");
qDebug()<<str;
//输出为:"HelloWorld %2"
//第一个arg执行完后变为:QString("%1World %2").arg("Hello")
//再次执行后"Hello"替换的为%1

所以在使用多个arg( )连接时,一定要注意,前面连接使用的arg( )里如果有“%+数字”的情况,后面的arg( )会同样替换!
  请注意下列例子:

str = QString("%1 %2").arg("%1World").arg("Hello");
//输出:"HelloWorld %2"
str = QString("%1 %2").arg("%2World").arg("Hello");
//输出:"HelloWorld Hello"
str = QString("%1 %2").arg("%3World").arg("Hello");
//输出:"%3World Hello"
str = QString("%1 %2").arg("%8World").arg("Hello");
//输出:"%8World Hello"
str = QString("%1 %6").arg("%3World").arg("Hello");
//输出:"HelloWorld %6"
str = QString("%2 %6").arg("%3World").arg("Hello");
//输出:"HelloWorld %6"
str = QString("%0 %6").arg("%3World").arg("Hello");
//输出:"HelloWorld %6"
str = QString("%-1 %6").arg("%3World").arg("Hello");
//输出:"%-1 HelloWorld"

arg( )里的参数在进行替换时,只会按照数字从小到大的顺序进行替换,只有比当前数字小的所有数字都替换完成了,才会替换当前数字,否则将当前数字和%按字符串处理。且数字为自然数!
参考网址:https://www.cnblogs.com/lomper/p/4135387.html

1.3 重载方法
QString QString::​arg(int a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const

这个方法用于填充字符串中的%1,%2…为给定格式的整形数字,其中第一个参数是要填充的数字,第二个参数为最小宽度,第二个参数为进制,第四个参数为当原始数字长度不足最小宽度时用于填充的字符。
  如:

// text = "00123:00456"
QString text = QString("%1:%2").arg(123, 5, 10, QChar('0')).arg(456, 5, 10, QChar('0')); 

参考网址:https://blog.youkuaiyun.com/fighting_youngman/article/details/70611052

2、connect连接的两种方式

qt Connect 函数是连接信号和槽函数的连接函数。现在有两种方式。

2.1 **connect(SenderObj, &SenderClass::signalMethod, ReceiveObj, &ReceiveClass::slotMethod)

这种方式不要求slot函数必须是槽函数,可以是private,也可以是public类型。
  假如signalMethod 与slotMethod(int a) 有参数,在connect中则比不必写参数,只用写函数名即可。

connect(ui->pushButton, &QPushButton::clicked,this, &MainWindow::clickkkk);
2.2 **connect(SenderObj, SIGNAL(signalMethod()), ReceiveObj, SLOT(slotMethod()))

格式必须统一,slotMethod()必须定义为public slots: 或者private slots: 槽函数。如果slotMethod(int arg),带有参数,则在connect函数里,也必须带上参数。

 connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(clickkkk()));
2.3 直接嵌入匿名函数
connect(ui->pushButton, &QPushButton::clicked, this, [=](){ //匿名表达式可以直接作为槽函数
        ui->label->setText("112");

参考网址:https://blog.youkuaiyun.com/xiezhongyuan07/article/details/79247022

2.4 Could not find or load the Qt platform plugin “windows”
The error is caused because the program can't find qwindows.dll
qwindows.dll has to be in a folder named platforms so that the path from your executable to the dll is platforms/qwindows.dll
Whereas this wasn't enough in my case. I had also to add following line at the beginning of my main()
QCoreApplication::addLibraryPath("./");
Then everything worked.

参考网址:https://blog.youkuaiyun.com/jh1513/article/details/52262188/
参考网址:https://stackoverflow.com/questions/25012347/qt-5-3-1-application-error-could-not-find-or-load-the-qt-platform-plugin-windo
参考网址:https://stackoverflow.com/questions/21268558/application-failed-to-start-because-it-could-not-find-or-load-the-qt-platform-pl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值