From 04e76ec857efea1c28322cd44cb04f15134944fe Mon Sep 17 00:00:00 2001
From: Marc Mutz <marc.mutz@kdab.com>
Date: Tue, 29 Dec 2015 10:04:55 +0100
Subject: [PATCH] QUnixPrintWidget: fix some poor uses of the QComboBox API

- populate the widget with addItems(QStringList) instead of
  looping over addItem(QString)
- Use findText() instead of looping over itemText(int).
  QComboBox::findText() delegates searching to the model
  (via QAbstractItemModel::match()), so is potentially
  much faster. I say potentially, because match() isn't
  properly reimplemented in most models, yet. But that is
  something to fix in the models.

Change-Id: I6e52cf5af810ab7869c0270504a241868a5ca281
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
---
 .../dialogs/qprintdialog_unix.cpp             | 25 ++++++++-----------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/src/printsupport/dialogs/qprintdialog_unix.cpp b/src/printsupport/dialogs/qprintdialog_unix.cpp
index 9633032a0f4..c6c84ca701b 100644
--- a/src/printsupport/dialogs/qprintdialog_unix.cpp
+++ b/src/printsupport/dialogs/qprintdialog_unix.cpp
@@ -661,18 +661,16 @@ QUnixPrintWidgetPrivate::QUnixPrintWidgetPrivate(QUnixPrintWidget *p, QPrinter *
     widget.setupUi(parent);
 
     int currentPrinterIndex = 0;
-    QStringList printers;
-    QString defaultPrinter;
     QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
     if (ps) {
-        printers = ps->availablePrintDeviceIds();
-        defaultPrinter = ps->defaultPrintDeviceId();
-    }
+        const QStringList printers = ps->availablePrintDeviceIds();
+        const QString defaultPrinter = ps->defaultPrintDeviceId();
+
+        widget.printers->addItems(printers);
 
-    for (int i = 0; i < printers.size(); ++i) {
-        widget.printers->addItem(printers.at(i));
-        if (printers.at(i) == defaultPrinter)
-            currentPrinterIndex = i;
+        const int idx = printers.indexOf(defaultPrinter);
+        if (idx >= 0)
+            currentPrinterIndex = idx;
     }
     widget.properties->setEnabled(true);
 
@@ -827,12 +825,9 @@ void QUnixPrintWidgetPrivate::applyPrinterProperties()
         widget.filename->setText( printer->outputFileName() );
     QString printerName = printer->printerName();
     if (!printerName.isEmpty()) {
-        for (int i = 0; i < widget.printers->count(); ++i) {
-            if (widget.printers->itemText(i) == printerName) {
-                widget.printers->setCurrentIndex(i);
-                break;
-            }
-        }
+        const int i = widget.printers->findText(printerName);
+        if (i >= 0)
+            widget.printers->setCurrentIndex(i);
     }
     // PDF printer not added to the dialog yet, we'll handle those cases in QUnixPrintWidgetPrivate::updateWidget
 
-- 
GitLab