欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT 开发经验汇总(持续更新中)

程序员文章站 2022-07-08 17:21:55
...

断开某对信号槽的connect

QMetaObject::Connection conn;
conn = connect(this,&TcpSocket::readyRead,this,&TcpSocket::readData);
disconnect(conn);

查找本窗口的子控件

QList<QLabel*> labelList = findChildren<QLabel*>();
QLabel * label = findChild<QLabel *>("label_username");

获取各种路径

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QStandardPaths>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

	//获取程序所在路径
	QString applicationDirPath = QCoreApplication::applicationDirPath();
	qDebug() << "applicationDirPath=" << applicationDirPath;

	//程序的完整路径
	QString applicationFilePath = qApp->applicationFilePath();
	qDebug() << "applicationFilePath=" << applicationFilePath;

	//当前工作目录
	QString currentPath = QDir::currentPath();
	qDebug() << "currentPath=" << currentPath;

	//用户目录路径
	QString HomeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
	qDebug() << "HomeLocation=" << HomeLocation;

	QStringList HomeLocation2 = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
	qDebug() << "HomeLocation2=" << HomeLocation2[0];

	//我的文档路径
	QString DocumentsLocation = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
	qDebug() << "DocumentsLocation=" << DocumentsLocation;

	QStringList DocumentsLocation2 = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
	qDebug() << "DocumentsLocation2=" << DocumentsLocation2[0];

	//桌面路径
	QString DesktopLocation = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
	qDebug() << "DesktopLocation=" << DesktopLocation;

	QStringList DesktopLocation2 = QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
	qDebug() << "DesktopLocation2=" << DesktopLocation2[0];

	//程序数据存放路径
	QString AppDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
	qDebug() << "AppDataLocation=" << AppDataLocation;

	QStringList AppDataLocation2 = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
	qDebug() << "AppDataLocation2=" << AppDataLocation2[0];

	/*Qt5.5 中引入了另一种方法*/
	QString AppConfigLocation = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
	qDebug() << "AppConfigLocation=" << AppConfigLocation;

	QStringList AppConfigLocation2 = QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation);
	qDebug() << "AppConfigLocation2=" << AppConfigLocation2[0];

	//临时文件路径
	QString TempLocation = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
	qDebug() << "TempLocation=" << TempLocation;

	QStringList TempLocation2 = QStandardPaths::standardLocations(QStandardPaths::TempLocation);
	qDebug() << "TempLocation2=" << TempLocation2[0];

	//更传统的方法是利用QDir的一个静态函数tempPath()
	QString tempPath  = QDir::tempPath();
	qDebug() << "tempPath=" << tempPath;

	system("pause");
    return a.exec();
}

以上的输出结果:

applicationDirPath= "C:/Users/Administrator/Desktop/QtPathsTest/Debug"
applicationFilePath= "C:/Users/Administrator/Desktop/QtPathsTest/Debug/QtPathsTest.exe"
currentPath= "C:/Users/Administrator/Desktop/QtPathsTest/QtPathsTest"
HomeLocation= "C:/Users/Administrator"
HomeLocation2= "C:/Users/Administrator"
DocumentsLocation= "C:/Users/Administrator/Documents"
DocumentsLocation2= "C:/Users/Administrator/Documents"
DesktopLocation= "C:/Users/Administrator/Desktop"
DesktopLocation2= "C:/Users/Administrator/Desktop"
AppDataLocation= "C:/Users/Administrator/AppData/Roaming/QtPathsTest"
AppDataLocation2= "C:/Users/Administrator/AppData/Roaming/QtPathsTest"
AppConfigLocation= "C:/Users/Administrator/AppData/Local/QtPathsTest"
AppConfigLocation2= "C:/Users/Administrator/AppData/Local/QtPathsTest"
TempLocation= "C:/Users/Administrator/AppData/Local/Temp"
TempLocation2= "C:/Users/Administrator/AppData/Local/Temp"
tempPath= "C:/Users/Administrator/AppData/Local/Temp"

获取指定文件夹下所有文件

	// 获取所有文件名
	QDir dir(FolderPath);
	FolderPath= dir.fromNativeSeparators(FolderPath);//  "\\"转为"/" 
	if (!dir.exists()) mImgNames = QStringList("");
	dir.setFilter(QDir::Files);
	dir.setSorting(QDir::Name);
	dir.setNameFilters(QString("*.tiff;*.tif").split(";"));
	mImgNames = dir.entryList();
	for (int i = 0; i < mImgNames.size(); ++i)
	{
		qDebug() << "entryList: " << i << "-" << FolderPath+ "/" +mImgNames[i];
	}

获取指定文件夹下所有文件夹

	// 获取所有文件夹名
	QDir dir(FolderPath);
	mFolderPath = dir.fromNativeSeparators(FolderPath);//  "\\"转为"/" 
	if (!dir.exists()) mImgNames = QStringList("");
	dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
	dir.setSorting(QDir::Name);
	mImgNames = dir.entryList();
	for (int i = 0; i < mImgNames.size(); ++i)
	{
		qDebug() << "entryList: " << i << "-" << FolderPath + "/" +mImgNames[i];
	}