QFileDialog自定義樣式設定SetStytlesheet

一字千金發表於2024-04-04

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();
}

相關文章