Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QButton and QIcon don't work well together (?)
Forum Updated to NodeBB v4.3 + New Features

QButton and QIcon don't work well together (?)

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 402 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oin-oin
    wrote on last edited by
    #1

    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:
    noIcon.png

    Note that if my images are more "squarish", then I do get an icon.

    Can Icons be "any" size ?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • O oin-oin

      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:
      noIcon.png

      Note that if my images are more "squarish", then I do get an icon.

      Can Icons be "any" size ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      O 1 Reply Last reply
      3
      • jsulmJ jsulm

        @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.

        O Offline
        O Offline
        oin-oin
        wrote on last edited by
        #3

        @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.

        Christian EhrlicherC 1 Reply Last reply
        0
        • O oin-oin

          @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.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          O 1 Reply Last reply
          0
          • O oin-oin

            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:
            noIcon.png

            Note that if my images are more "squarish", then I do get an icon.

            Can Icons be "any" size ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @oin-oin

            qDebug() << QFile::exists("./x000y000_x299y097.png");
            Button *onOffButton = createButton("188", "./x000y000_x299y097.png", SLOT(gotAClick()));
            
            O 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @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.

              O Offline
              O Offline
              oin-oin
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0
              • JonBJ JonB

                @oin-oin

                qDebug() << QFile::exists("./x000y000_x299y097.png");
                Button *onOffButton = createButton("188", "./x000y000_x299y097.png", SLOT(gotAClick()));
                
                O Offline
                O Offline
                oin-oin
                wrote on last edited by
                #7

                @JonB Thank you for your help. Indeed, as in a previous post, to my great shame going between projects I hadn't noticed that during cutting and pasting I changed the full pathname, and was debugging the wrong project !

                1 Reply Last reply
                0
                • O oin-oin has marked this topic as solved on

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved