Qt5编程——word操作

	QDir dir;
	QString srcPath, dstPath;
	//新建一个word应用程序,并设置为不可见
	QAxWidget *word = new QAxWidget("Word.Application");
	word->setProperty("Visible", false);

	//获取所有的工作文档
	QAxObject *documents = new QAxObject;
	documents = word->querySubObject("Documents");

	//新建一个文档
	//documents->dynamicCall("Add(void)");

	//以ReportTemplate.docx为模版新建一个文档
	srcPath = dir.currentPath() + "/docs/ReportTemplate.docx";
	documents->dynamicCall("Add(QString)", srcPath);

	//获取当前激活的文档
	QAxObject *document = word->querySubObject("ActiveDocument");

	//获取文档中的标签并写入
	QAxObject *bookmark_code;
	QAxObject *selection;
	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Name");//Name是word文档中的标签名
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", querySelected.name);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Age");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", reportAge);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Sex");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", reportSex);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Time");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", reportTime);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Assessment");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", reportAssess);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Number");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", reportID);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "IPAddr");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", querySelected.ipAndPort);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Operator");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", querySelected.username);

	//写入“hello world”
	//selection = word->querySubObject("Selection");
	//selection->dynamicCall("TypeText(const QString&)", "hello world");

	//设置保存
	dstPath = dir.currentPath() + "/" + QString::fromLocal8Bit("测试报告")+".doc";
	QVariant newFileName(dstPath);//保存路径及名称
	QVariant fileFormat(1);//文件格式
	document->dynamicCall("SaveAs(const QVariant&, const QVariant&)", newFileName, fileFormat);

	document->dynamicCall("Close (boolean)", true);
	word->dynamicCall("Quit(void)");

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
QT5串口编程可以用于编写简单的上位机,通过串口与下位机进行通信。下面是一个简单的上位机编写示例: 首先,要在QT项目加入串口模块,可以通过在.pro文件加入以下代码实现: ``` QT += serialport ``` 接下来,创建一个新的类,命名为SerialPort,继承自QObject,并添加以下代码: ```cpp #include "serialport.h" SerialPort::SerialPort(QObject *parent) : QObject(parent) { serial = new QSerialPort(this); connect(serial, &QSerialPort::readyRead, this, &SerialPort::readData); } void SerialPort::openPort(QString portName, qint32 baudRate) { serial->setPortName(portName); serial->setBaudRate(baudRate); if(serial->open(QIODevice::ReadWrite)) { qDebug() << "Serial port is open"; } else { qDebug() << "Serial port failed to open"; } } void SerialPort::closePort() { if(serial->isOpen()) { serial->close(); qDebug() << "Serial port is closed"; } } void SerialPort::readData() { QByteArray data = serial->readAll(); qDebug() << "Received data: " << data; // 在这里可以对接收到的数据进行处理 } void SerialPort::writeData(QByteArray data) { if(serial->isOpen()) { serial->write(data); qDebug() << "Sent data: " << data; } else { qDebug() << "Serial port is not open"; } } ``` 在主窗口,可以使用SerialPort类的实例来控制串口的打开、关闭和数据的读写,可以通过按钮来触发相应的操作。下面是一个简单的主窗口实现: ```cpp #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); serialPort = new SerialPort(this); connect(ui->openButton, &QPushButton::clicked, this, &MainWindow::openPort); connect(ui->closeButton, &QPushButton::clicked, this, &MainWindow::closePort); connect(ui->sendButton, &QPushButton::clicked, this, &MainWindow::sendData); } MainWindow::~MainWindow() { delete ui; } void MainWindow::openPort() { QString portName = ui->portComboBox->currentText(); qint32 baudRate = ui->baudRateComboBox->currentText().toInt(); serialPort->openPort(portName, baudRate); } void MainWindow::closePort() { serialPort->closePort(); } void MainWindow::sendData() { QString data = ui->sendLineEdit->text(); serialPort->writeData(data.toUtf8()); ui->sendLineEdit->clear(); } ``` 在QT设计器,可以创建一个MainWindow窗口,包含一个QComboBox用于选择串口号,一个QComboBox用于选择波特率,一个QLineEdit用于输入发送的数据,以及几个QPushButton用于触发相应的操作。 以上就是一个简单的QT5串口编程的上位机实现,你可以根据具体的需求进行进一步的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值