|
24Qter豆
本帖最后由 erics_wu1983 于 2016-1-12 14:55 编辑
新手求助!想做个界面按钮,按下可以将图片通过打印机打印,但一次只能发送给打印机一个打印信息,在前一个打印完成结束前禁止再发给打印机打印任务,打印完成后再允许按钮可以按。网上看了很多例子,也买了相关书籍学习了很多,目前写了些代码,但是无法读取到系统打印机的打印状态,返回的值一直是0。
我的程序是主画面的按钮启动打印的同时开启一个子线程,在子线程中循环去读取系统打印机的打印状态,再由信号和槽让主画面的按钮可操作。信号跟槽测试启动都能正常连接。软件版本QT4.8.7
求大神帮我看下,这样能否实现?我的程序有没有问题?
mainwindow.cpp
void MainWindow::on_ok_clicked()
{ // 禁止再次按下打印按钮
ui->ok->setEnabled(false);
QPalette pal;
pal.setColor(QPalette::ButtonText, QColor(218,165,105));
ui->ok->setPalette(pal);
myCom ->open(QIODevice::WriteOnly); //以可读写方式打开串口
myCom->write(Comdatasend); // connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom())); //信号和槽函数关联,当串口缓冲区有数据时,进行读串口操作
QSettings printer_file(QCoreApplication::applicationDirPath() + "/printer.ini", QSettings::IniFormat, this); // 设置打印机设置文件名称
coo_x = printer_file.value("coo_x").toFloat(); // 坐标值X
cor_x = printer_file.value("cor_x").toFloat(); // 修正值X
dev_x = printer_file.value("dev_x").toFloat(); // 偏差值X
coo_y = printer_file.value("coo_y").toFloat(); // 坐标值Y
cor_y = printer_file.value("cor_y").toFloat(); // 修正值Y
dev_y = printer_file.value("dev_y").toFloat(); // 偏差值Y
if(cor_x==0) { cor_x=1; }
if(cor_y==0) { cor_y=1; } // 准备工作
int activated;
DWORD need, returned;
TCHAR name[500 = {0};
QString printer_name;
// 申请内存 EnumPrinters(PRINTER_ENUM_LOCAL, name, 2, NULL, 0, &need, &returned);
const LPPRINTER_INFO_2 pPrinterEnum = static_cast<LPPRINTER_INFO_2>(LocalAlloc(LPTR, need + 4));
// 万一申请内存失败
if (!pPrinterEnum) { ui->ok->setEnabled(true); return; }
// 万一列表打印机失败
if (!EnumPrinters(PRINTER_ENUM_LOCAL, name, 2, reinterpret_cast<LPBYTE>(pPrinterEnum), need, &need, &returned))
{ ui->ok->setEnabled(true); return; }
// 万一没有打印机
if (returned == 0)
{ ui->ok->setEnabled(true); return; }
// 找到当前激活的打印机
for (DWORD i = 0; i < returned; i++)
{ printer_name = QString::fromWCharArray(pPrinterEnum[i].pPrinterName);
if (printer_name == printer_info->printerName()) { activated = i; break; } }
// 判定是否打印完毕
if (pPrinterEnum[activated].cJobs != 0)
{ ui->ok->setEnabled(true); return; }
// 初始化打印机
QPrinter printer(*printer_info);
QPainter painter(&printer);
Log log_element;
// 记录打印时间
log_element.date = QDate::currentDate();
log_element.time = QTime::currentTime();
// 记录打印单价
log_element.unit = settings_file->value("unit/unit", 7).toDouble();
// 打印开始
QTransform transform_x, transform_y;
transform_x.rotate(180, Qt::XAxis);
transform_y.rotate(180, Qt::YAxis); //int prcoo_x=(int)coo_x; // int prcoo_y=(int)coo_y;
for (QObjectList::size_type i = 0; i < ui->nail->children().size(); i++)
if (!static_cast<Nail*>(ui->nail->children().at(i))->isHidden() && !static_cast<Nail*>(ui->nail->children().at(i))->get_path().isEmpty())
{ QRect PrintMakep =static_cast<Nail*>(ui->nail->children().at(i))->geometry();
PrintMakep.adjust(0, 0,coo_x,coo_y );
// painter.drawPixmap(static_cast<Nail*>(ui->nail->children().at(i))->geometry(), painter.drawPixmap(PrintMakep, static_cast<Nail*>(ui->nail->children().at(i))->get_raw(). transformed(transform_x*cor_x).transformed(transform_y*cor_y));
log_element.file = static_cast<Nail*>(ui->nail->children().at(i))->get_path();
log->append(log_element); }
painter.end();
// 保存打印记录
log_file->beginWriteArray("log");
for (QList<Log>::size_type i = 0; i < log->size(); i++)
{ log_file->setArrayIndex(i);
log_file->setValue("date", log->at(i).date);
log_file->setValue("time", log->at(i).time);
log_file->setValue("unit", log->at(i).unit);
log_file->setValue("file", log->at(i).file); } log_file->endArray();
myCom->close(); //关闭串口,该函数在win_qextserialport.cpp文件中定义
//启动线程判断
thread->start();
}
子线程程序
#include "mythread.h"
mythread::mythread(QObject *parent): QThread(parent)
{//stopped = false;}
mythread::~mythread()
{
}
void mythread::run()
{
int activated;
DWORD need, returned;
TCHAR name[500 = {0};
printer_info = new QPrinterInfo(QPrinterInfo::defaultPrinter());
// 申请内存 EnumPrinters(PRINTER_ENUM_LOCAL, name, 2, NULL, 0, &need, &returned);
const LPPRINTER_INFO_2 pPrinterEnum = static_cast<LPPRINTER_INFO_2>(LocalAlloc(LPTR, need + 4)); // 找到当前激活的打印机
// PRINTER_STATUS_PRINTING;
for (DWORD i = 0; i < returned; i++)
{
printer_name = QString::fromWCharArray(pPrinterEnum[i].pPrinterName);
if (printer_name == printer_info->printerName())
{
activated = i;
break; }
}
sleep(10);
while(1) { EnumPrinters(PRINTER_ENUM_LOCAL, name, 2, NULL, 0, &need, &returned);
const LPPRINTER_INFO_2 pPrinterEnum = static_cast<LPPRINTER_INFO_2>(LocalAlloc(LPTR, need + 4));
if (pPrinterEnum[activated].cJobs ==0)
{
emit Updateok();
break; }
}
}
|
|