mercredi 4 août 2021

Qt: How can I know which printer is selected

I have a program with the typical printer dialog (made with QPrintDialog from Qt5.1) where you can select the printer you want from a list. Let's say you have printerA (selected by default), printerB, printerC and printerD. I have a problem with printerD so when the user selects it I want to do something (maybe show a popup message or maybe dissable some buttons).

I've been looking for how to know if printerD is selected but couldn't find it. I've search in QPrinter, QPrinterInfo, QPrintDialog and QDialog and I couldn't find it. Also I've found this here which I though it could help me but the answer only tells you how to set the default one.

If you don't know which dialog I am talking about, it's the same from here.

I also was thinking if this list may be a QListWidget Class but if it was that, i 'don't know how to get it.

So my question is: How may I know which is the printer selected in any moment of this PrinterDialog life? (While showing up).

Also, I want to add, that I tried to dissable the button for the printer which gives me error with the next code but the QList is empty so I can't reach the Print or Preferences Button to dissable it I don't know why:

 QList<QPrinterInfo> info_list = QPrinterInfo::availablePrinters();
 foreach(QPrinterInfo info, info_list)
 {
     if (info.state() == 3) // the printer gives error
     {
        QList<QPushButton *> allButtons = printDialog.findChildren<QPushButton *>();

       for (int i = 0; i < allButtons.size(); i++) 
       {
          if (allButtons.at(i)->text().contains("Print") || allButtons.at(i)->text().contains("Preferences"))
          {
           allButtons.at(i)->setDisabled(true);
          }
       }
     }
  }

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire