QAbstractButton* MainWindow::createIconButton(const QString &iconPath, const QString &text, QWidget *parent) const { QCheckBox *button = new QCheckBox(text, parent); //button->setIcon(QIcon(iconPath)); button->setStyleSheet("QCheckBox::indicator { image: url(" + iconPath.arg("") + "); }" "QCheckBox::indicator:hover { image: url(" + iconPath.arg("_hovered") + "); }" "QCheckBox::indicator:pressed { image: url(" + iconPath.arg("_pressed") + "); }"); button->setIconSize(QImage(iconPath).size()); button->setContentsMargins(0, 0, 0, 0); button->setCursor(Qt::PointingHandCursor); button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); return button; }
/// /// \brief Widget::loadIcons /// load all icons from items.json void Widget::loadIcons() { QFile file; QString txt; file.setFileName(":/resources/items.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); txt = file.readAll(); file.close(); // parse JSON QJsonDocument doc = QJsonDocument::fromJson(txt.toUtf8()); QJsonObject obj = doc.object(); for (auto it = obj.begin(); it != obj.end(); ++it) { QCheckBox* check = new QCheckBox(); check->setText((*it).toObject().value("complete_name").toString()); check->setIcon(QIcon((*it).toObject().value("icon").toString())); check->setIconSize(QSize(50,50)); ui->gridLayout->addWidget(check); } }
void rareColorsDialog::processNewMin(int min) { // clean out the old rows for (int i = checkboxes_.size() - 1; i >= 0; --i) { delete checkboxes_.takeAt(i); } // see sarcastic note about this krap at the bottom of this bleepin // function delete scrollWidget_; // deletes any children scrollWidget_ = new QWidget(scroll_); colorsLayout_ = new QVBoxLayout; scrollWidget_->setLayout(colorsLayout_); QList<QRgb> rareColors; QList<QRgb> commonColors; for (QHash<QRgb, int>::const_iterator it = colorCounts_.begin(); it != colorCounts_.end(); ++it) { if (it.value() <= min) { rareColors.push_back(it.key()); } else { commonColors.push_back(it.key()); } } // sort the rare colors by intensity qSort(rareColors.begin(), rareColors.end(), qRgbIntensity()); if (!commonColors.isEmpty() && !rareColors.isEmpty()) { // each row consists of a checkbox saying whether or not to include // the row's color, followed by the rare color and its count and then // the replacement color for the rare color for (int i = 0, size = rareColors.size(); i < size; ++i) { QRgb thisOldColor = rareColors[i]; // find the new color QRgb thisNewColor = ::closestMatch(thisOldColor, commonColors); const int thisColorCount = colorCounts_[thisOldColor]; const QString squareString = (thisColorCount == 1) ? " square " : " squares "; const QString iconString(::itoqs(thisColorCount) + squareString + ::colorToPrettyString(thisOldColor) + " --> " + ::colorToPrettyString(thisNewColor)); QCheckBox* checkBox = new QCheckBox(iconString); const QPixmap iconPixmap = createIconPixmap(thisOldColor, thisNewColor); checkBox->setIconSize(QSize(ICON_SIZE, ICON_SIZE)); checkBox->setIcon(QIcon(iconPixmap)); checkBox->setChecked(true); checkboxes_.push_back(checkBox); colorsLayout_->addWidget(checkBox); } } else if (rareColors.isEmpty()) { noRareColorsLabel_ = new QLabel(tr("There aren't any colors that occur ") + ::itoqs(minCountBox_->value()) + " or fewer times!"); colorsLayout_->addWidget(noRareColorsLabel_); } else if (commonColors.isEmpty()) { noReplacementColorsLabel_ = new QLabel(tr("There are no replacement colors available!")); colorsLayout_->addWidget(noReplacementColorsLabel_); } scrollWidget_->resize(colorsLayout_->sizeHint()); // if you put this up where the other stuff gets recreated (or you don't // recreate the other stuff) then on the second and all later runs // through this function colorsLayout_->sizeHint() will return (18, 18). // Obviously. scroll_->setWidget(scrollWidget_); }