代码void Index::on_pushButton_2_clicked(){ QList<QTableWidgetItem*> items = ui->tableWidget->selectedItems(); int count = items.count(); if(count!=0){ printer=new QPrinter(); QSettings settings("./saveFile/printsetting.ini",QSettings::IniFormat); settings.setIniCodec("UTF8"); settings.beginGroup("/systemCfg"); int value2 = settings.value("getModeSelect1").toInt(); settings.endGroup(); if(value2==0){ printer->setOrientation(QPrinter::Portrait); printer->setPageSize(QPrinter::A5); } pd = new QPrintPreviewDialog(printer); pd->setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint); if(value2==0||value2==1){ connect(pd,SIGNAL(paintRequested(QPrinter*)),this,SLOT(printPreview1(QPrinter*))); }else{ connect(pd,SIGNAL(paintRequested(QPrinter*)),this,SLOT(printPreview(QPrinter*))); } pd->resize(800,1050); pd->exec(); delete pd; delete printer; } }中怎么使用QPrintPreviewDialog类的静态函数setDefaultPrinter()保留打印预览的设置
时间: 2024-03-26 08:39:16 浏览: 85
在上述代码中,可以在 `printer` 对象创建后,调用 `QPrintPreviewDialog::setDefaultPrinter(printer)` 来设置默认打印机。这样,在打印预览的时候,会自动使用该打印机。
具体实现可以在 `printer` 对象创建之后,添加如下代码:
```c++
QPrintPreviewDialog::setDefaultPrinter(printer);
```
这样,打印预览对话框就会使用该打印机来进行预览和打印。同时,在程序退出时,可以在析构函数中调用 `QPrintPreviewDialog::resetDefaultPrinter()` 来重置默认打印机。
完整代码如下:
```c++
void Index::on_pushButton_2_clicked(){
QList<QTableWidgetItem*> items = ui->tableWidget->selectedItems();
int count = items.count();
if(count!=0){
printer=new QPrinter();
QSettings settings("./saveFile/printsetting.ini",QSettings::IniFormat);
settings.setIniCodec("UTF8");
settings.beginGroup("/systemCfg");
int value2 = settings.value("getModeSelect1").toInt();
settings.endGroup();
if(value2==0){
printer->setOrientation(QPrinter::Portrait);
printer->setPageSize(QPrinter::A5);
}
// 设置默认打印机
QPrintPreviewDialog::setDefaultPrinter(printer);
pd = new QPrintPreviewDialog(printer);
pd->setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
if(value2==0||value2==1){
connect(pd,SIGNAL(paintRequested(QPrinter*)),this,SLOT(printPreview1(QPrinter*)));
}else{
connect(pd,SIGNAL(paintRequested(QPrinter*)),this,SLOT(printPreview(QPrinter*)));
}
pd->resize(800,1050);
pd->exec();
delete pd;
delete printer;
}
}
Index::~Index()
{
// 重置默认打印机
QPrintPreviewDialog::resetDefaultPrinter();
delete ui;
}
```
阅读全文