文章目录
前言
新入职的码农,嵌入式软件方向
提示:以下是本篇文章正文内容,下面案例可供参考
一、Qt读写pdf文件(将文字图片混合加入pdf文件中)
1.qt窗口截屏
把qt窗口界面保存为图片
QPixmap pixmap = this->grab(); //获取界面的图片
2.将文字信息写入pdf文件中
//保存文字输入信息
QPrinter printer_text;
printer_text.setOutputFormat(QPrinter::PdfFormat); //设置输出格式为PDF
printer_text.setOutputFileName(savefile_name); //设置输出路径
QPainter painter_text;
painter_text.begin(&printer_text);
QPoint point(10,10);
QString msg = (SubmittingunitLabel->text()).append(SubmittingunitEdit->text());
msg.append(" ");
point.setY(30);
msg.append((EquipmentmodelLabel->text())).append(EquipmentmodelEdit->text());
msg.append(" ");
point.setY(50);
msg.append((TesttimeLabel->text())).append(TesttimeEdit->text());
painter_text.drawText(point, msg);
msg.clear();
3.将图像信息保存到pdf文件中
QRect rect = painter_text.viewport();
float multiple = (double(rect.width()) / pixmap.width());
float yMultiple = (double(rect.height()) / pixmap.height());
float fMin = (multiple < yMultiple) ? multiple : yMultiple;
painter_text.scale(fMin, fMin); //将图像(所有要画的东西)在pdf上放大multiple-1倍
point.setY(50+20);
painter_text.drawPixmap(0, 90, pixmap.width(), pixmap.height(), pixmap); //在指定位置画图
painter_text.end();
二、新建一个窗口
1.使用QWidget
代码如下(示例):
QWidget *previeWidget = new QWidget();
previeWidget->show();
2.在窗口中添加控件
代码如下(示例):
QLabel *messageLabel = new QLabel(previeWidget);
三、日期时间
1.获取当前日期
QDate date = QDate::currentDate();
QString file_date = date.toString("yyyy-MM-dd");
2.获取当前时间
QTime time = QTime::currentTime();
QString file_time = time.toString("hh-mm-ss");
四、控件之间互斥
1.mainwindow.h
public slots:
void m_pActionMeasure_PTPChecked();
void m_pActionMeasure_2048KHzChecked();
void compareChecked(QAction *newChecked);
signals:
void selectedID(QAction *newChecked);
private:
QAction *buffer;
QList<QActionGroup*> actionList;
QActionGroup * Mutual_Exclusion = nullptr;
2.mainwindow.cpp
void MainWindow::CreateMeasureActions()
{
// PTP
icon = QIcon(":/images/N_PTP.png");
m_pActionMeasure_PTP = new QAction( icon, "PTP", this );
connect( m_pActionMeasure_PTP, &QAction::triggered,this,&MainWindow::slot_StartMeasurePTP);
m_pActionMeasure_PTP->setCheckable( true );
// 2048KHz
icon = QIcon(":/images/N_2048.png");
m_pActionMeasure_2048KHz = new QAction( icon, "2048KHz", this );
connect( m_pActionMeasure_2048KHz, &QAction::triggered,this,&MainWindow::slot_StartMeasure2048kHz);
m_pActionMeasure_2048KHz->setCheckable( true );
//实现曲线显示互斥
buffer = 0;
Mutual_Exclusion = new QActionGroup(this);
Mutual_Exclusion->addAction(m_pActionMeasure_PTP);
Mutual_Exclusion->addAction(m_pActionMeasure_2048KHz);
connect(m_pActionMeasure_PTP,SIGNAL(triggered()),this,SLOT(m_pActionMeasure_PTPChecked()));
connect(m_pActionMeasure_2048KHz,SIGNAL(triggered()),this,SLOT(m_pActionMeasure_2048KHzChecked()));
connect(this,SIGNAL(selectedID(QAction*)),this,SLOT(compareChecked(QAction*)));
}
void MainWindow::compareChecked(QAction *newChecked)
{
if (newChecked != buffer) {
if (buffer != 0)
buffer->setChecked(false);
buffer = newChecked;
}
}
void MainWindow::m_pActionMeasure_2048KHzChecked()
{
if(m_pActionMeasure_2048KHz->isChecked()){
m_printMsgName = "2048KHz";
emit selectedID(m_pActionMeasure_2048KHz);
}
}
void MainWindow::m_pActionMeasure_PTPChecked()
{
if(m_pActionMeasure_PTP->isChecked()){
m_printMsgName = "PTP";
emit selectedID(m_pActionMeasure_PTP);
}
}
3.工具条中加进去控件
m_pToolbar->addAction( m_pActionMeasure_PTP );
m_pToolbar->addAction( m_pActionMeasure_2048KHz );
总结
1.越往后越得心应手
2.实现功能真的很有成就感
3.及时反馈问题,遇到不会的功能,把你的困扰讲给老板听,会理解的(他也会想他刚入职是什么样子的hhh)