QFileDialog自定义样式设置SetStytlesheet

 

void QStockView::SlotOutputExcel()
{
	QFileDialog fileDilog;
	fileDilog.setOption(QFileDialog::DontUseNativeDialog, true);
	const QString g_FileDialogQSS =
		" .QFileDialog                                    "
		" {                                              "
		"       background-color: #151515;              "
		"       color: #FFFFFF;                         "
		" }                                              "
		" .QListView                                      "
		" {                                              "
		"       background-color: #151515;               "
		"       color: #FFFFFF;                          "
		" }                                              "
		" .QListView::item                               "
		" {                                              "
		"       background-color: #151515;               "
		"       color: #FFFFFF;                          "
		" }                                              "
		" .QComboBox QAbstractItemView                   "
		" {                                              "
		"       background-color: #151515;               "
		"       color: #FFFFFF;                          "
		" }                                              "
		" .QComboBox                                     "
		" {                                              "
		"       background-color: #151515;              "
		"       color: #FFFFFF;                          "
		" }                                              "
		" .QComboBox QAbstractItemView ::item            "
		" {                                              "
		"       background-color: #151515;              "
		"       color: #FFFFFF;                          "
		" }                                              "
		" .QLineEdit                                     "
		" {                                              "
		"       background-color: #151515;              "
		"       color: #FFFFFF;                          "
		" }                                              "
		" .QFrame                                         "
		" {                                              "
		"       background-color: #151515;               "
		"       color: #FFFFFF;                          "
		" }                                              "
		" .QSidebar                                      "
		" {                                              "
		"       background-color: #151515;               "
		"       color: #FFFFFF;                          "
		" }                                              "
		" .QLabel                                         "
		" {                                              "
		"       background-color: #151515;               "
		"       color: #FFFFFF;                          "
		" }                                              "
		" .QTreeView#treeView                             "
		" {                                              "
		"       background-color: #151515;              "
		"       color: #FFFFFF;                          "
		" }                                              "
		" .QPushButton                                   "
		" {                                              "
		"       background-color: #151515;               "
		"       border: 1px solid #414141;               "
		"       border-radius: 3px;                      "
		"       color: #FFFFFF;                          "
		" }                                              ";
	
	fileDilog.setStyleSheet(g_FileDialogQSS);
	fileDilog.setAcceptMode(QFileDialog::AcceptSave);
	fileDilog.setLabelText(QFileDialog::Accept, tr("保存"));   // 设置对应控件的文字
	fileDilog.setLabelText(QFileDialog::LookIn, (u8"查看"));   // 设置对应控件的文字
	fileDilog.setLabelText(QFileDialog::FileName, (u8"文件名"));   // 设置对应控件的文字
	fileDilog.setLabelText(QFileDialog::FileType, (u8"文件类型"));   // 设置对应控件的文字
	fileDilog.setLabelText(QFileDialog::Reject, (u8"取消"));   // 设置对应控件的文字
	fileDilog.setWindowTitle(QString(u8"保存数据到Excel"));
	fileDilog.setNameFilter(tr(u8"file(*.xlsx)"));
	if (fileDilog.exec() != QDialog::Accepted)
	{
		return;
	}
	//	HRESULT retval = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);和这个冲突无法打开选择框
	//QString filePath = fileDilog.getSaveFileName(NULL, tr("保存到Excel"), m_defaultPath, tr("*.xlsx"),nullptr, QFileDialog::DontUseNativeDialog);
	QStringList selectfiles = fileDilog.selectedFiles();
	if (selectfiles.size() == 0)
	{
		return;
	}
	QString filePath = selectfiles[0];
	//!!!判空处理
	if (filePath.isEmpty())
		return;
	m_defaultPath = QFileInfo(filePath).absoluteDir().absolutePath();
	QAxObject excel("Excel.Application");
	//!!!获取工作簿集合
	QAxObject*workbooks = excel.querySubObject("WorkBooks");
	//!!!新建工作簿
	workbooks->querySubObject("Add");
	//!!!获取当前工作簿
	QAxObject*workbook = excel.querySubObject("ActiveWorkBook");
	if (workbook == nullptr)
		return;
	//!!!获取表页对象
	QAxObject*worksheets = workbook->querySubObject("WorkSheets");
	//!!!获取第一页
	QAxObject*worksheet = worksheets->querySubObject("Item(int)", 1);
	//!!!获取权限
	QAxObject*usedrange = worksheet->querySubObject("Usedrange");
	//先保存表头
	QString headItem = "";
	for (int j = 1; j < ui.tableWidgetHis->columnCount(); j++)
	{
		headItem = ui.tableWidgetHis->horizontalHeaderItem(j)->text();
		worksheet->querySubObject("Cells(int, int)", 1, j )->dynamicCall("setValue(const QVariant&)", headItem.toStdString().c_str());
	}
	//!!!遍历获取数据将数据写入文件
	for (int i = 0; i < ui.tableWidgetHis->rowCount(); i++)
	{
		QTableWidgetItem* pCheck = ui.tableWidgetHis->item(i, 0);
		if (pCheck!=NULL)
		{
			if (pCheck->checkState()==Qt::Checked)
			{
				for (int j = 1; j < ui.tableWidgetHis->columnCount(); j++)
				{				
					//worksheet->querySubObject("Cells(int, int)", i + 1, j+1)->dynamicCall("SetNumberFormatLocal(const QVariant&)", "@");
					worksheet->querySubObject("Cells(int, int)", i + 2, j )->dynamicCall("setValue(const QVariant&)", ui.tableWidgetHis->item(i, j)->text().toStdString().c_str());
				}
			}
		}
	}
	//!!!保存文件
	workbook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(filePath));
	workbooks->dynamicCall("Close()");
	excel.dynamicCall("Quit()");
	WaringDialog diag("提示", "保存数据到Excel成功!");
	diag.setPicType(0);
	diag.exec();

}

  

void QStockView::SlotOutputExcel(){QFileDialog fileDilog;fileDilog.setOption(QFileDialog::DontUseNativeDialog, true);const QString g_FileDialogQSS =" .QFileDialog                                    "" {                                              ""       background-color: #151515;              ""       color: #FFFFFF;                         "" }                                              "" .QListView                                      "" {                                              ""       background-color: #151515;               ""       color: #FFFFFF;                          "" }                                              "" .QListView::item                               "" {                                              ""       background-color: #151515;               ""       color: #FFFFFF;                          "" }                                              "" .QComboBox QAbstractItemView                   "" {                                              ""       background-color: #151515;               ""       color: #FFFFFF;                          "" }                                              "" .QComboBox                                     "" {                                              ""       background-color: #151515;              ""       color: #FFFFFF;                          "" }                                              "" .QComboBox QAbstractItemView ::item            "" {                                              ""       background-color: #151515;              ""       color: #FFFFFF;                          "" }                                              "" .QLineEdit                                     "" {                                              ""       background-color: #151515;              ""       color: #FFFFFF;                          "" }                                              "" .QFrame                                         "" {                                              ""       background-color: #151515;               ""       color: #FFFFFF;                          "" }                                              "" .QSidebar                                      "" {                                              ""       background-color: #151515;               ""       color: #FFFFFF;                          "" }                                              "" .QLabel                                         "" {                                              ""       background-color: #151515;               ""       color: #FFFFFF;                          "" }                                              "" .QTreeView#treeView                             "" {                                              ""       background-color: #151515;              ""       color: #FFFFFF;                          "" }                                              "" .QPushButton                                   "" {                                              ""       background-color: #151515;               ""       border: 1px solid #414141;               ""       border-radius: 3px;                      ""       color: #FFFFFF;                          "" }                                              ";fileDilog.setStyleSheet(g_FileDialogQSS);fileDilog.setAcceptMode(QFileDialog::AcceptSave);fileDilog.setLabelText(QFileDialog::Accept, tr("保存"));   // 设置对应控件的文字fileDilog.setLabelText(QFileDialog::LookIn, (u8"查看"));   // 设置对应控件的文字fileDilog.setLabelText(QFileDialog::FileName, (u8"文件名"));   // 设置对应控件的文字fileDilog.setLabelText(QFileDialog::FileType, (u8"文件类型"));   // 设置对应控件的文字fileDilog.setLabelText(QFileDialog::Reject, (u8"取消"));   // 设置对应控件的文字fileDilog.setWindowTitle(QString(u8"保存数据到Excel"));fileDilog.setNameFilter(tr(u8"file(*.xlsx)"));if (fileDilog.exec() != QDialog::Accepted){return;}//HRESULT retval = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);和这个冲突无法打开选择框//QString filePath = fileDilog.getSaveFileName(NULL, tr("保存到Excel"), m_defaultPath, tr("*.xlsx"),nullptr, QFileDialog::DontUseNativeDialog);QStringList selectfiles = fileDilog.selectedFiles();if (selectfiles.size() == 0){return;}QString filePath = selectfiles[0];//!!!判空处理if (filePath.isEmpty())return;m_defaultPath = QFileInfo(filePath).absoluteDir().absolutePath();QAxObject excel("Excel.Application");//!!!获取工作簿集合QAxObject*workbooks = excel.querySubObject("WorkBooks");//!!!新建工作簿workbooks->querySubObject("Add");//!!!获取当前工作簿QAxObject*workbook = excel.querySubObject("ActiveWorkBook");if (workbook == nullptr)return;//!!!获取表页对象QAxObject*worksheets = workbook->querySubObject("WorkSheets");//!!!获取第一页QAxObject*worksheet = worksheets->querySubObject("Item(int)", 1);//!!!获取权限QAxObject*usedrange = worksheet->querySubObject("Usedrange");//先保存表头QString headItem = "";for (int j = 1; j < ui.tableWidgetHis->columnCount(); j++){headItem = ui.tableWidgetHis->horizontalHeaderItem(j)->text();worksheet->querySubObject("Cells(int, int)", 1, j )->dynamicCall("setValue(const QVariant&)", headItem.toStdString().c_str());}//!!!遍历获取数据将数据写入文件for (int i = 0; i < ui.tableWidgetHis->rowCount(); i++){QTableWidgetItem* pCheck = ui.tableWidgetHis->item(i, 0);if (pCheck!=NULL){if (pCheck->checkState()==Qt::Checked){for (int j = 1; j < ui.tableWidgetHis->columnCount(); j++){//worksheet->querySubObject("Cells(int, int)", i + 1, j+1)->dynamicCall("SetNumberFormatLocal(const QVariant&)", "@");worksheet->querySubObject("Cells(int, int)", i + 2, j )->dynamicCall("setValue(const QVariant&)", ui.tableWidgetHis->item(i, j)->text().toStdString().c_str());}}}}//!!!保存文件workbook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(filePath));workbooks->dynamicCall("Close()");excel.dynamicCall("Quit()");WaringDialog diag("提示", "保存数据到Excel成功!");diag.setPicType(0);diag.exec();
}
posted @ 2024-04-04 22:44  一字千金  阅读(254)  评论(0编辑  收藏  举报