QButton and QIcon don't work well together (?)
-
I took the calculator example app direct from Qt examples. I'm using Qt Creator 7.0.2 (Based on Qt 6.2.3 (Clang 13.0 (Apple), 64 bit), Built on May 23 2022 01:27:08, From revision ac1e86fe74).
I want to customize Buttons with specific images, but it seems that Buttons won't accept just any image. I have a png file, which is 300 pixels by 98 pixels that I wish to use. In spite of my specifying the size of the Icon, I can't get it to display on the button (all else seems to work).
Here's the code mostly taken from the calculator app example, leaving only the essential code:
First, the header files:
button.h:#ifndef BUTTON_H #define BUTTON_H #include <QToolButton> //! [0] class Button : public QToolButton { Q_OBJECT public: explicit Button(const QString &text, QWidget *parent = nullptr); QSize sizeHint() const override; }; //! [0] #endif
calculator.h:
#ifndef CALCULATOR_H #define CALCULATOR_H #include <QWidget> QT_BEGIN_NAMESPACE class QLineEdit; QT_END_NAMESPACE class Button; //! [0] class Calculator : public QWidget { Q_OBJECT public: Calculator(QWidget *parent = nullptr); private slots: //! [0] void gotAClick(); //! [1] private: //! [1] //! [2]A Button *createButton(const QString &text, std::string path, const char *member); //! [9] QLineEdit *display; //! [9] //! [10] }; //! [10] #endif
button.cpp:
#include "button.h" //! [0] Button::Button(const QString &text, QWidget *parent) : QToolButton(parent) { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); setText(text); } //! [0] //! [1] QSize Button::sizeHint() const //! [1] //! [2] { QSize size = QToolButton::sizeHint(); size.rheight() += 20; size.rwidth() = qMax(size.width(), size.height()); return size; } //! [2]
calculator:cpp
#include "calculator.h" #include "button.h" #include <QGridLayout> #include <QLineEdit> #include <QtMath> #include <QRawFont> #include <string> //! [0] Calculator::Calculator(QWidget *parent) : QWidget(parent) { //! [0] //! [1] display = new QLineEdit("0"); //! [1] //! [2] display->setReadOnly(true); display->setAlignment(Qt::AlignRight); display->setMaxLength(15); Button *onOffButton = createButton("188", "./x000y000_x299y097.png", SLOT(gotAClick())); //! [5] QGridLayout *mainLayout = new QGridLayout; //! [5] //! [6] mainLayout->setSizeConstraint(QLayout::SetFixedSize); mainLayout->setHorizontalSpacing(0); mainLayout->setVerticalSpacing(0); mainLayout->addWidget(display, 0, 0, 1, 6, Qt::AlignCenter); mainLayout->addWidget(onOffButton, 1, 0, 1, 2); //, Qt::AlignCenter); setLayout(mainLayout); setWindowTitle(tr("Button Icons DFW")); } //! [6] //! [34] Button *Calculator::createButton(const QString &qtext, std::string path, const char *member) { int xpos = path.rfind("/")+2; int ypos = xpos + 4; int x = std::atoi(path.substr(xpos, 3).c_str()); int y = std::atoi(path.substr(ypos, 3).c_str()); int w, h; xpos = ypos + 5; ypos = xpos + 4; w = std::atoi(path.substr(xpos, 3).c_str()) - x + 1; h = std::atoi(path.substr(ypos, 3).c_str()) - y + 1; Button *button = new Button(qtext); QIcon eq; QSize qs; qs.setWidth(w); qs.setHeight(h); eq.addFile(path.c_str(), qs, QIcon::Normal); button->setIcon(eq); button->setIconSize(qs); button->setFixedSize(qs); button->setContentsMargins(0, 0, 0, 0); connect(button, SIGNAL(clicked()), this, member); return button; } void Calculator::gotAClick() { Button *clickedButton = qobject_cast<Button *>(sender()); int digitValue = clickedButton->text().toInt(); printf("gotAClick, digitValue = %d\n", digitValue); fflush(stdout); } //! [38]
finally, main.cpp:
#include <QApplication> #include "calculator.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Calculator calc; calc.show(); return app.exec(); }
All I get is the following:
Note that if my images are more "squarish", then I do get an icon.
Can Icons be "any" size ?
-
I took the calculator example app direct from Qt examples. I'm using Qt Creator 7.0.2 (Based on Qt 6.2.3 (Clang 13.0 (Apple), 64 bit), Built on May 23 2022 01:27:08, From revision ac1e86fe74).
I want to customize Buttons with specific images, but it seems that Buttons won't accept just any image. I have a png file, which is 300 pixels by 98 pixels that I wish to use. In spite of my specifying the size of the Icon, I can't get it to display on the button (all else seems to work).
Here's the code mostly taken from the calculator app example, leaving only the essential code:
First, the header files:
button.h:#ifndef BUTTON_H #define BUTTON_H #include <QToolButton> //! [0] class Button : public QToolButton { Q_OBJECT public: explicit Button(const QString &text, QWidget *parent = nullptr); QSize sizeHint() const override; }; //! [0] #endif
calculator.h:
#ifndef CALCULATOR_H #define CALCULATOR_H #include <QWidget> QT_BEGIN_NAMESPACE class QLineEdit; QT_END_NAMESPACE class Button; //! [0] class Calculator : public QWidget { Q_OBJECT public: Calculator(QWidget *parent = nullptr); private slots: //! [0] void gotAClick(); //! [1] private: //! [1] //! [2]A Button *createButton(const QString &text, std::string path, const char *member); //! [9] QLineEdit *display; //! [9] //! [10] }; //! [10] #endif
button.cpp:
#include "button.h" //! [0] Button::Button(const QString &text, QWidget *parent) : QToolButton(parent) { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); setText(text); } //! [0] //! [1] QSize Button::sizeHint() const //! [1] //! [2] { QSize size = QToolButton::sizeHint(); size.rheight() += 20; size.rwidth() = qMax(size.width(), size.height()); return size; } //! [2]
calculator:cpp
#include "calculator.h" #include "button.h" #include <QGridLayout> #include <QLineEdit> #include <QtMath> #include <QRawFont> #include <string> //! [0] Calculator::Calculator(QWidget *parent) : QWidget(parent) { //! [0] //! [1] display = new QLineEdit("0"); //! [1] //! [2] display->setReadOnly(true); display->setAlignment(Qt::AlignRight); display->setMaxLength(15); Button *onOffButton = createButton("188", "./x000y000_x299y097.png", SLOT(gotAClick())); //! [5] QGridLayout *mainLayout = new QGridLayout; //! [5] //! [6] mainLayout->setSizeConstraint(QLayout::SetFixedSize); mainLayout->setHorizontalSpacing(0); mainLayout->setVerticalSpacing(0); mainLayout->addWidget(display, 0, 0, 1, 6, Qt::AlignCenter); mainLayout->addWidget(onOffButton, 1, 0, 1, 2); //, Qt::AlignCenter); setLayout(mainLayout); setWindowTitle(tr("Button Icons DFW")); } //! [6] //! [34] Button *Calculator::createButton(const QString &qtext, std::string path, const char *member) { int xpos = path.rfind("/")+2; int ypos = xpos + 4; int x = std::atoi(path.substr(xpos, 3).c_str()); int y = std::atoi(path.substr(ypos, 3).c_str()); int w, h; xpos = ypos + 5; ypos = xpos + 4; w = std::atoi(path.substr(xpos, 3).c_str()) - x + 1; h = std::atoi(path.substr(ypos, 3).c_str()) - y + 1; Button *button = new Button(qtext); QIcon eq; QSize qs; qs.setWidth(w); qs.setHeight(h); eq.addFile(path.c_str(), qs, QIcon::Normal); button->setIcon(eq); button->setIconSize(qs); button->setFixedSize(qs); button->setContentsMargins(0, 0, 0, 0); connect(button, SIGNAL(clicked()), this, member); return button; } void Calculator::gotAClick() { Button *clickedButton = qobject_cast<Button *>(sender()); int digitValue = clickedButton->text().toInt(); printf("gotAClick, digitValue = %d\n", digitValue); fflush(stdout); } //! [38]
finally, main.cpp:
#include <QApplication> #include "calculator.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Calculator calc; calc.show(); return app.exec(); }
All I get is the following:
Note that if my images are more "squarish", then I do get an icon.
Can Icons be "any" size ?
@oin-oin said in QButton and QIcon don't work well together (?):
Button *onOffButton = createButton("188", "./x000y000_x299y097.png", SLOT(gotAClick()));
You're using relative path to the icon. Most probably the file is not found at runtime. Usually you pack all the icons into a resource file and reference them from there.
-
@oin-oin said in QButton and QIcon don't work well together (?):
Button *onOffButton = createButton("188", "./x000y000_x299y097.png", SLOT(gotAClick()));
You're using relative path to the icon. Most probably the file is not found at runtime. Usually you pack all the icons into a resource file and reference them from there.
-
@jsulm Thanks for the suggestion, but that's not the problem. Of course I checked, and the image file is right where it is supposed to be. In fact, I put in many debugging statements (not shown) to confirm the opening of the file.
@oin-oin said in QButton and QIcon don't work well together (?):
o confirm the opening of the file.
Your code does not open a file, it just passes the filename to QIcon.
Please show the code - I'm pretty sure the path is wrong. -
I took the calculator example app direct from Qt examples. I'm using Qt Creator 7.0.2 (Based on Qt 6.2.3 (Clang 13.0 (Apple), 64 bit), Built on May 23 2022 01:27:08, From revision ac1e86fe74).
I want to customize Buttons with specific images, but it seems that Buttons won't accept just any image. I have a png file, which is 300 pixels by 98 pixels that I wish to use. In spite of my specifying the size of the Icon, I can't get it to display on the button (all else seems to work).
Here's the code mostly taken from the calculator app example, leaving only the essential code:
First, the header files:
button.h:#ifndef BUTTON_H #define BUTTON_H #include <QToolButton> //! [0] class Button : public QToolButton { Q_OBJECT public: explicit Button(const QString &text, QWidget *parent = nullptr); QSize sizeHint() const override; }; //! [0] #endif
calculator.h:
#ifndef CALCULATOR_H #define CALCULATOR_H #include <QWidget> QT_BEGIN_NAMESPACE class QLineEdit; QT_END_NAMESPACE class Button; //! [0] class Calculator : public QWidget { Q_OBJECT public: Calculator(QWidget *parent = nullptr); private slots: //! [0] void gotAClick(); //! [1] private: //! [1] //! [2]A Button *createButton(const QString &text, std::string path, const char *member); //! [9] QLineEdit *display; //! [9] //! [10] }; //! [10] #endif
button.cpp:
#include "button.h" //! [0] Button::Button(const QString &text, QWidget *parent) : QToolButton(parent) { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); setText(text); } //! [0] //! [1] QSize Button::sizeHint() const //! [1] //! [2] { QSize size = QToolButton::sizeHint(); size.rheight() += 20; size.rwidth() = qMax(size.width(), size.height()); return size; } //! [2]
calculator:cpp
#include "calculator.h" #include "button.h" #include <QGridLayout> #include <QLineEdit> #include <QtMath> #include <QRawFont> #include <string> //! [0] Calculator::Calculator(QWidget *parent) : QWidget(parent) { //! [0] //! [1] display = new QLineEdit("0"); //! [1] //! [2] display->setReadOnly(true); display->setAlignment(Qt::AlignRight); display->setMaxLength(15); Button *onOffButton = createButton("188", "./x000y000_x299y097.png", SLOT(gotAClick())); //! [5] QGridLayout *mainLayout = new QGridLayout; //! [5] //! [6] mainLayout->setSizeConstraint(QLayout::SetFixedSize); mainLayout->setHorizontalSpacing(0); mainLayout->setVerticalSpacing(0); mainLayout->addWidget(display, 0, 0, 1, 6, Qt::AlignCenter); mainLayout->addWidget(onOffButton, 1, 0, 1, 2); //, Qt::AlignCenter); setLayout(mainLayout); setWindowTitle(tr("Button Icons DFW")); } //! [6] //! [34] Button *Calculator::createButton(const QString &qtext, std::string path, const char *member) { int xpos = path.rfind("/")+2; int ypos = xpos + 4; int x = std::atoi(path.substr(xpos, 3).c_str()); int y = std::atoi(path.substr(ypos, 3).c_str()); int w, h; xpos = ypos + 5; ypos = xpos + 4; w = std::atoi(path.substr(xpos, 3).c_str()) - x + 1; h = std::atoi(path.substr(ypos, 3).c_str()) - y + 1; Button *button = new Button(qtext); QIcon eq; QSize qs; qs.setWidth(w); qs.setHeight(h); eq.addFile(path.c_str(), qs, QIcon::Normal); button->setIcon(eq); button->setIconSize(qs); button->setFixedSize(qs); button->setContentsMargins(0, 0, 0, 0); connect(button, SIGNAL(clicked()), this, member); return button; } void Calculator::gotAClick() { Button *clickedButton = qobject_cast<Button *>(sender()); int digitValue = clickedButton->text().toInt(); printf("gotAClick, digitValue = %d\n", digitValue); fflush(stdout); } //! [38]
finally, main.cpp:
#include <QApplication> #include "calculator.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Calculator calc; calc.show(); return app.exec(); }
All I get is the following:
Note that if my images are more "squarish", then I do get an icon.
Can Icons be "any" size ?
-
@oin-oin said in QButton and QIcon don't work well together (?):
o confirm the opening of the file.
Your code does not open a file, it just passes the filename to QIcon.
Please show the code - I'm pretty sure the path is wrong.@Christian-Ehrlicher Thank you for your help.
The code is there already, in calculator.cpp, function createButton.
But, (blushing and wiping the egg off my face), I somehow changed the full pathname between projects, and the file was not being opened.
Thank you for your help.
-
qDebug() << QFile::exists("./x000y000_x299y097.png"); Button *onOffButton = createButton("188", "./x000y000_x299y097.png", SLOT(gotAClick()));
-