How can I know which printer is selected - c++

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.

You are probably looking for the printer name: QPrinter::printerName.
QPrinter printer;
QPrintDialog printDialog(printer);
if (printDialog.exec() == QDialog::Accepted) {
// executed after the print dialog is closed and accepted.
if (printer.printerName() == "printerD") {
// show a message or do something special ...
}
// print ...
}
Note that QPrintDialog does not provide any method to check the selected printer while the printer dialog is still open (not yet accepted). Using undocumented hacks such as using findChildren will not work in a cross platform way as native print dialog may be used[1]:
On Windows and macOS, the native print dialog is used, which means
that some QWidget and QDialog properties set on the dialog won't be
respected. The native print dialog on macOS does not support setting
printer options, i.e. setOptions() and setOption() have no effect.
If you really want/need to determine printer selection in realtime, you should implement your own custom printer dialog.

Related

Call button click function from grandchild

I'm creating my first C++ wxWidgets application. I'm trying to create some kind of split button where the options are displayed in a grid. I have a custom button class which, when right-clicked on, opens a custom wxPopupTransientWindow that contains other buttons.
When I click on the buttons in the popup, I want to simulate a left click on the main button. I'm trying to achieve this through events, but I'm kinda confused.
void expandButton::mouseReleased(wxMouseEvent& evt)
{
if (pressed) {
pressed = false;
paintNow();
wxWindow* mBtn = this->GetGrandParent();
mBtn->SetLabel(this->GetLabel());
mBtn->Refresh();
wxCommandEvent event(wxEVT_BUTTON);
event.SetId(GetId());
event.SetEventObject(mBtn);
mBtn-> //make it process the event somehow?
wxPopupTransientWindow* popup = wxDynamicCast(this->GetParent(), wxPopupTransientWindow);
popup->Dismiss();
}
}
What is the best way to do this?
You should do mBtn->ProcessWindowEvent() which is a shorter synonym for mBtn->GetEventHandler()->ProcessEvent() already mentioned in the comments.
Note that, generally speaking, you're not supposed to create wxEVT_BUTTON events from your own code. In this particular case and with current (and all past) version(s) of wxWidgets it will work, but a cleaner, and guaranteed to also work with the future versions, solution would be define your own custom event and generate it instead.

Is it possible to remove "Write to PDF" from QPrinter dialog called from QPrintPreviewDialog?

I wan't to remove under Linux, "Write to PDF" from printers list in QPrinter settings dialog that is called, when printing document from QPrintPreviewDialog. Is it possible to do?
I've tried replacing print icon inside QPrintPreviewDialog with my own button and action, but still Qt shows standard QPrinter dialog, and I don't know how to remove "Write to PDF" from printers list in that dialog.
Short answer: no, there's no way to alter the printers list in the default print dialog.
Workaround: if the user choose that "Print to file" option, don't print anything (and maybe tell them they won't have a PDF from your app).
You can do it this way: connect the QPrintPreviewDialog::paintRequested signal to a slot like this:
void Widget::onPaintRequested(QPrinter *printer)
{
QVariant printername = printer->printEngine()->property(QPrintEngine::PPK_PrinterName);
if(printername.toString().isEmpty())
{
QMessageBox::information(this, "Print issue", "Print to file is not available for weird unknown reasons ...");
}
else
{
//supply the requested paint code
}
}
Some more (maybe inspiring) thoughts on the topic in the answer to this SO question.

How to catch information for Qt designer

I have created a Qdialog box using the Qt creator designer as shown below:
When I need to display it, I'm instantiate the class dialogoverwrite (.cpp, .h and .ui)
DialogOverwrite *OverwriteDialog = new DialogOverwrite;
OverwriteDialog->exec();
OverwriteOption = OverwriteDialog->result()
My issue is that I want to get the QDialogButtonBox result but I do not know how. the current code, returning the result of the OverwriteDialog but it's not returning any QDialogButtonBox::Yes, QDialogButtonBox::YesToAll ...
How to catch the QButtonGroup result and not the QDialog result.
In the same way, If I want to change the label value from "File(s) and/or Folder(s)" to another label, how to access to this QLabel ?
Thanks for your help
When you pressed QDialogButton it was emit signal clicked(QAbstractButton*) by catching this signal you can identify which action button pressed.
Please go through following link it would be help you.
Qt: How to implement QDialogButtonBox with QSignalMapper for non-standard button ??
Well the standard way to do this is to handle the result by connecting it. So you could do:
connect(this, SIGNAL(clickedDialogButton(QAbstractButton*)),
SLOT(dialogButton(QAbstractButton* aButton)));
Next you would create a function in your class called dialogButton (for example) and have that handle the result:
void MyUI::dialogButton(QAbstractButton* aButton) {
// Obtain the standard button
StandardButton button = buttonBox−>standardButton(button);
// Switch on the type of button
switch (button) {
case QDialogButtonBox::YesToAll:
// Do the thing you would like to do here
break;
// add some more cases?
}
}
You could also check for the signal given by the QButtonGroup. Something like: void QGroupButton::buttonClicked(QAbstractButton* button) would work in the same way.

Detecting PDF Printing on Mac

I use a QPrintDialog to initialize a QPrinter object like this:
QPrinter printer;
QPrintDialog dlg(&printer);
if (dlg.exec() == QDialog::Accepted)
{
/* Are we printing to PDF? */
}
On Windows, it's easy to detect if the output is going to a file or to a PDF writer. On a Mac, none of the same functions work:
if ((printer.outputFormat() == QPrinter::PdfFormat)
|| (!printer.outputFileName().isEmpty()))
{
qDebug("PDF mode");
}
Looking at a copy of qprintdialog_mac.mm online, in the function QPrintDialogPrivate::closeCarbonPrintPanel(), Qt attempts to detect if the output is redirected to a file. It stores the file name in a member of QMacPrintEnginePrivate. Somehow that name never makes its way to the QPrinter object. I'm not sure where the disconnect is.
So..... how can I tell if the print output is actually going to a file? I'm willing to get platform specific here if it's easy. I have zero Mac programming experience though.
This was a bug in Qt.
In Qt 5.3 the sample code provided will work because of the second condition, the call to QPrinter::outputFileName().
As of Qt 5.14, the outputFileName property of QPrinter is still null, even if "Save as PDF" was selected in the QPrintDialog.
However, when painting to the QPrinter object, the right thing is done.
if "Open with Preview" was selected, the painted contents will open
in preview.
if "Save as PDF" is selected, a file dialog will pop up
if "Send in Mail" is selected, the mail client will open with the PDF
attaced
etc.
Somehow, the QPrinter seems to store all the info from the dialog in an opaque way not accessible through public getters.
The correct way to support all the options from the mac print dialog seems to be:
QPrinter printer;
QPrintDialog dlg(&printer);
if (dlg.exec() == QDialog::Accepted)
{
QPainter painter;
painter.begin(&printer);
// do the painting
painter.end();
}
Unfortunately, it does not seem possible to extract the info out of the QPrinter object in case you like to implement your own printing logic.

Taking data from a Dialog in Qt and using it in a Ui

So I'm making a text editor using Qt and right now I have a button that opens a dialog called "Format text". I want it to work kind of like the dialog in notepad called "font" where you select a few text attributes from some drop down lists and it shows you what your text will look like. Right now I have it working where you can select the font style, font color, and font size and hit preview and it shows you in a box in the dialog what your text will look like. However, I have a button called "okay" which is supposed to change the highlighted text or the text you are about to type, but I can't figure out how to display those changes on the main window. The .ui files are private and a lot of the already made functions and pointers are the same in every ui file so if I change the ui file to pubic I have to change a whole bunch of things. Can anyway give me a simple answer? I'm trying to do this with as little confusion as possible. More coding and less confusion is better than less coding and more confusion for someone of my skill level. Sorry that this is all one giant paragraph and that I didn't provide any code, but I didn't think the code was necessary, however if you do need some of the code i'd be happy to share it.
Thank you for your help and your time. I hope you all have a nice evening.
QDialog have a signal called finished(), you can connect this signal with your slot. To accomplish your work, pass a QSettings or for simplicity QStringList to dialog settings (responsible for changing font, color ...), the QStringList will save user defined settings, after closing the dialog, iterate through QStringList member to alert Main window.
A pseudo code will look like this
Class Editor:
Editor::Editor()
{
TextSettings textSettings;
textSettings.setSettings(settings); // settings is a member
connect(textSettings, &finished(int)), this, SLOT(alertEditor(int)))
}
Editor::alertEditor(int s)
{
if(s == 0)
{
for (int i = 0; i < settings.size(); ++i)
settings.at(i).toLocal8Bit().constData(); // extract various user settings
}
}
Class TextSettings:
TextSettings::TextSettings(QStringList settings)
{
settings << ui->combobox->currentItem(); // font name as example
}