1.获取QML程序的依赖库需要指明qml库的路径,如:
windeployqt.exe test.exe --qmldir d:\Qt\Qt5.6.0\5.6\msvc2013\qml
2.在信号和槽中使用自定义的数据类型时,需要向元对象系统注册,如:
qRegisterMetaType<MyClass>("MyClass");
3.在打印日志信息的时候,加上"__FUNCTION__"这个宏,用以打印出日志信息所在类及函数的信息,如:
void Widget::outputLog()
{
qDebug() << __FUNCTION__ << "this is test log";
}
//调用outputLog()函数,输出Widget::outputLog this is test log
4.QLabel可以设置多行显示,只要设置setWordWrap(true),如:
QLabel* pLblDisplay = new QLabel(this);
pLblDisplay->setWordWrap(true);
5.使用HTTP通讯时,打印返回的HTTP状态码,如:
qDebug() << __FUNCTION__ << m_pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
6.如要把QtQuick窗口嵌入QtWdigets中,要在.pro文件中添加quickwidgets模块,如:
QT += quickwidgets
7.QStringList的removeDuplicates()函数可以用来去除重复的项,如:
QStringList lst;
lst << "test" << "test" << "Test" << "TEST";
qDebug() << lst;
lst.removeDuplicates();
qDebug() << lst;
结果:
8.QString分离成QStringList时,去掉空白的项,如:
QString str = QString("One,Two,Three,,Five");
QStringList lstHaveEmpty = str.split(",");
qDebug() << "size is" << lstHaveEmpty.size() << lstHaveEmpty;
QStringList lstNoneEmpty = str.split(",", QString::SkipEmptyParts);
qDebug() << "size is" << lstNoneEmpty.size() << lstNoneEmpty;
结果:
9.获取Windows系统启动时间,如:
#include <Windows.h>
QDateTime bootTime = QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t() - GetTickCount64() / 1000);
10.为程序设置图标,在.pro文件中添加如下语句:
RC_ICONS = xxx.ico
注意:.pro文件所在目录要存在同名文件。
11.给QPushButton添加菜单(SetMenu())后,会默认显示下拉箭头,去掉的方法就是在样式表增加下面语句:
QPushButton::menu-indicator{image:none;}
效果如图:
注意:此时文本的对齐方式是不计算下拉箭头所占位置的,所以即使是设置居中对齐,看起来也是偏左的。