最近Qt用到的技术简要总结

Posted by LuckXiang on April 29, 2018

一些比较零散的接口和操作,主要是excel,项目不便开源,简单记录一下。

	QApplication::setStyle(QStyleFactory::create("fusion"));//设置窗体风格
	w.setWindowFlags(w.windowFlags() &~Qt::WindowMaximizeButtonHint);//禁止最大化
	w.setFixedSize(w.width(), w.height());                     // 禁止拖动窗口大小 
    setWindowIcon(QIcon(":/auto_create_grouting_tools/icon")); //设置程序图标
    QString setting_file_path = QDir::toNativeSeparators(QCoreApplication::applicationDirPath() + "/setting.cfg"); //把路径转换成windows能识别的格式
/* 系统开始的时候创建excel链接*/
	excel = new QAxObject(this);
	excel->setControl("Excel.Application");
	excel->setProperty("DisplayAlerts", false);
	workbooks = excel->querySubObject("WorkBooks");


/* 新建一个表工作,操作完成以后把表关闭,也可以在这里去打开一个worksheet文件*/
	workbooks->dynamicCall("Add");
	QAxObject *workbook = excel->querySubObject("ActiveWorkBook");
	QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)", 1);
	excel_work_sheet_create(worksheet);
	excel->setProperty("Visible", true);
	worksheet->dynamicCall("PrintPreview");
	excel->setProperty("Visible", false);
	workbook->dynamicCall("Close(Boolean)", false);

/* 系统退出的时候退出excel*/
	excel->dynamicCall("Quit(void)");
	delete excel;

/* 一部分excel表格操作的代码,这些参数都是通过在excel里边录制宏,然后通过宏代码参考修改的*/
void auto_create_grouting_tools::excel_work_sheet_create(QAxObject *work_sheet)
{
	int line, row_count, comuln_count;
	QAxObject *cell;
	QAxObject *range;
	QAxObject *font;
	QList<QList<QVariant> > cells;
	QList<QVariant> rows;

	line = 1;
	if(ui.checkBox_report_name->isChecked())
	{ 
		range = work_sheet->querySubObject("Range(const QString&)", QString("A%1:L%2").arg(line).arg(line));
		range->setProperty("MergeCells", true);
		font = range->querySubObject("Font");
		font->setProperty("Bold", true); 
		//font->setProperty("Name", QStringLiteral("微软雅黑"));
		font->setProperty("Size", 22);  
		range->setProperty("WrapText", true);
		range->setProperty("RowHeight", 30);
		range->setProperty("HorizontalAlignment", -4108);//左对齐(xlLeft):-4131  居中(xlCenter):-4108  右对齐(xlRight):-4152  
		//range->setProperty("VerticalAlignment", -4108);  //上对齐(xlTop)-4160 居中(xlCenter):-4108  下对齐(xlBottom):-4107 
		range->setProperty("NumberFormatLocal", "@");
		range->setProperty("Value", ui.lineEdit_report_name->text());
		delete range;
		line = line + 2;

	}
}

/*excel快速读写,从excel_work_sheet_create函数中取出来的,用了Qt的模板类*/
	for (int i = 0; i<ui.tableWidget_report->model()->columnCount(); i++)
	{
		QString Stmp = ui.tableWidget_report->model()->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
		rows.append(Stmp);
	}
	cells.append(rows);
	range = work_sheet->querySubObject("Range(const QString&)", QString("A%1:L%2").arg(line).arg(line));//读取表头
	font = range->querySubObject("Font");
	font->setProperty("Size", 9);
	range->setProperty("WrapText", false);
	range->setProperty("RowHeight", 24);
	range->setProperty("HorizontalAlignment", -4108);
	range->setProperty("NumberFormatLocal", "@");
	range->setProperty("Value", castListListVariant2Variant(cells));
	rows.clear();
	cells.clear();
	delete range;
	line++;

	for (int row = 0; row < row_count; row++) //把一个tablet的数据先写到cells中,最后一次操作写到excel
	{
		for (int comuln = 0; comuln < comuln_count; comuln++)
		{
			rows.append(ui.tableWidget_report->item(row, comuln)->text());
		}
		cells.append(rows);
		rows.clear();
	}
	range = work_sheet->querySubObject("Range(const QString&)", QString("A%1:L%2").arg(line).arg(line + row_count - 1));
	range->setProperty("WrapText", false);
	range->setProperty("RowHeight", 16);
	range->setProperty("HorizontalAlignment", -4108);
	range->setProperty("NumberFormatLocal", "@");
	range->setProperty("Value", castListListVariant2Variant(cells));
	cells.clear();
	delete range;
	line = line + row_count;

/* 转换函数*/
QVariant auto_create_grouting_tools::castListListVariant2Variant(const QList<QList<QVariant> > &cells)
{
	QVariantList vars;
	const int rows = cells.size();
	for (int i = 0; i<rows; ++i)
	{
		vars.append(QVariant(cells[i]));
	}
	return QVariant(vars);
}

/* excel 打印预览*/
void auto_create_grouting_tools::slot_pushButton_preview_clicked()
{
	if (magic_number() < 0)
		return;
	workbooks->dynamicCall("Add");
	QAxObject *workbook = excel->querySubObject("ActiveWorkBook");
	QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)", 1);
	excel_work_sheet_create(worksheet);
	excel->setProperty("Visible", true);
	worksheet->dynamicCall("PrintPreview");
	excel->setProperty("Visible", false);
	workbook->dynamicCall("Close(Boolean)", false);
}

/* excel 打印,需要先在excel中设置好默认打印机*/
void auto_create_grouting_tools::slot_pushButton_print_clicked()
{
	if (magic_number() < 0)
		return;
	workbooks->dynamicCall("Add");
	QAxObject *workbook = excel->querySubObject("ActiveWorkBook");
	QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)", 1);
	excel_work_sheet_create(worksheet);
	excel->setProperty("Visible", true);
	worksheet->dynamicCall("PrintOut");
	excel->setProperty("Visible", false);
	workbook->dynamicCall("Close(Boolean)", false);
}

/* Qt5中最新的信号槽写法*/
	connect(ui.checkBox_user_key, &QCheckBox::stateChanged, this, &auto_create_grouting_tools::slot_checkBox_user_key_stateChanged);
	connect(ui.pushButton_user_key, &QPushButton::clicked, this, &auto_create_grouting_tools::slot_pushButton_user_key_clicked);

QMessageBox::warning(this, u8"出错啦", u8" 请先清空数据再一键生成", QMessageBox::Cancel);//消息框

/* 正则表达式对输入参数进行检查*/

bool auto_create_grouting_tools::regExpCheck(int type, QString str_value, QString& str_info)
{
	bool bRet = true;
	QRegExp reg;
	QRegExpValidator *validator;
	int pos = 0;

	switch (type) {
	case VALID_POSITIVE_INTEGER:
		reg = QRegExp("^[-?\\d+]?\\d*$");
		validator = new QRegExpValidator(reg, 0);
		if (QValidator::Acceptable != validator->validate(str_value, pos)) {
			str_info += QString(u8"输入有问题,请输入数字!");
			bRet = false;
		}
		break;
	case VALID_REAL_NUMBER:
		reg = QRegExp("^-?\\d+(\\.\\d+)?$");
		validator = new QRegExpValidator(reg, 0);
		if (QValidator::Acceptable != validator->validate(str_value, pos)) {
			str_info += QString(u8"输入有问题,请输入数字!");
			bRet = false;
		}
		delete validator;
		break;
	}
	return bRet;
}

加密解密用openssl库做的封装,目前实现了des算法和rsa算法,注意MD5只是散列表摘要算法,不要拿来作加密用。见security-verification-for-Qt;
UI设计一定要用qt creater的布局管理,可以通过设置最小尺寸和比例来获得自己想要的UI效果