• 搜索:    
 

QT中设置打印机为默认打印 - C++QT

时间:2019-07-03

//设置setting.ini文件记录选择的打印机

void MainWindow::WriteConfing(const QString className,const QString classKeyName,const QString classNameVal){

               const QString classAndKeyName = QString("%1/%2").arg(className).arg(classKeyName);

               QSettings *configIniWrite = new QSettings("setting.ini", QSettings::IniFormat);

               configIniWrite->setValue(classAndKeyName,classNameVal);

               delete configIniWrite;

}

QString MainWindow::ReadConfing(const QString className,const QString classKeyName){

               const QString classAndKeyName = QString("%1/%2").arg(className).arg(classKeyName);

               QSettings *configIniRead = new QSettings("setting.ini", QSettings::IniFormat);

               QString classNameVal = configIniRead->value(classAndKeyName).toString();

               delete configIniRead;

               return classNameVal;

}

 

//检查配置文件setting.ini

//[printsName]

//printsDefName=hp 1005

//【节名】 变量名=值

QString printNameStr = ReadConfing("printsName","printsDefName");

if(printNameStr.isEmpty()){

//选择打印机窗口

              QScopedPointer<QPrintDialog> dialog(new QPrintDialog(printer,this));

              dialog->setWindowTitle(tr("选择打印机"));

              if (dialog->exec() != QDialog::Accepted) {

                            return;

              }

              WriteConfing("printsName","printsDefName",QString(printer->printerName()));

              printNameStr = QString(printer->printerName());

}

//设置选择的打印机

printer->setPrinterName(printNameStr);

1