记录 QT

快捷键

复制某一行 Ctrl+Alt+上(下)键
上下移动某一行 Ctrl+Shift+上(下)键

定时器

Elecalibration::Elecalibration(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Elecalibration)
{
    ui->setupUi(this);
    connect(&timer, SIGNAL(timeout()), this, SLOT(timeout_slot()));
    timer.start(100);
}
void Elecalibration::timeout_slot(){
	...
}

start()里设置时间,超时就会触发timeout_slot()槽函数,槽函数里写自己想要实时触发的内容

connect(&timer, SIGNAL(timeout()), this, SLOT(timeout_slot()));
timer.start(100);

就这两句

计时

//.h
#include <QDateTime>
QDateTime startTime;
QDateTime endTime;
float testTime;//时间
//.cpp
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setWindowTitle(tr("自由的三子"));
    //定时器
    connect(&timer, SIGNAL(timeout()), this, SLOT(timeout_slot()));
    timer.start(100);
    startTime= QDateTime::currentDateTime();
}
void MainWindow::timeout_slot(){
    endTime= QDateTime::currentDateTime();
    testTime = (startTime.msecsTo(endTime))/1000.00;//显示到毫秒
    ui->timelabel->setText(QString("时间:%1").arg(testTime));
}

QStringList

赋值,不用[],用{}

QStringList ForceCalibrationDB = {"ForceCalibrationDB1","ForceCalibrationDB2","ForceCalibrationDB3","ForceCalibrationDB4","ForceCalibrationDB5","ForceCalibrationDB6"};

点击取消退出窗口

在这里插入图片描述

在这里插入图片描述

类型转换

int转QString:
QString::number(i);
ui->DeformationEdit->setText(QString::number(Deformation_Cal.DeformationValue,'f',2));
QString转int:
s.toInt();
DeformationValue = ui->DeformationEdit->text().toFloat();

模态窗口

模态窗口会阻止其他窗口的输入型事件(如获取焦点),但是模态窗口的子窗口不会被限制

this->setWindowModality(Qt::ApplicationModal);

添加资源

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

屏蔽 (double)spinbox 和 combobox TabWidget的滚轮事件

//禁用滑轮.cpp
Elecalibration::Elecalibration(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::Elecalibration){
    ui->setupUi(this);
    // 安装事件过滤器//禁用滑轮
    foreach (QAbstractSpinBox* sb, this->findChildren<QAbstractSpinBox*>()){
        sb->installEventFilter(this);
    }
    foreach (QComboBox* cb, this->findChildren<QComboBox*>()){
        cb->installEventFilter(this);
    }
    //禁用滑轮
    ui->curveTabWidget->tabBar()->installEventFilter(this);
    ui->parametersTabWidget->tabBar()->installEventFilter(this);
}
bool Elecalibration::eventFilter(QObject *obj, QEvent *event){
    // 屏蔽 spinbox 和 combobox 的滚轮事件
    if(obj->inherits("QAbstractSpinBox")||obj->inherits("QComboBox")) {
        if(event->type() == QEvent::Wheel) return true;
    }
    return QMainWindow::eventFilter(obj, event);
}

bool Elecalibration::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->curveTabWidget->tabBar()||watched == ui->parametersTabWidget->tabBar())
    {
        if(event->type() == QEvent::Wheel)
        {
            return true;
        }
        return QWidget::eventFilter(watched,event);
    }
    return false;
}
//.h
bool eventFilter(QObject *obj, QEvent *event);//禁用滑轮

QDoubleSpinBox 点击上下总会自动全选

在这里插入图片描述

https://blog.csdn.net/qq_22328661/article/details/102385469

 if(ui->doubleSpinBox == watched)
    {
        if(e->type() == QEvent::FocusIn)
        {
            QList<QLineEdit *> lineEditList = ui->doubleSpinBox->findChildren<QLineEdit *>();
            for(int i = 0; i < lineEditList.count(); i++)
            {
 
                int len = lineEditList.at(i)->text().length();
                lineEditList.at(i)->event(e);
                e->accept();
                lineEditList.at(i)->setCursorPosition(len);   
                return true;
            }
        }
    }

修改FocusIn为HoverLeave,鼠标移出去,全选就没了

bool Elecalibration::eventFilter(QObject *obj, QEvent *event)
{
    if(obj->inherits("QAbstractSpinBox"))
       {
           if(event->type() == QEvent::HoverLeave)
           {
               QList<QLineEdit *> lineEditList = ui->doubleSpinBox->findChildren<QLineEdit *>();
               for(int i = 0; i < lineEditList.count(); i++)
               {

                   int len = lineEditList.at(i)->text().length();
                   lineEditList.at(i)->event(event);
                   event->accept();
                   lineEditList.at(i)->setCursorPosition(len);
                   return true;
               }
           }
       }
    // 屏蔽 spinbox 和 combobox 的滚轮事件
    if(obj->inherits("QAbstractSpinBox")||obj->inherits("QComboBox")) {
        if(event->type() == QEvent::Wheel) return true;
    }
    return QMainWindow::eventFilter(obj, event);


}

弹窗限制

//.h
#include "popupdialog.h"
PopupDialog *dialog = new PopupDialog;
//.cpp
if(!dialog->isVisible()){//如果没有弹窗
	if(testforce > 100.0){
		//弹出窗口
		ui->teststopTBtn->click();
		dialog->showPopupDialog(tr("AAAAAAAAAAAAAAAAAA"));
	}
}
#include "popupdialog.h"
#include "ui_popupdialog.h"
PopupDialog::PopupDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PopupDialog){
    ui->setupUi(this);
    this->setWindowModality(Qt::ApplicationModal);
    this->setWindowTitle(tr("警告!!!"));
}
PopupDialog::~PopupDialog(){
    delete ui;
}
void PopupDialog::showPopupDialog(QString text){
    this->show();
    ui->textLab->setText(text);
}
void PopupDialog::on_pushButton_clicked(){
    this->close();
}
void PopupDialog::on_pushButton_2_clicked(){
    this->close();
}

在这里插入图片描述
在这里插入图片描述

数据导出

另存运行程序文件夹下的database到所选位置

//数据导出
void Elecalibration::Export(){
    QFileDialog fileDialog;
        //选择要存储的路径
        QString saveDir = fileDialog.getExistingDirectory(this);
        //获取文件所在路径
        QString originDir = QDir::currentPath();
		//获取路径下的所有文件
        QDir dirlist(originDir);
        //需要过滤的文件的格式,这边只筛选db文件
        QStringList namefile;
        namefile<<"*.db";
        //从程序运行文件夹中进行筛选,并返回带有db后缀的文件
        namefile = dirlist.entryList(namefile, QDir::Files | QDir::Readable, QDir::Name);
        qDebug()<<"namefile===="<<namefile;

        foreach(QString files,  namefile)
        {
            QString originFilePath = QString("%1//%2").arg(originDir).arg(files);
            qDebug()<<"originFilePath=="<<originFilePath;
            QString destFilePath = QString("%1//%2").arg(saveDir).arg(files);
            qDebug()<<"destFilePath=="<<destFilePath;
            //如果已经存在,则删除原来的文件
            if(QFile::exists(destFilePath))
            {
              QFile::remove(destFilePath);
            }
            //开始复制
            QFile::copy(originFilePath,destFilePath);
        }
}

鼠标样式

//鼠标样式
    QPixmap pixmap(":/picture/gun.png");
    QSize cSize(50, 50);//设置图片大小
    pixmap = pixmap.scaled(cSize, Qt::KeepAspectRatio);//设置图片可以自适应调整

    QCursor cursor(pixmap);//定义一个光标变量
    setCursor(cursor);

判断鼠标是否在某个控件上

if(ui->pushButton->geometry().contains(this->mapFromGlobal(QCursor::pos()))){
}
if(ui->pushButton->underMouse()){
}

特殊数学字符

平方

tr("环刚度kN/m")+"²"

在这里插入图片描述

QTableWidget可编辑设置

//设置全部单元格可编辑
ui->tableWidget->setEditTriggers(QAbstractItemView::CurrentChanged);
//设置全部单元格不可编辑
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
//设置全部单元格可编辑
ui->beforeTestparameterTWidget->setEditTriggers(QAbstractItemView::CurrentChanged);
for(int j=0; j<ExpReportText1.size(); j++)
{	 //设置第一列不可修改;
     ui->beforeTestparameterTWidget->item(j,0)->setFlags(Qt::ItemIsEnabled);
}

打包

在这里插入图片描述

复制exe文件到一个文件夹

在这里插入图片描述

打开QT的cmd

在这里插入图片描述

输入windeployqt,空格,将exe拖进去

在这里插入图片描述

自动生成路径,回车

在这里插入图片描述

完成

在这里插入图片描述

在这里插入图片描述

操作Word

插入图片不用绝对路径会报错

//打开和创建 word文件
void GenerateWord::open(){
    QString fileName = "D:/Doc1.docx";
    DeleteFileOrFolder(fileName);
    if(fileName.isEmpty())
    {
        qDebug()<<"打开失败,文件名为空";
    }
    QFile file1(fileName);
    if (!file1.exists())
    {

        //文件不存在则创建
        if(!file1.open(QIODevice::WriteOnly))
        {
            qDebug()<<"WzWord: 文件创建失败";
            file1.close();
        }
        file1.close();
    } else
    {
        //存在,但不能以写入状态打开
        if(!file1.open(QIODevice::ReadWrite))
        {
            file1.close();
        }
        file1.close();
    }

//    QAxObject* m_wordWidget = new QAxObject();
    bool flag = m_wordWidget->setControl("Word.Application");//初始化COM对象,新建一个word应用程序

    if(!flag)
    {
        flag = m_wordWidget->setControl("kwps.Application");//尝试用wps打开
        if(!flag)
            return;
    }
    m_wordWidget->setProperty("Visible", true);//设置为可见,false 则不会在界面打开显示
    //m_wordWidget->setParent(parient);

    QAxObject* documents = m_wordWidget->querySubObject("Documents");//获取所有的工作文档(返回一个指向QAxObject包含的COM对象)
    if(!documents)
        return ;
    //法 一
    QFile file(fileName);
    if(file.exists())
        documents->dynamicCall("Open(const QString&)",fileName);//以Doc1.dot新建一个
    else
        return ;
    activeDocument = m_wordWidget->querySubObject("ActiveDocument");//获取当前激活的文档
    selection  = m_wordWidget->querySubObject("Selection");


    //delete documents;


}
//selecttion  插入文字    date 文字内容   fontsize 字体大小  alignment 对其方式  默认首行缩进 Whetherbold是否加粗
void GenerateWord::writedate(QString date ,int fontsize,uchar alignment,bool Whetherbold){

    if(!selection)
        return;

    //selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleBodyTextFirstIndent2");//	正文首行缩进
    selection->querySubObject("Font")->setProperty("Name","宋体");      //设置字体
    selection->querySubObject("Font")->setProperty("Size",fontsize);              //设置字号
    selection->querySubObject("Font")->setProperty("Bold",Whetherbold);              //加粗

    selection->dynamicCall("TypeText(const QString&)",date);
    switch (alignment) {
    case 0:
        selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleBodyTextFirstIndent2");//	正文首行缩进
        break;
    case 1:
        selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphCenter");//居中
        break;
    case 2:
        selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphLeft");//左对齐
        break;
    case 3:
        selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphRight");//右对齐
        break;
    }
    selection->dynamicCall("TypeParagraph(void)");      //打个换行

    //Inserttable();
    //Inserttable();


}

//初始化 插入表格  有表头的表格
void GenerateWord::Inserttable(int rownum, int columnsnum, QStringList headList, QStringList rowList[],int fontsize){
    //插入表格
    selection->querySubObject("Font")->setProperty("Size",fontsize);              //设置字号
    QAxObject *range = selection->querySubObject("Range");
    QAxObject *tables = activeDocument->querySubObject("Tables");
    QAxObject *table = tables->querySubObject("Add(QVariant,int,int)",
    range->asVariant(),rownum,columnsnum);//map.size 行  行 和列

    table->setProperty("Style","网格型");        //这个是设置表格属性  网格 方框  和无
    table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2);//表格自动拉伸列 0固定  1根据内容调整  2 根据窗口调整

//    //下面两句  第一句是将一个表格内的边框全部去掉    下面那句是加上下边框
//    table->querySubObject("Cell(Long,Long)",1,1)->querySubObject("Range")
//        ->querySubObject("Borders")
//        ->dynamicCall("Enable",false);//设置表格底色
//    table->querySubObject("Cell(Long,Long)",1,1)->querySubObject("Range")
//        ->querySubObject("Borders(WdBorderType)",-3)->dynamicCall("LineStyle","wdLineStyleSingle");
        //->dynamicCall("LineStyle","wdLineStyleNone");//设置表格底色
    //Borders(wdBorderBottom).LineStyle = wdLineStyleSingle


    //设置表头
    for(int i=0;i<headList.size();i++)
    {
        table->querySubObject("Cell(Long,Long)",1,i+1)->querySubObject("Range")
            ->querySubObject("Shading")
            ->dynamicCall("BackgroundPatternColorIndex", "");//设置表格底色

        table->querySubObject("Cell(Long,Long)",1,i+1)->querySubObject("Range")
                ->querySubObject("ParagraphFormat")
                ->dynamicCall("Alignment", "wdAlignParagraphCenter");//设置表格居中

        table->querySubObject("Cell(Long,Long)",1,i+1)->querySubObject("Range")
        ->dynamicCall("SetText(QString)", headList.at(i));//设置表格内容
        table->querySubObject("Cell(Long,Long)",1,i+1)->querySubObject("Range")
        ->dynamicCall("SetBold(int)", true);//加粗


        table->querySubObject("Cell(Long,Long)",2,i+1)->querySubObject("Range")
            ->querySubObject("Shading")
            ->dynamicCall("BackgroundPatternColorIndex", "");//设置表格底色

        table->querySubObject("Cell(Long,Long)",2,i+1)->querySubObject("Range")
                ->querySubObject("ParagraphFormat")
                ->dynamicCall("Alignment", "wdAlignParagraphCenter");//设置表格居中

        table->querySubObject("Cell(Long,Long)",2,i+1)->querySubObject("Range")
        ->dynamicCall("SetText(QString)", headList.at(i));//设置表格内容
        table->querySubObject("Cell(Long,Long)",2,i+1)->querySubObject("Range")
        ->dynamicCall("SetBold(int)", true);//加粗
    }

    for(int i=0;i<rownum-1;i++){

        for (int j=0;j<columnsnum;j++) {
            table->querySubObject("Cell(Long,Long)",i+2,j+1)->querySubObject("Range")
                    ->querySubObject("ParagraphFormat")
                    ->dynamicCall("Alignment", "wdAlignParagraphCenter");//居左wdAlignParagraphLeft 居中wdAlignParagraphCenter

            table->querySubObject("Cell(Long,Long)",i+2,j+1)
            ->querySubObject("Range")->querySubObject("Font")
            ->setProperty("Color","wdColorBlack");//设置字体颜色
            table->querySubObject("Cell(Long,Long)",i+2,j+1)
            ->querySubObject("Range")->dynamicCall("SetText(QString)", rowList[i].at(j));

        }
    }

    QVariantList params;
    params.append(6);
    params.append(0);
    selection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt();

}
//光标跳出表格
void GenerateWord::moveForEnd()//光标移到末尾,才能真正的跳出单元格
{
    QVariantList params;
    params.append(6);
    //1  如果 wdMove ,则区域和选定内容对象的结尾都移至指定单位的末尾。
    //0  如果使用了 wdExtend ,则该区域或所选内容的末尾被扩展到指定单位的末尾。
    params.append(0);

    selection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt();
}
//将光标插入到下个单元格中
void GenerateWord::moveRight(){
    if(!selection)
        {
            return;
        }
        selection->dynamicCall("MoveRight(int)",1);
}
//合并单元格 tableindex表示的是第几个table
void GenerateWord::MergeCells(int tableIndex, int nStartRow,int nStartCol,int nEndRow,int nEndCol)
{
    QAxObject* tables = activeDocument->querySubObject("Tables");
    QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
    QAxObject* StartCell =table->querySubObject("Cell(int, int)",nStartRow,nStartCol);
    QAxObject* EndCell = table->querySubObject("Cell(int, int)",nEndRow,nEndCol);
    StartCell->querySubObject("Merge(QAxObject *)",EndCell->asVariant());
    //合并完居左
    table->querySubObject("Cell(Long,Long)",nStartRow,nStartCol)->querySubObject("Range")
            ->querySubObject("ParagraphFormat")
            ->dynamicCall("Alignment", "wdAlignParagraphLeft");//居左wdAlignParagraphLeft 居中wdAlignParagraphCenter
}
//插入图片的函数
void GenerateWord::insertCellPic(int tableIndex, int row,int column,const QString& picPath)
{
    qDebug()<<"picPath"<<picPath;
    QAxObject* tables = activeDocument->querySubObject("Tables");
    if(nullptr== tables)
    {
        return;
    }
    QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
    if(nullptr== table)
    {
        return;
    }
    QAxObject* range = table->querySubObject("Cell(int, int)", row, column)->querySubObject("Range");
    if(nullptr== range)
    {
        return;
    }
    range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)", picPath);


}
//插入文字
void GenerateWord::typeText(int tableIndex, int row,int column,QString text)
{
    QAxObject* tables = activeDocument->querySubObject("Tables");
    QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
    table->querySubObject("Cell(int,int)",row,column)->querySubObject("Range")->dynamicCall("SetText(QString)", text);
}
//没有表头的 字体大小
void GenerateWord::Inserttable( int rownum, int columnsnum, QStringList rowList[],int fontsize){
    //插入表格
    selection->dynamicCall("InsertAfter(QString&)", "\r\n");
    selection->querySubObject("Font")->setProperty("Size",fontsize);              //设置字号
    QAxObject *range = selection->querySubObject("Range");
    QAxObject *tables = activeDocument->querySubObject("Tables");
    //指定表 根据序号
    QAxObject *table = tables->querySubObject("Add(QVariant,int,int)",
    range->asVariant(),rownum,columnsnum);//map.size 行  行 和列

    table->setProperty("Style","网格型");        //这个是设置表格属性  网格 方框  和无
    table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2);//表格自动拉伸列 0固定  1根据内容调整  2 根据窗口调整

    for(int i=0;i<rownum;i++){

        for (int j=0;j<columnsnum;j++) {
            //qDebug()<<"rowList"<<rowList[i].at(j);
            table->querySubObject("Cell(Long,Long)",i+1,j+1)
            ->querySubObject("Range")->querySubObject("Font")
            ->setProperty("Color","wdColorBlack");//设置字体颜色
            table->querySubObject("Cell(Long,Long)",i+1,j+1)->querySubObject("Range")
            ->dynamicCall("SetBold(int)", false);//不加粗
            table->querySubObject("Cell(Long,Long)",i+1,j+1)
            ->querySubObject("Range")->dynamicCall("SetText(QString)", rowList[i].at(j));


            if((j+1) % 2 == 0){
                table->querySubObject("Cell(Long,Long)",i+1,j+1)->querySubObject("Range")
                        ->querySubObject("ParagraphFormat")
                        ->dynamicCall("Alignment", "wdAlignParagraphCenter");//居左wdAlignParagraphLeft 居中wdAlignParagraphCenter
            }else{
                table->querySubObject("Cell(Long,Long)",i+1,j+1)->querySubObject("Range")
                        ->querySubObject("ParagraphFormat")
                        ->dynamicCall("Alignment", "wdAlignParagraphCenter");//居左wdAlignParagraphLeft 居中wdAlignParagraphCenter
            }
        }
    }
    //光标跳出表格
    QVariantList params;
    params.append(6);
    params.append(0);
    selection->dynamicCall("EndOf(QVariant&, QVariant&)",params).toInt();
    selection->dynamicCall("TypeParagraph(void)");      //打个换行

//    delete range;
//    delete tables;
//    delete table;
}

当前界面是否显示

if(this->isVisible()){
pass
}
//超时触发
void ::timeout_slot(){
    if(this->isVisible()){
       
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值